久久久久久久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 排序功能對列表進行排序)
主站蜘蛛池模板: 91超碰在线观看 | 成人永久免费 | 日韩精品网站 | 一级黄毛片 | 视频一二三区 | 99久久精品国产毛片 | 欧美色综合 | 欧美视频区 | 黄色午夜 | 欧美不卡视频 | 日韩av手机在线 | 欧美在线a| 中国一级毛片 | 欧美日韩四区 | 日韩一区二区三区在线 | 午夜精品在线观看 | 四虎视频 | 欧美理论在线观看 | 日本成人一区二区 | 国产强伦人妻毛片 | 在线播放国产精品 | 欧美一区二区三区视频 | 性色av一区二区 | 国内精品一区二区 | 久久只有精品 | 精品一区二区三区免费 | 深夜久久| 日产久久视频 | 国产69精品久久久久久 | 一区视频| 中文字幕理论片 | 久草福利在线 | 成人小视频在线 | 天天射日日干 | 国产永久在线 | 天天cao| 三年中文在线观看免费大全中国 | 国产在线观看一区二区三区 | 一级黄色免费看 | 精品国产一二三 | 黄色录像免费看 |