Results 1 to 7 of 7

Thread: QProgressBar "crash" - doesn't work properly

  1. #1
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default QProgressBar "crash" - doesn't work properly

    I'm usign a QProgressBar to know the state of a job that consist in read a file.
    My problem is if during that process i click in anything or open anothers aplications, my aplication act like it crashed and just show 100% when the process finish, like: 1%, 2%, i click, ------------------ 100%.
    If i just run and didn't do nothing in my pc the QProgressBar show the process correct, like, 1%, 2%, 3%, 4%, ..., 99%, 100%.

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QProgressBar>
    6.  
    7. class MainWindow : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow(QWidget *parent = 0);
    13. ~MainWindow();
    14.  
    15. public slots:
    16. void test();
    17.  
    18. private:
    19. QProgressBar *progress;
    20. };
    21.  
    22. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QDebug>
    3. #include <QString>
    4. #include <QDir>
    5. #include <QFileDialog>
    6. #include <QMessageBox>
    7. #include <QLabel>
    8. #include <QProgressBar>
    9. #include <QLayout>
    10. #include <QVBoxLayout>
    11. #include <QTimer>
    12. #include <QProgressDialog>
    13.  
    14. MainWindow::MainWindow(QWidget *parent)
    15. : QMainWindow(parent)
    16. {
    17.  
    18. QVBoxLayout *layout = new QVBoxLayout;
    19. progress = new QProgressBar;
    20. layout->addWidget(progress);
    21.  
    22. QWidget *w=new QWidget();
    23. w->setLayout(layout);
    24. this->setCentralWidget(w);
    25.  
    26. QTimer::singleShot(100, this, SLOT(test()));
    27. }
    28.  
    29. MainWindow::~MainWindow()
    30. {
    31.  
    32. }
    33.  
    34. void MainWindow::test(){
    35. QString dir = QFileDialog::getOpenFileName(this, tr("Select Text File"),"",tr("Text (*.txt)"));
    36. QFile f(dir);
    37. f.open(QIODevice::ReadOnly);
    38. if(f.isOpen())
    39. {
    40. int fileSize=f.size();
    41. int step=fileSize/100;
    42. int bytesProcessed=0;
    43. QString line;
    44. progress->setMaximum(fileSize);
    45. progress->setValue(bytesProcessed);
    46. while (not f.atEnd()){
    47. line = f.readLine().data();
    48. bytesProcessed+= line.size();
    49. list = line.split(":");
    50. if (bytesProcessed%step<=20){
    51. //qDebug()<<"bytesProcessed:"<<bytesProcessed;
    52. progress->setValue(bytesProcessed);
    53. }
    54. }
    55. progress->setValue(bytesProcessed);
    56.  
    57. }
    58. else
    59. {
    60. QMessageBox::warning(this,"Error","No file selected!");
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QProgressBar "crash" - doesn't work properly

    To be honest I'm suprised that if you don't do anything then it works correctly. You have a while loop that doesn't allow events to be processed so the progress bar shouldn't get updated. At least add a QCoreApplication::processEvents() call after each call to QProgressBar::setValue().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    metRo_ (23rd October 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QProgressBar "crash" - doesn't work properly

    It's work now even if i do another things Thanks

    To be honest I'm suprised that if you don't do anything then it works correctly.
    It's true, i can record a video if you want to see :s

  5. #4
    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: QProgressBar "crash" - doesn't work properly

    Quote Originally Posted by metRo_ View Post
    It's true
    Well, it shouldn't work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QProgressBar "crash" - doesn't work properly

    Anyway, this "QCoreApplication:rocessEvents()" is the best way to do what i want or just an easy way?

  7. #6
    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: QProgressBar "crash" - doesn't work properly

    It's an easy way. But for such a simple operation like this it is ok. If you start doing something besides reading from file then some other approach might be better. For this situation the only change I could advise would be to read the file using a thread so that the main part of the application can do other things while the data is being loaded.

    In general read this: [wiki]Keeping the GUI Responsive[/wiki].
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2010
    Posts
    56
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QProgressBar "crash" - doesn't work properly

    thanks, i'll read this.

Similar Threads

  1. Replies: 11
    Last Post: 30th March 2015, 06:03
  2. Replies: 21
    Last Post: 20th October 2010, 13:25
  3. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  4. Setting QFrame palette in Designer doesn't "stick"
    By Doug Broadwell in forum Qt Tools
    Replies: 1
    Last Post: 9th September 2007, 21:44
  5. Replies: 6
    Last Post: 3rd November 2006, 11:53

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.