PDA

View Full Version : Use of QProgressBar in Thread



merry
5th January 2009, 09:20
Hi All

Working on Qt4.4.3, using my intel mac machine. What I am trying to do is,
I am selecting a file/folder using QFileDialog, and open that file in read write mode and writing that file with some other text, For this process I am using thread, I am selecting file in one class, and after selection i called thread , Using thread , I called some other class for writing on that selected file . For writing on the file, i want to run progressBar, till the writing completes, but its not working, i dont know why,


void ClassWrite::fnWriteFile(FILE *fp, int nFillWith)
{
unsigned long value = 0;
int iProgressCounter = 0;
long long llSizeRead = 0;
unsigned long SizetoRead =0;
long long SizeRead = 0;
QFileInfo filename(m_FileName);
llSizeRead = filename.size();
QString strMessage = "";
unsigned char *pBuffer = NULL;
unsigned long dwBufferValue = 4*1024*1024;
pBuffer = (unsigned char *)calloc(dwBufferValue,sizeof(unsigned char));
if(pBuffer == NULL)
return;

strMessage = "WritingFile " + m_FileName;
emit ProgressMaximumValue(100);
qApp->processEvents();

while(llSizeRead>0)
{
if(bStopFlag)
break;


if(llSizeRead>dwBufferValue)
{
SizetoRead = dwBufferValue;
value = ((SizetoRead/llSizeRead)*100);
if(iProgressCounter == 0 || iProgressCounter == 100)
{
emit SetProgressValue(strMessage,value);
qApp->processEvents();
memset(pBuffer,nFillWith,SizetoRead);
fseek(fp, 0+SizeRead, SEEK_CUR);
fwrite((void*)pBuffer,SizetoRead,1,fp);
llSizeRead-=SizetoRead;
SizeRead = SizetoRead;
iProgressCounter = 0;
}
iProgressCounter++;
}
else
{
SizetoRead = llSizeRead;
value = ((SizetoRead/llSizeRead)*100);
if(iProgressCounter == 0 || iProgressCounter == 100)
{
emit SetProgressValue(strMessage,value);
qApp->processEvents();
memset(pBuffer,nFillWith,SizetoRead);
fseek(fp, 0+SizeRead, SEEK_CUR);
fwrite((void*)pBuffer,SizetoRead,1,fp);
llSizeRead-=SizetoRead;
SizeRead = SizetoRead;
iProgressCounter = 0;
}
iProgressCounter++;
}


}

fflush(fp);
if(pBuffer)
{
free(pBuffer);
pBuffer = NULL;
}
}


Pls suggest me something

spirit
5th January 2009, 10:21
try something like this


class AnotherThread: public QThread
{
Q_OBJECT

signals:
void valueChanged(int);

public:
AnotherThread(QObject *parent, int val)
: QThread(parent),
m_currentVal(val)
{
}

protected:
virtual void run() {
for (int i = 0; i < m_currentVal; ++i)
emit valueChanged(i);
}

private:
int m_currentVal;
};

class MainWindow:public QMainWindow
{
Q_OBJECT

public:
MainWindow::MainWindow(QWidget *parent = 0)
: QMainWindow(parent)
{
QHBoxLayout *mainLayout = new QHBoxLayout();

m_pbProgress = new QProgressBar();
m_pbProgress->setMinimum(0);
m_pbProgress->setMaximum(100);

m_at = new AnotherThread(this, m_pbProgress->maximum());
connect(m_at, SIGNAL(valueChanged(int)), m_pbProgress, SLOT(setValue(int)));

QPushButton *pbRun = new QPushButton("Run");
connect(pbRun, SIGNAL(clicked()), m_at, SLOT(start()));

mainLayout->addWidget(m_pbProgress);
mainLayout->addWidget(pbRun);

QWidget *central = new QWidget();
central->setLayout(mainLayout);
setCentralWidget(central);
}

private:
QProgressBar *m_pbProgress;
AnotherThread *m_at;
};

wysota
5th January 2009, 21:53
Wow... what a C-ish code :) What do you use threads for? You don't need them if all you want to do is copy some data.