One signal to two window
-
Hello,
Please give me a simple example code for the following:
I have a "working thread" which has a variable.
I would like to display this variable on two different GUI window.
How can i do it?
It is working for me with one window, but how can I use the same working thread signal to the two windows at the same time? -
It depends on the architecture of you app.
If it is a simple example that contains two windows then you can initialize everything in main() function()
start thread
create both windows
connect all necessary signals"Here":http://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/ is a good explanation on how to use QThread in general.
And a simple example
@
class WindowOne : public QWidget
{
...
public slots:
void updateValue(int);
...
};class WindowTwo : public QWidget
{
...
public slots:
void updateValue(int);
...
};class Worker : public QObject
{
...
signals:
void valueChanged(int);
};int main()
{
QApplication a(argc, argv);QThread* thread = new QThread; Worker* worker = new Worker; WindowOne* winOne = new WindowOne; WindowTwo* winTwo = new WindowTwo; connect(worker, &Worker::valueChanged, winOne, &WindowOne::updateValue); connect(worker, &Worker::valueChanged, winTwo, &WindowOne::updateValue); worker->moveToThread(thread); connect(thread, &QThread::started, worker, &Worker::process); connect(worker, &QThread::finished, thread, &QThread::quit); connect(worker, &QThread::finished, worker, &Worker::deleteLater); connect(thread, &QThread::finished, thread, &QThread::deleteLater); thread->start(); winOne->show(); winTwo->show(); // Start app event loop return a.exec();
}
@ -
How can I use "QPushButton::clicked() show second window" in the main, if I have an UI window with that pushbutton? (I can open the second window from the first window, but than I cant make the connection between the worker thread and the second window with the same signal what iI use for the first window in the worker thread (because I initialized the worker in the main)).
I tried to send a signal from that pushbutton, but I cant do to the main.
My program is same now what you give me in the example.
Thank You! -
Hello,
I have an other conection problem now.
I have a widget what I want to connect to my worker thread.
My thread is initialized in other class (Mainwindow).widget .h:
@class RoverSatView : public QWidget
{
Q_OBJECT
public:
RoverSatView(QWidget *parent = 0);protected:
void paintEvent(QPaintEvent *event);private slots:
void slot_rtk_rover_sat_azel_in(const SatList &sats);private: SatList satellites; rtk_thread worker;
};
widget .cpp:
RoverSatView::RoverSatView(QWidget *parent) : QWidget(parent)
{
connect(&worker,SIGNAL(rtk_rover_sat_azel(const SatList)),this,SLOT(slot_rtk_rover_sat_azel_in(const SatList)));
}@No error message, but not working. What is the misstake?
Of course I have emit signal in the worker thread. -
Yes, the SatList type is registered.
No message in the consol.
I have this in the class RoverSatView header:
@ private:
rtk_thread worker;
@@MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QThread* rtkthread=new QThread; rtk_thread* worker=new rtk_thread;
...
worker->moveToThread(rtkthread);
connect(rtkthread, SIGNAL(started()), worker, SLOT(run()));
connect(worker, SIGNAL(finished()), rtkthread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(rtkthread, SIGNAL(finished()), rtkthread, SLOT(deleteLater()));
rtkthread->start();}
@
@class RoverSatView : public QWidget
{
Q_OBJECT
public:
RoverSatView(QWidget *parent = 0);protected:
void paintEvent(QPaintEvent *event);private slots:
void slot_rtk_rover_sat_azel_in(const SatList &sats);private: SatList satellites; rtk_thread worker;
};@
-
then where you have the generation code you simply take that variable from there - from the constructor and connect it. just after it was constructed or generated