Results 1 to 7 of 7

Thread: How to use progress bar

  1. #1
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to use progress bar

    my program takes lot of time to complete so i want a progress bar so that user can see how much percentage of program is complete ... please help .... .. if you want to see my code then you can see my code at http://www.qtcentre.org/threads/3044...475#post149475 Thanks in advance .

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to use progress bar

    please help
    With what?
    What is the question?
    What did you try?

    PLEASE DONT CROSS POST!
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use progress bar

    I know i have to put progress bar in void run and next to promo.start but tell me how do i update it my program take approx 6 hours to complete if infile is 25 mb so how do i update it.
    I have no idea bout that thats why i have posted this thread here because i m beginner progress ->update widget is not working so what do I suppose to do

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to use progress bar

    If something takes 6 hours to complete, it is better to move the function to a seperate thread.
    Use QtConcurrent for example.
    http://doc.qt.nokia.com/4.6/qtconcur...-main-cpp.html

  5. #5
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use progress bar

    well i m not very comfortable with threading so I thought to use QProgressDialoge while process is running
    but the problem is it is still running even if qprogress stops ... please help .... any one .....


    Qt Code:
    1. #include <QtGui>
    2. #include "promo.h"
    3.  
    4.  
    5.  
    6. promotif::promotif(QWidget *parent)
    7. {
    8.  
    9.  
    10. setupUi(this); // this sets up GUI
    11.  
    12. // signals/slots mechanism in action
    13. connect( pushButton_browse, SIGNAL( clicked() ), this, SLOT( getPath() ) );
    14. connect( pushButton_run, SIGNAL( clicked() ), this, SLOT( run() ) );
    15. connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( about() ) );
    16. connect(pushButton_cancel,SIGNAL(clicked()),this,SLOT(cancel()));
    17. }
    18.  
    19. QString path;
    20.  
    21. void promotif::getPath()
    22. {
    23.  
    24.  
    25. path = QFileDialog::getOpenFileName(
    26. this,
    27. "Choose a file to open",
    28. QString::null,
    29. QString::null);
    30. lineEdit->setText( path );
    31. }
    32.  
    33.  
    34. void promotif::run()
    35. {
    36.  
    37. QProcess *promo = new QProcess();
    38. QString str;
    39. QString combo;
    40.  
    41. dlg.setLabelText("Running ....");
    42.  
    43. dlg.setRange(0,0);
    44. dlg.setValue(0);
    45. connect(promo,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(stop));
    46. dlg.exec();
    47.  
    48.  
    49. promo->start("a.exe");
    50.  
    51. promo->waitForStarted();
    52.  
    53.  
    54. if(radioButton->isChecked())
    55. promo->write("1\n");
    56. if(radioButton_2->isChecked())
    57. promo->write("2\n");
    58. if(radioButton_3->isChecked())
    59. promo->write("3\n");
    60. if(radioButton_4->isChecked())
    61. promo->write("4\n");
    62. if(radioButton_5->isChecked())
    63. promo->write("5\n");
    64. if(radioButton_6->isChecked())
    65. promo->write("6\n");
    66. if(radioButton_7->isChecked())
    67. promo->write("7\n");
    68. if(radioButton_8->isChecked())
    69. promo->write("8\n");
    70. if(radioButton_9->isChecked())
    71. promo->write("9\n");
    72.  
    73. QString num = QVariant(spinBox1->value()).toString()+"\n";
    74. promo->write(num.toAscii().data());
    75. promo->write(path.toAscii().data());
    76.  
    77.  
    78. promo->closeWriteChannel();
    79.  
    80.  
    81. //dlg.setLabelText("complete...");
    82.  
    83. //promo.terminate();
    84.  
    85.  
    86. }
    To copy to clipboard, switch view to plain text mode 

    I read this thread also but no use ..

    http://www.qtcentre.org/threads/2446...sDialog+stucks
    Last edited by Ashwani; 7th July 2010 at 12:19.

  6. #6
    Join Date
    May 2010
    Posts
    11
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    ok I got the solution but for different code I m going to post the codes
    Qt Code:
    1. #include "type1.h"
    2. #include "ui_type1.h"
    3.  
    4. #include "type1.h"
    5. #include "ui_type1.h"
    6. #include <QtGui>
    7.  
    8. Type1::Type1(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::Type1)
    11. {
    12. ui->setupUi(this);
    13.  
    14. Qt::WindowFlags flags; // remove maximize button
    15. flags = Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint;
    16. setWindowFlags( flags );
    17. QDesktopWidget *desktop = QApplication::desktop();
    18.  
    19.  
    20. // To load your Application at center of screen
    21.  
    22. int screenWidth, width;
    23. int screenHeight, height;
    24. int x, y;
    25. QSize windowSize;
    26.  
    27. screenWidth = desktop->width(); // get width of screen
    28. screenHeight = desktop->height(); // get height of screen
    29.  
    30. windowSize = size(); // size of our application window
    31. width = windowSize.width();
    32. height = windowSize.height();
    33.  
    34. // little computations
    35. x = (screenWidth - width) / 2;
    36. y = (screenHeight - height) / 2;
    37. y -= 50;
    38.  
    39. // move window to desired coordinates
    40. move ( x, y );
    41.  
    42.  
    43. connect(ui->pushButton_browse,SIGNAL(clicked()),this,SLOT(getpath()));
    44. connect(ui->pushButton_submit,SIGNAL(clicked()),this,SLOT(start()));
    45. connect(ui->pushButton_help,SIGNAL(clicked()),this,SLOT(help()));
    46. connect(ui->pushButton_result,SIGNAL(clicked()),this,SLOT(result()));
    47. connect(ui->pushButton_open,SIGNAL(clicked()),this,SLOT(open()));
    48. }
    49.  
    50.  
    51. QString path;
    52. void Type1::getpath(){ //choose file 1
    53.  
    54. path = QFileDialog::getOpenFileName(
    55. this,
    56. "Choose query file",
    57. QString::null,
    58. QString::null);
    59. ui->lineEdit->setText(path);
    60. }
    61.  
    62. QString res;
    63. void Type1::result()
    64. {
    65. res = QFileDialog::getExistingDirectory(
    66. this,
    67. "open directory"),
    68. ui->lineEdit_2->setText(res);
    69. }
    70.  
    71.  
    72. mythread::mythread() : QThread()
    73. {
    74. moveToThread(this); //initiates thread
    75. }
    76.  
    77. void mythread::run()
    78. {
    79.  
    80. QTimer::singleShot(0, this, SLOT(doTheWork()));
    81.  
    82. exec();
    83.  
    84.  
    85. }
    86.  
    87.  
    88.  
    89. void mythread::doTheWork() // main program running in a thread
    90. {
    91. system("perl .type1/run.pl");
    92.  
    93. emit done("finished 1");
    94.  
    95. }
    96.  
    97.  
    98. void Type1::start() // main GUI with progress bar dialogue
    99. {
    100.  
    101. mythread *wrapper = new mythread(); // connecting thread and main gui
    102. connect(wrapper,SIGNAL(done(QString)),this,SLOT(wrapperDone(QString)),Qt::QueuedConnection);
    103. wrapper->start();
    104.  
    105.  
    106.  
    107. /*QTimer *timer1 = new QTimer();
    108.   connect(timer1, SIGNAL(timeout()),this, SLOT(check()));
    109.   timer1->start(2000);
    110. */
    111. dlg.setLabelText("please wait..");
    112. dlg.setRange(0,0);
    113.  
    114. dlg.show();
    115.  
    116.  
    117. QString mismatch;
    118. mismatch = ui->comboBox_mm->currentText().toAscii().data();
    119. QString energy;
    120. energy = ui->comboBox_energy->currentText().toAscii().data();
    121. QString svm;
    122. svm = ui->comboBox_svm->currentText().toAscii().data();
    123.  
    124.  
    125. QStringList lines; // writing to file "userinfo"
    126. QFile file (".type1/userinfo");
    127. QString line;
    128. line.append("path:");
    129. line.append(path.toAscii().data());
    130. line.append("\n");
    131. line.append(mismatch);
    132. line.append("\n");
    133. line.append(energy);
    134. line.append("\n");
    135. line.append("100\n");
    136. line.append(svm);
    137. line.append("\n");
    138. line.append("result:");
    139. line.append(res.toAscii().data());
    140. line.append("\n");
    141.  
    142. lines += line;
    143. file.open(QIODevice::ReadWrite | QIODevice::Text);
    144.  
    145. QTextStream stream (&file);
    146. for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
    147. stream << *it<<"\n";
    148. file.close();
    149.  
    150.  
    151.  
    152.  
    153.  
    154. }
    155.  
    156. void Type1::time()
    157. {
    158. QTimer *timer = new QTimer(this);
    159. connect(timer, SIGNAL(timeout()), this, SLOT(cancel()));
    160. timer->start(1000);
    161. }
    162.  
    163.  
    164. void Type1::wrapperDone(const QString &results)
    165. {
    166. // The wrapper is now done with its long, blocking
    167. // operation, and we didn't freeze the application.
    168.  
    169.  
    170. /
    171.  
    172. dlg.close();
    173. QMessageBox::information(this,"type1",
    174. "TAref proces over");
    175. qDebug() << "Here are your results:" << results;
    176. }
    177.  
    178.  
    179. void mythread::stop() // stop thread
    180. {
    181.  
    182. {
    183. if(currentThread() != this)
    184. {
    185.  
    186. QMetaObject::invokeMethod(this, "stop",
    187. Qt::QueuedConnection);
    188. }
    189. else
    190. {
    191. quit();
    192. }
    193. }
    194. }
    195.  
    196. void Type1::help() // help button in GUI this will open a new widget :)
    197. {
    198.  
    199. QWidget *win = new QWidget;
    200.  
    201. QLabel *label = new QLabel(this);
    202. label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    203. label->setText("\t\t\tTYPE1 \n\n1) Please upload the sequence file in which you want to find the target \t\n\n2) Choose the Energy cut off value which is required\nfor the RNA-hybrid program \n\n3)Choose the mismatch cutoff\n\n4) Choose the SVM model\n\n5)Press submit to start the job\n\n6) A dialoge box will apper which means your job is processing\n\n7) On complition of job another box will apperar stating 'Process over'");
    204.  
    205. QVBoxLayout *layout = new QVBoxLayout;
    206. layout->addWidget(label);
    207. win->setLayout(layout);
    208. win->show();
    209.  
    210. }
    211.  
    212.  
    213. void Type1::open()
    214. {
    215. QString file;
    216. file =res.toAscii().data();
    217.  
    218. if(!QDesktopServices::openUrl(QUrl::fromLocalFile(file)))
    219. qDebug() << "Fail to run the file";
    220.  
    221. }
    222. Type1::~Type1()
    223. {
    224. delete ui;
    225. }
    To copy to clipboard, switch view to plain text mode 

    Here is the header file
    Qt Code:
    1. #ifndef TYPE1_H
    2. #define TYPE1_H
    3.  
    4. #include <QMainWindow>
    5. #include <QThread>
    6. #include <QProgressDialog>
    7.  
    8. namespace Ui {
    9. class Type1;
    10. }
    11.  
    12. class Type1 : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17.  
    18. explicit Type1(QWidget *parent = 0);
    19. ~Type1();
    20.  
    21. public slots:
    22. void start();
    23. void getpath();
    24.  
    25. void help();
    26. void open();
    27. void result();
    28. void time();
    29. void cancel();
    30. void wrapperDone(const QString &results);
    31.  
    32.  
    33. private:
    34. Ui::Type1 *ui;
    35. };
    36.  
    37.  
    38. class mythread : public QThread{ // create new thread
    39. Q_OBJECT
    40. public:
    41. mythread();
    42. protected:
    43. void run();
    44.  
    45. signals:
    46. void done(const QString &results);
    47. private slots:
    48. void doTheWork();
    49. void stop();
    50. //void check();
    51.  
    52.  
    53. };
    54.  
    55. #endif // TYPE1_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 10th September 2010 at 15:15.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: How to use progress bar

    Instead of using the thread you could have stayed with the process approach and connected the finished() signal from QProcess to the progress dialog.
    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. The following user says thank you to wysota for this useful post:

    Ashwani (17th April 2011)

Similar Threads

  1. Marquee progress bar?
    By daviddoria in forum Newbie
    Replies: 2
    Last Post: 13th February 2010, 02:20
  2. Adding a Progress bar
    By srohit24 in forum Qt Programming
    Replies: 15
    Last Post: 1st June 2009, 09:52
  3. progress bar issues
    By shooogun in forum Qt Programming
    Replies: 4
    Last Post: 14th March 2008, 04:57
  4. Replies: 4
    Last Post: 11th March 2008, 11:44
  5. Reg - Progress bar
    By suresh in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2006, 15:11

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.