Results 1 to 3 of 3

Thread: Use of QProgressBar in Thread

  1. #1
    Join Date
    Jan 2007
    Posts
    326
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Use of QProgressBar in Thread

    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,

    Qt Code:
    1. void ClassWrite::fnWriteFile(FILE *fp, int nFillWith)
    2. {
    3. unsigned long value = 0;
    4. int iProgressCounter = 0;
    5. long long llSizeRead = 0;
    6. unsigned long SizetoRead =0;
    7. long long SizeRead = 0;
    8. QFileInfo filename(m_FileName);
    9. llSizeRead = filename.size();
    10. QString strMessage = "";
    11. unsigned char *pBuffer = NULL;
    12. unsigned long dwBufferValue = 4*1024*1024;
    13. pBuffer = (unsigned char *)calloc(dwBufferValue,sizeof(unsigned char));
    14. if(pBuffer == NULL)
    15. return;
    16.  
    17. strMessage = "WritingFile " + m_FileName;
    18. emit ProgressMaximumValue(100);
    19. qApp->processEvents();
    20.  
    21. while(llSizeRead>0)
    22. {
    23. if(bStopFlag)
    24. break;
    25.  
    26.  
    27. if(llSizeRead>dwBufferValue)
    28. {
    29. SizetoRead = dwBufferValue;
    30. value = ((SizetoRead/llSizeRead)*100);
    31. if(iProgressCounter == 0 || iProgressCounter == 100)
    32. {
    33. emit SetProgressValue(strMessage,value);
    34. qApp->processEvents();
    35. memset(pBuffer,nFillWith,SizetoRead);
    36. fseek(fp, 0+SizeRead, SEEK_CUR);
    37. fwrite((void*)pBuffer,SizetoRead,1,fp);
    38. llSizeRead-=SizetoRead;
    39. SizeRead = SizetoRead;
    40. iProgressCounter = 0;
    41. }
    42. iProgressCounter++;
    43. }
    44. else
    45. {
    46. SizetoRead = llSizeRead;
    47. value = ((SizetoRead/llSizeRead)*100);
    48. if(iProgressCounter == 0 || iProgressCounter == 100)
    49. {
    50. emit SetProgressValue(strMessage,value);
    51. qApp->processEvents();
    52. memset(pBuffer,nFillWith,SizetoRead);
    53. fseek(fp, 0+SizeRead, SEEK_CUR);
    54. fwrite((void*)pBuffer,SizetoRead,1,fp);
    55. llSizeRead-=SizetoRead;
    56. SizeRead = SizetoRead;
    57. iProgressCounter = 0;
    58. }
    59. iProgressCounter++;
    60. }
    61.  
    62.  
    63. }
    64.  
    65. fflush(fp);
    66. if(pBuffer)
    67. {
    68. free(pBuffer);
    69. pBuffer = NULL;
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 

    Pls suggest me something
    Always Believe in Urself
    Merry

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Use of QProgressBar in Thread

    try something like this
    Qt Code:
    1. class AnotherThread: public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. signals:
    6. void valueChanged(int);
    7.  
    8. public:
    9. AnotherThread(QObject *parent, int val)
    10. : QThread(parent),
    11. m_currentVal(val)
    12. {
    13. }
    14.  
    15. protected:
    16. virtual void run() {
    17. for (int i = 0; i < m_currentVal; ++i)
    18. emit valueChanged(i);
    19. }
    20.  
    21. private:
    22. int m_currentVal;
    23. };
    24.  
    25. class MainWindow:public QMainWindow
    26. {
    27. Q_OBJECT
    28.  
    29. public:
    30. MainWindow::MainWindow(QWidget *parent = 0)
    31. : QMainWindow(parent)
    32. {
    33. QHBoxLayout *mainLayout = new QHBoxLayout();
    34.  
    35. m_pbProgress = new QProgressBar();
    36. m_pbProgress->setMinimum(0);
    37. m_pbProgress->setMaximum(100);
    38.  
    39. m_at = new AnotherThread(this, m_pbProgress->maximum());
    40. connect(m_at, SIGNAL(valueChanged(int)), m_pbProgress, SLOT(setValue(int)));
    41.  
    42. QPushButton *pbRun = new QPushButton("Run");
    43. connect(pbRun, SIGNAL(clicked()), m_at, SLOT(start()));
    44.  
    45. mainLayout->addWidget(m_pbProgress);
    46. mainLayout->addWidget(pbRun);
    47.  
    48. QWidget *central = new QWidget();
    49. central->setLayout(mainLayout);
    50. setCentralWidget(central);
    51. }
    52.  
    53. private:
    54. QProgressBar *m_pbProgress;
    55. AnotherThread *m_at;
    56. };
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    merry (7th January 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use of QProgressBar in Thread

    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.

  5. The following user says thank you to wysota for this useful post:

    merry (7th January 2009)

Similar Threads

  1. QT + OpenGL + Thread => aaaahhhhh !
    By anthibug in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2008, 13:36
  2. Replies: 5
    Last Post: 17th January 2008, 21:49
  3. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  5. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.