Using signal slots mechanism for displaying a transient “generating image†dialog
want to display a "generating image..." kind of modal dialog, other than the main GUI. This "generating image..." dialog should be temporary, and be displayed and disappear without user intervention.
For displaying this dialog, the Qt code should check for existence of a .txt file in a specific location in the PC's hard disk. If the .txt file exists, then the dialog should pop-up.
For making this dialog disappear, the Qt code should check whether that .txt file contains the string "OK" in the first line. The dialog should disappear only when this "OK" is found, until then it should continue to display "generating image..."
A good way to do this is to use signal slot mechanism. I would like to know, what functions should be used as SIGNALS in both the cases, of displaying and removing the dialog.
So far, I could manage a simple code, illustrating a "generating image..." using signal slot mechanism, but with setValue() and pressing a push button(i.e. involving user intervention), and not with the checking of .txt file or the "OK" string inside that .txt file(user non-intervention).
Please advise me, whether my logic can be implemented? If yes, how? Also, what SIGNALs should be used?
Here is my code:
dialog.h
Code:
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QProgressDialog>
#include "mytask.h"
namespace Ui {
class Dialog;
}
{
Q_OBJECT
public:
explicit Dialog
(QWidget *parent
= 0);
~Dialog();
private slots:
void on_modalButton_clicked();
private:
Ui::Dialog *ui;
MyTask *myTask;
};
#endif // DIALOG_H
mytask.h
Code:
#ifndef MYTASK_H
#define MYTASK_H
#include <QObject>
#include <QProgressDialog>
#include <QTimer>
#include <QDebug>
#define NUM_TASKS 50000
{
Q_OBJECT
public:
explicit MyTask
(QObject *parent
= 0);
signals:
public slots:
void perform();
void cancel();
private:
int steps;
};
#endif // MYTASK_H
dialog.cpp
Code:
#include "dialog.h"
#include "ui_dialog.h"
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_modalButton_clicked()
{
myTask = new MyTask;
}
mytask.cpp
Code:
#include "mytask.h"
{
connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
connect(t, SIGNAL(timeout()), this, SLOT(perform()));
t->start(0);
/* to make a window modal in nature. */
pd->setModal(true);
}
void MyTask::perform()
{
disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
pd->setValue(steps); //... perform one percent of the operation
steps++;
qDebug()<<steps;
if (steps > pd->maximum())
{
t->stop();
}
}
void MyTask::cancel()
{
t->stop(); //... cleanup
}
main.cpp
Code:
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
Dialog w;
w.show();
return a.exec();
}
Re: Using signal slots mechanism for displaying a transient “generating image†dialog
Quote:
For making this dialog disappear, the Qt code should check whether that .txt file contains the string "OK" in the first line
You can use QFileSystemWatcher for that (fileChanged(QString) signal).
Quote:
The dialog should disappear only when this "OK" is found, until then it should continue to display "generating image..."
I'd use some kind of timeout too, especially if this "OK" is written by some other process. Btw. check if there is no "OK" already before displaying the dialog ;)
Also, I don't really understand this line:
Code:
void MyTask::perform()
{
disconnect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
...
}
Why do you keep disconnecting the signal over and over on each timer tick ?