Hi everyone,
I'm implementing an application that shows the status of the initialization process in a buttonless window, just like the one in the figure.
Attachment 7971
This window must show different messages depending on the initialization step.
I've created a "secondary thread" which performs the actual configuration tasks, whereas the "main thread" shows the info window, implemented as a buttonless dialog box. During configuration process, the "secondary thread" can modify the "Text" field of the dialog box and so update the info shown by the info window. However, at the end of the work, I'm not able to close the window, as it is buttonless. Obviously, I've tried to "close()" the dialog, but it gets a Runtime error:
Attachment 7970
How can I get the "secondary thread" closes the window? Or how can I implement the same idea in another way?
These are the codes of both the main and secondary threads.
Main thread:
void VFITW::ConfigureTool()
{
if (Initialized)
return ;
// Create information window
info.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
info.setWindowTitle("Info");
info.setText(tr("Initializing tool."));
info.setInformativeText(tr("This operation can last some time. Please wait ..."));
// Create secondary thread
FormActiveThread *thread = new FormActiveThread(&info, ..., 0);
// connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
// Launch information window
info.exec();
// info.~QDialog();
// Wait for the end of secondary thread
// thread->wait();
}
void VFITW::ConfigureTool()
{
if (Initialized)
return ;
// Create information window
QMessageBox info(0);
info.setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint);
info.setWindowTitle("Info");
info.setIcon(QMessageBox::Information);
info.setStandardButtons(QMessageBox::NoButton);
info.setText(tr("Initializing tool."));
info.setInformativeText(tr("This operation can last some time. Please wait ..."));
// Create secondary thread
FormActiveThread *thread = new FormActiveThread(&info, ..., 0);
// connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
// Launch information window
info.exec();
// info.~QDialog();
// Wait for the end of secondary thread
// thread->wait();
}
To copy to clipboard, switch view to plain text mode
Secondary thread (header):
#ifndef FORMACTIVETHREAD_H
#define FORMACTIVETHREAD_H
#include <QObject>
#include <QThread>
#include <QMessageBox>
class FormActiveThread
: public QThread{
Q_OBJECT
public:
// I pass a pointer to the info window to let the "secondary thread" update the information shown.
~FormActiveThread();
protected:
void run();
private:
...
};
#endif // FORMACTIVETHREAD_H
#ifndef FORMACTIVETHREAD_H
#define FORMACTIVETHREAD_H
#include <QObject>
#include <QThread>
#include <QMessageBox>
class FormActiveThread : public QThread
{
Q_OBJECT
public:
// I pass a pointer to the info window to let the "secondary thread" update the information shown.
FormActiveThread(QMessageBox *info, ..., QObject *parent = 0);
~FormActiveThread();
protected:
void run();
private:
QMessageBox *infoT;
...
};
#endif // FORMACTIVETHREAD_H
To copy to clipboard, switch view to plain text mode
Secondary thread (source code):
#include "FormActiveThreads.h"
{
infoT = info;
...
}
FormActiveThread::~FormActiveThread()
{
infoT = NULL;
...
}
void FormActiveThread::run()
{
// Initialize application parameters
...
// Initialize connections
(*infoT).setText(tr("Initializing connections."));
(*infoT).repaint();
...
// (*infoT).close();
}
#include "FormActiveThreads.h"
FormActiveThread::FormActiveThread(QMessageBox *info, ..., QObject *parent)
: QThread(parent)
{
infoT = info;
...
}
FormActiveThread::~FormActiveThread()
{
infoT = NULL;
...
}
void FormActiveThread::run()
{
// Initialize application parameters
...
// Initialize connections
(*infoT).setText(tr("Initializing connections."));
(*infoT).repaint();
...
// (*infoT).close();
}
To copy to clipboard, switch view to plain text mode
Besides this, I have additional problems:
1.- How can I get that the the dialog box expands as to have both Text and InformativeText in one line (see that InformativeText occupies two lis) as I've got in other dilog boxes that I've used?
2.- I can't make the "seconday thread" to self-delete. If I uncomment this sentence in the main thread:
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
To copy to clipboard, switch view to plain text mode
I get a Runtime error. Why?
3.- I can't wait for the end of secondary thread, because if I uncomment the sentence in the main thread:
thread->wait();
thread->wait();
To copy to clipboard, switch view to plain text mode
I get a Runtime error. Why?
Thank you very much in advance.
Bookmarks