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

问题描述 投票:0回答:1

我读了一些主题,现在我知道我必须使用

Signals
Slots
,但我在做这件事时遇到了一些麻烦。 在我的用户界面上,我有一个
Q List Widget
,我需要从另一个线程向其中添加项目;我在我的
tela_chat.cpp
中使用以下代码,该代码对应于插入我的
QList
的屏幕:

//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++ multithreading oop qt-creator qt-signals
1个回答
0
投票

在您的构造函数中,您将 call_chatReceived (信号)连接到 receiveChat (槽),但您的信号被定义为 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 {
        void run() override {
            QString mensagem = "This is what I want to put into QList";
            emit call_receivedChat(mensagem);
        }
    };

    // Initializing the new thread
    ThreadChat *thread = new ThreadChat;
    thread->start(); // Starts the thread (calling run())
}
© www.soinside.com 2019 - 2024. All rights reserved.