久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Qt - 用第二個線程更新主窗口

Qt - updating main window with second thread(Qt - 用第二個線程更新主窗口)
本文介紹了Qt - 用第二個線程更新主窗口的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個多線程的 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What is the fastest way to transpose a matrix in C++?(在 C++ 中轉置矩陣的最快方法是什么?)
Sorting zipped (locked) containers in C++ using boost or the STL(使用 boost 或 STL 在 C++ 中對壓縮(鎖定)容器進行排序)
Rotating a point about another point (2D)(圍繞另一個點旋轉一個點 (2D))
Image Processing: Algorithm Improvement for #39;Coca-Cola Can#39; Recognition(圖像處理:Coca-Cola Can 識別的算法改進)
How do I construct an ISO 8601 datetime in C++?(如何在 C++ 中構建 ISO 8601 日期時間?)
Sort list using STL sort function(使用 STL 排序功能對列表進行排序)
主站蜘蛛池模板: 成人黄在线观看 | 国产精品久久久久久av公交车 | 夜夜骑首页 | 黄色av大片 | 91久久久久久久久 | 日韩淫片免费看 | 中文字幕av在线 | 亚洲免费一区二区 | 欧美一级欧美一级在线播放 | 国产极品粉嫩美女呻吟在线看人 | 久久久精选 | 手机av免费在线 | 国际精品鲁一鲁一区二区小说 | 亚洲成人免费视频 | 免费成人午夜 | 欧美男人天堂 | 久久大陆 | 韩国av电影网 | 日韩欧美视频在线 | 日韩欧美国产精品综合嫩v 一区中文字幕 | 日本人麻豆 | 国产一区二区三区亚洲 | 黄色免费av | 九九99靖品 | 免费午夜视频在线观看 | 91久久久久久久久久久久久 | 欧美中文字幕 | av日韩精品 | 国产99久久| 国产精成人 | 武道仙尊动漫在线观看 | 亚洲福利在线视频 | 免费黄色录像片 | 亚洲成人福利在线观看 | 亚洲精品乱码久久久久久按摩 | 久久久国产一区二区三区 | 久草色播 | 午夜资源 | 精品亚洲视频在线 | 91精品久久久久久久久久入口 | 99只有精品 |