問題描述
我有一個多線程的 qt 應用程序.當我在 mainwindow.cpp 中做一些進程時,同時我想從其他線程更新 mainwindow.ui.
i have an multithreaded qt application. when i am doing some processes in mainwindow.cpp, at the same time, i want to update mainwindow.ui from other thread.
我有 mythread.h
i have mythread.h
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include <QThread>
#include "mainwindow.h"
class mythread : public QThread
{
public:
void run();
mythread( MainWindow* ana );
MainWindow* ana;
private:
};
#endif // MYTHREAD_H
mythread.cpp
mythread.cpp
mythread::mythread(MainWindow* a)
{
cout << "thread created" << endl;
ana = a;
}
void mythread::run()
{
QPixmap i1 (":/notes/pic/4mdodiyez.jpg");
QLabel *label = new QLabel();
label->setPixmap(i1);
ana->ui->horizontalLayout_4->addWidget(label);
}
但問題是,我無法到達 ana->ui->horizo??ntalLayout_4->addWidget(label);
but the problem is that, i cannot reach the ana->ui->horizontalLayout_4->addWidget(label);
我該怎么做?
推薦答案
但問題是,我無法到達ana->ui->horizo??ntalLayout_4->addWidget(label);
but the problem is that, i cannot reach the ana->ui->horizontalLayout_4->addWidget(label);
將您的 UI 修改放在主窗口的一個插槽中,并將線程信號連接到該插槽,很有可能它會起作用.我認為只有主線程可以訪問 Qt 中的 UI.因此,如果您想要 GUI 功能,它必須存在,并且只能從其他線程發出信號.
Put your UI modifications in a slot in your main window, and connect a thread signal to that slot, chances are it will work. I think only the main thread has access to the UI in Qt. Thus if you want GUI functionality, it must be there, and can be only signaled from other threads.
好的,這是一個簡單的例子.順便說一句,您的方案并不真正需要擴展 QThread
- 所以你最好不要這樣做,除非你真的必須這樣做.這就是為什么在這個例子中我將使用一個普通的 QThread
和一個基于 QObject
的工作線程,但是如果你子類化 QThread
,概念是一樣的:
OK, here is a simple example. BTW, your scenario doesn't really require to extend QThread
- so you are better off not doing it, unless you really have to. That is why in this example I will use a normal QThread
with a QObject
based worker instead, but the concept is the same if you subclass QThread
:
主界面:
class MainUI : public QWidget
{
Q_OBJECT
public:
explicit MainUI(QWidget *parent = 0): QWidget(parent) {
layout = new QHBoxLayout(this);
setLayout(layout);
QThread *thread = new QThread(this);
GUIUpdater *updater = new GUIUpdater();
updater->moveToThread(thread);
connect(updater, SIGNAL(requestNewLabel(QString)), this, SLOT(createLabel(QString)));
connect(thread, SIGNAL(destroyed()), updater, SLOT(deleteLater()));
updater->newLabel("h:/test.png");
}
public slots:
void createLabel(const QString &imgSource) {
QPixmap i1(imgSource);
QLabel *label = new QLabel(this);
label->setPixmap(i1);
layout->addWidget(label);
}
private:
QHBoxLayout *layout;
};
... 和工作對象:
class GUIUpdater : public QObject {
Q_OBJECT
public:
explicit GUIUpdater(QObject *parent = 0) : QObject(parent) {}
void newLabel(const QString &image) { emit requestNewLabel(image); }
signals:
void requestNewLabel(const QString &);
};
worker 對象被創建并移動到另一個線程,然后連接到創建標簽的槽,然后調用它的 newLabel
方法,它只是一個包裝器來發出 requestNewLabel
信號并將路徑傳遞給圖像.然后將信號從工作對象/線程與圖像路徑參數一起傳遞到主 UI 插槽,并將新標簽添加到布局中.
The worker object is created and moved to another thread, then connected to the slot that creates the labels, then its newLabel
method is invoked, which is just a wrapper to emit the requestNewLabel
signal and pass the path to the image. The signal is then passed from the worker object/thread to the main UI slot along with the image path parameter and a new label is added to the layout.
由于工作對象是在沒有父對象的情況下創建的,以便能夠將其移動到另一個線程,因此我們還將線程銷毀信號連接到工作對象 deleteLater()
槽.
Since the worker object is created without parent in order to be able to move it to another thread, we also connect the thread destroyed signal to the worker deleteLater()
slot.
這篇關于Qt - 用第二個線程更新主窗口的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!