PDA

View Full Version : Control MainWindow from QThread



lixo1
15th February 2009, 20:34
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:


#ifndef HRSMAINWINDOW_H
#define HRSMAINWINDOW_H

#include <QtGui/QMainWindow>

namespace Ui
{
class HRSMainWindowForm;
}

class HRSMainWindow : public QMainWindow
{
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



#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

talk2amulya
15th February 2009, 20:51
sorry buddy, but u cant perform any GUI operation through a QThread in QT...

ayoy
15th February 2009, 21:35
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.

wagmare
16th February 2009, 05:54
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

lixo1
16th February 2009, 10:33
Hi everybody,

Thank you very much, know I understand what I have to do, the Blocking Client example is absolutely perfect!!