qzb1111
31st July 2007, 08:38
My application is to do some file operations.You know while processing file of large size ,the application looks as if it is dead.So I put all my works into QThread like this:
class MainOperation:public QThread
{
Q_OBJECT
....
public slots:
virtual void run()
{
FileWriter fw1(_errorFile);
FileReviser fr(_templateFile);
fw1.appendReviser(&fr);
emit fileSizedRecognized(fw1.GetFileSize());
while(!fw1.IsOK())
{
fw1.save(_outputFile);
emit posChanged(fw1.GetPos());
}
emit finished();
}
signals:
void fileSizedRecognized(int fileSize);
void posChanged(int pos);
void finished();
}
Then I want these signals to be sent to My UI Form then update the position of the progressbar like this(below are slots in my UI Form):
public slots:
void prepareFile(int fileSize)
{
pushButton_Begin->setEnabled(false);
pushButton_end->setEnabled(false);
_fileSize=fileSize;
progressBar->setTotalSteps(100);
}
void setProgressBarPos(int pos)
{
progressBar->setProgress(pos/_fileSize*100);
}
void finish()
{
pushButton_end->setEnabled(true);
}
But it doesn't work.These slots are fired but the ProgressBar's position is always zero.Below is code fragments in main.cpp
Ui::Form ui;
ui.setupUi(&w);
ui.label_ErrorFile->setText(sErrorFile);
ui.label_TemplateFile->setText(sTemplateFile);
ui.label_OutputFile->setText(sOutputFile);
MainOperation mainOp(sErrorFile,sTemplateFile,sOutputFile);
QObject::connect(ui.pushButton_Begin,SIGNAL(clicke d()),&mainOp,SLOT(start()
));
QObject::connect(&mainOp,SIGNAL(fileSizedRecognized(int)),&ui,SLOT(prepareF
ile(int)));
QObject::connect(&mainOp,SIGNAL(posChanged(int)),&ui,SLOT(setProgressBarPos
(int)));
QObject::connect(&mainOp,SIGNAL(finished()),&ui,SLOT(finish()));
QObject::connect(ui.pushButton_end, SIGNAL(clicked()), &app, SLOT(quit()));
I was really confused,expecting your help.Thanks!
class MainOperation:public QThread
{
Q_OBJECT
....
public slots:
virtual void run()
{
FileWriter fw1(_errorFile);
FileReviser fr(_templateFile);
fw1.appendReviser(&fr);
emit fileSizedRecognized(fw1.GetFileSize());
while(!fw1.IsOK())
{
fw1.save(_outputFile);
emit posChanged(fw1.GetPos());
}
emit finished();
}
signals:
void fileSizedRecognized(int fileSize);
void posChanged(int pos);
void finished();
}
Then I want these signals to be sent to My UI Form then update the position of the progressbar like this(below are slots in my UI Form):
public slots:
void prepareFile(int fileSize)
{
pushButton_Begin->setEnabled(false);
pushButton_end->setEnabled(false);
_fileSize=fileSize;
progressBar->setTotalSteps(100);
}
void setProgressBarPos(int pos)
{
progressBar->setProgress(pos/_fileSize*100);
}
void finish()
{
pushButton_end->setEnabled(true);
}
But it doesn't work.These slots are fired but the ProgressBar's position is always zero.Below is code fragments in main.cpp
Ui::Form ui;
ui.setupUi(&w);
ui.label_ErrorFile->setText(sErrorFile);
ui.label_TemplateFile->setText(sTemplateFile);
ui.label_OutputFile->setText(sOutputFile);
MainOperation mainOp(sErrorFile,sTemplateFile,sOutputFile);
QObject::connect(ui.pushButton_Begin,SIGNAL(clicke d()),&mainOp,SLOT(start()
));
QObject::connect(&mainOp,SIGNAL(fileSizedRecognized(int)),&ui,SLOT(prepareF
ile(int)));
QObject::connect(&mainOp,SIGNAL(posChanged(int)),&ui,SLOT(setProgressBarPos
(int)));
QObject::connect(&mainOp,SIGNAL(finished()),&ui,SLOT(finish()));
QObject::connect(ui.pushButton_end, SIGNAL(clicked()), &app, SLOT(quit()));
I was really confused,expecting your help.Thanks!