Control MainWindow from QThread
Hi everybody,
I would like to know the best way to control/modify elements of my MainWindow class from a QThread class started from the mainwindow.
In particular I only want to append text to the textEdit on the mainwindow and modify the progress bar value, during the QThread calculation.
I tried the heritage method but it doesn't works.
Ex. of my app:
Code:
#ifndef HRSMAINWINDOW_H
#define HRSMAINWINDOW_H
#include <QtGui/QMainWindow>
namespace Ui
{
class HRSMainWindowForm;
}
{
Q_OBJECT
public:
HRSMainWindow
(QWidget *parent
= 0);
~HRSMainWindow();
void APT
(QString a
){ //append text to the textEdit ui->textEdit->append(a);
}
private:
Ui::HRSMainWindowForm *ui;
};
#endif // HRSMAINWINDOW_H
Code:
#ifndef HRSTHREAD_H
#define HRSTHREAD_H
#include <QThread>
#include "hrsmainwindow.h"
class HRSThread
: public QThread,
public HRSMainWindow
{
public:
void run(){
APT("Hello World"); // don`t work
}
};
#endif // HRSTHREAD_H
Thank you very much
Re: Control MainWindow from QThread
sorry buddy, but u cant perform any GUI operation through a QThread in QT...
Re: Control MainWindow from QThread
You still can emit signals from a running thread, that, connected to slots in MainWindow would modify the textEdit. You can set the thread class to be a member of MainWindow if you like, but the best solution would be having the thread and MainWindow as two separate classes. The connection can be made in main.cpp.
Re: Control MainWindow from QThread
read thread support in Qt and blocking fortune client example .. its easier to use threads eventhough we cant use QWidget .. QWidgets are not reentrant but we can connect between the main thread and worker thread
Re: Control MainWindow from QThread
Hi everybody,
Thank you very much, know I understand what I have to do, the Blocking Client example is absolutely perfect!!