如何在 QT Creator 上从外部线程调用 GUI 函数?

How may I call a GUI function from an external thread on QT Creator?

提问人:kuybt6 提问时间:10/29/2023 更新时间:10/29/2023 访问量:49

问:

我读了一些主题,现在我知道我必须使用 和 ,但我在做的时候遇到了一些麻烦。 在我的用户界面上,我有一个,我需要从另一个线程向它添加项目;我在 my 中使用以下代码,该代码对应于插入 my 的屏幕:SignalsSlotsQ List Widgettela_chat.cppQList

//tela_chat.cpp
#include <QThread>
#incluede "tela_chat.h"

tela_chat::tela_chat(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::tela_chat)
{
    ui->setupUi(this);
    connect(this, SIGNAL(call_chatReceived(QString)), this, SLOT(receivedChat(QString))); //Connect signal to slot function into the constructor of the class
}


    tela_chat::~tela_chat()
    {
        delete ui;
    }
    
    void tela_chat::receivedChat(QString mensagem)
    {
        //The signal has been received
        ui->lstWdgt_chat->addItem("Professor: " + mensagem); //Add item to QList
    }
    
    void tela_chat::on_btn_conectar_clicked()
    {
    
        //Creating an other thread
    
        class ThreadChat : public QThread {
            void run() override {
    
                //Cóigo para rodar na thread separada:
                    QString mensagem = "This is what I want to put into QList";
                    emit call_receivedChat(mensagem);
            }
        };
    
    
        //INICIALIZATING THE NEW THREAD
    
        ThreadChat *thread = new ThreadChat;
        thread -> start(); //Inicia a thread (chamando o run())
        //thread -> wait(); 
    
               
    }

这是链接的头文件:

//tela_chat.h
#include <QString>

namespace Ui {
class tela_chat;
}

class tela_chat : public QFrame
{
    Q_OBJECT

public:
    explicit tela_chat(QWidget *parent = nullptr);
    ~tela_chat();

private slots:  
    void receivedChat(QString mensagem);
    void on_btn_conectar_clicked();

private:
    Ui::tela_chat *ui;
    
signals:
    void call_receivedChat(QString mensagem);
};

还有一些其他函数,如套接字连接脚本,但我删除了它,使代码成为可重现代码的最小数量。

C++ 多线程 OOP QT-CREATOR QT 信号

评论

0赞 Hans Passant 10/29/2023
doc.qt.io/qt-6/qcoreapplication.html#postEvent

答:

1赞 Cem Polat 10/29/2023 #1

在构造函数中,将 call_chatReceived(信号)连接到 receivedChat(插槽),但信号被定义为call_receivedChat。确保信号和插槽名称匹配。 此外,您应该在 tela_chat 构造函数中连接信号,而不是在方法中连接信号。连接应位于构造函数中,以便在创建对象时建立一次连接。

下面是更正后的代码:

// tela_chat.cpp
#include <QThread>
#include "tela_chat.h"

tela_chat::tela_chat(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::tela_chat)
{
    ui->setupUi(this);

    // Connect the signal and slot in the constructor
    connect(this, SIGNAL(call_receivedChat(QString)), this, SLOT(receivedChat(QString));
}

void tela_chat::receivedChat(QString mensagem)
{
    // The signal has been received
    ui->lstWdgt_chat->addItem("Professor: " + mensagem); // Add item to QList
}

void tela_chat::on_btn_conectar_clicked() {
// Creating another thread
class ThreadChat : public QThread {
public:
    ThreadChat(tela_chat* chatInstance) : chatInstance(chatInstance) {}

    void run() override {
        QString mensagem = "This is what I want to put into QList";
        emit chatInstance->call_receivedChat(mensagem);
    }

private:
    tela_chat* chatInstance;
};

// Initializing the new thread
ThreadChat* thread = new ThreadChat(this); // Pass the instance of tela_chat
thread->start(); // Starts the thread (calling run())

}

通过将 tela_chat 实例传递给 ThreadChat 构造函数,可以使用它来发出信号。这可确保在从嵌套类发出信号时有一个有效的实例可供使用。

评论

0赞 kuybt6 10/29/2023
好的,我试试看!
0赞 kuybt6 10/29/2023
我修复了我的语义错误,并在行“”处收到错误“”,但每件事都已正确声明。tela_chat.cpp:132:22: error: call to non-static member function 'call_receivedChat' of 'tela_chat' from nested type 'ThreadChat'emit call_receivedChat(mensagem);
1赞 Cem Polat 10/29/2023
İ 对代码进行了更新以传递实例。