PDA

View Full Version : How to use progress bar



Ashwani
1st July 2010, 13:57
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/30440-Running-External-c-program-when-user-hit-ok-button?p=149475#post149475 Thanks in advance .

high_flyer
1st July 2010, 14:19
please help
With what?
What is the question?
What did you try?

PLEASE DONT CROSS POST! :mad:

Ashwani
2nd July 2010, 05:10
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

tbscope
2nd July 2010, 05:42
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/qtconcurrent-progressdialog-main-cpp.html

Ashwani
7th July 2010, 12:11
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 .....



#include <QtGui>
#include "promo.h"



promotif::promotif(QWidget *parent)
{


setupUi(this); // this sets up GUI

// signals/slots mechanism in action
connect( pushButton_browse, SIGNAL( clicked() ), this, SLOT( getPath() ) );
connect( pushButton_run, SIGNAL( clicked() ), this, SLOT( run() ) );
connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( about() ) );
connect(pushButton_cancel,SIGNAL(clicked()),this,S LOT(cancel()));
}

QString path;

void promotif::getPath()
{


path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null);
lineEdit->setText( path );
}


void promotif::run()
{

QProcess *promo = new QProcess();
QString str;
QString combo;

QProgressDialog dlg ;
dlg.setLabelText("Running ....");

dlg.setRange(0,0);
dlg.setValue(0);
connect(promo,SIGNAL(finished(int,QProcess::ExitSt atus)),this,SLOT(stop));
dlg.exec();


promo->start("a.exe");

promo->waitForStarted();


if(radioButton->isChecked())
promo->write("1\n");
if(radioButton_2->isChecked())
promo->write("2\n");
if(radioButton_3->isChecked())
promo->write("3\n");
if(radioButton_4->isChecked())
promo->write("4\n");
if(radioButton_5->isChecked())
promo->write("5\n");
if(radioButton_6->isChecked())
promo->write("6\n");
if(radioButton_7->isChecked())
promo->write("7\n");
if(radioButton_8->isChecked())
promo->write("8\n");
if(radioButton_9->isChecked())
promo->write("9\n");

QString num = QVariant(spinBox1->value()).toString()+"\n";
promo->write(num.toAscii().data());
promo->write(path.toAscii().data());


promo->closeWriteChannel();


//dlg.setLabelText("complete...");

//promo.terminate();


}


I read this thread also but no use ..

http://www.qtcentre.org/threads/24468-QProgressDialog-stucks?highlight=QProgressDialog+stucks

Ashwani
10th September 2010, 15:03
ok I got the solution but for different code I m going to post the codes :)

#include "type1.h"
#include "ui_type1.h"

#include "type1.h"
#include "ui_type1.h"
#include <QtGui>

Type1::Type1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Type1)
{
ui->setupUi(this);

Qt::WindowFlags flags; // remove maximize button
flags = Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint;
setWindowFlags( flags );
QDesktopWidget *desktop = QApplication::desktop();


// To load your Application at center of screen

int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;

screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->height(); // get height of screen

windowSize = size(); // size of our application window
width = windowSize.width();
height = windowSize.height();

// little computations
x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;

// move window to desired coordinates
move ( x, y );


connect(ui->pushButton_browse,SIGNAL(clicked()),this,SLOT(getp ath()));
connect(ui->pushButton_submit,SIGNAL(clicked()),this,SLOT(star t()));
connect(ui->pushButton_help,SIGNAL(clicked()),this,SLOT(help() ));
connect(ui->pushButton_result,SIGNAL(clicked()),this,SLOT(resu lt()));
connect(ui->pushButton_open,SIGNAL(clicked()),this,SLOT(open() ));
}


QString path;
void Type1::getpath(){ //choose file 1

path = QFileDialog::getOpenFileName(
this,
"Choose query file",
QString::null,
QString::null);
ui->lineEdit->setText(path);
}

QString res;
void Type1::result()
{
res = QFileDialog::getExistingDirectory(
this,
"open directory"),
ui->lineEdit_2->setText(res);
}


mythread::mythread() : QThread()
{
moveToThread(this); //initiates thread
}

void mythread::run()
{

QTimer::singleShot(0, this, SLOT(doTheWork()));

exec();


}



void mythread::doTheWork() // main program running in a thread
{
system("perl .type1/run.pl");

emit done("finished 1");

}


void Type1::start() // main GUI with progress bar dialogue
{

mythread *wrapper = new mythread(); // connecting thread and main gui
connect(wrapper,SIGNAL(done(QString)),this,SLOT(wr apperDone(QString)),Qt::QueuedConnection);
wrapper->start();



/*QTimer *timer1 = new QTimer();
connect(timer1, SIGNAL(timeout()),this, SLOT(check()));
timer1->start(2000);
*/
dlg.setLabelText("please wait..");
dlg.setRange(0,0);

dlg.show();


QString mismatch;
mismatch = ui->comboBox_mm->currentText().toAscii().data();
QString energy;
energy = ui->comboBox_energy->currentText().toAscii().data();
QString svm;
svm = ui->comboBox_svm->currentText().toAscii().data();


QStringList lines; // writing to file "userinfo"
QFile file (".type1/userinfo");
QString line;
line.append("path:");
line.append(path.toAscii().data());
line.append("\n");
line.append(mismatch);
line.append("\n");
line.append(energy);
line.append("\n");
line.append("100\n");
line.append(svm);
line.append("\n");
line.append("result:");
line.append(res.toAscii().data());
line.append("\n");

lines += line;
file.open(QIODevice::ReadWrite | QIODevice::Text);

QTextStream stream (&file);
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
stream << *it<<"\n";
file.close();





}

void Type1::time()
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(cancel()));
timer->start(1000);
}


void Type1::wrapperDone(const QString &results)
{
// The wrapper is now done with its long, blocking
// operation, and we didn't freeze the application.


/

dlg.close();
QMessageBox::information(this,"type1",
"TAref proces over");
qDebug() << "Here are your results:" << results;
}


void mythread::stop() // stop thread
{

{
if(currentThread() != this)
{

QMetaObject::invokeMethod(this, "stop",
Qt::QueuedConnection);
}
else
{
quit();
}
}
}

void Type1::help() // help button in GUI this will open a new widget :)
{

QWidget *win = new QWidget;

QLabel *label = new QLabel(this);
label->setFrameStyle(QFrame::Panel | QFrame::Sunken);
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'");

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(label);
win->setLayout(layout);
win->show();

}


void Type1::open()
{
QString file;
file =res.toAscii().data();

if(!QDesktopServices::openUrl(QUrl::fromLocalFile( file)))
qDebug() << "Fail to run the file";

}
Type1::~Type1()
{
delete ui;
}

Here is the header file

#ifndef TYPE1_H
#define TYPE1_H

#include <QMainWindow>
#include <QThread>
#include <QProgressDialog>

namespace Ui {
class Type1;
}

class Type1 : public QMainWindow
{
Q_OBJECT

public:

QProgressDialog dlg;
explicit Type1(QWidget *parent = 0);
~Type1();

public slots:
void start();
void getpath();

void help();
void open();
void result();
void time();
void cancel();
void wrapperDone(const QString &results);


private:
Ui::Type1 *ui;
};


class mythread : public QThread{ // create new thread
Q_OBJECT
public:
mythread();
protected:
void run();

signals:
void done(const QString &results);
private slots:
void doTheWork();
void stop();
//void check();


};

#endif // TYPE1_H

wysota
10th September 2010, 15:19
Instead of using the thread you could have stayed with the process approach and connected the finished() signal from QProcess to the progress dialog.