簡體   English   中英

嘗試連接發射信號,但另一側沒有出現

[英]Trying to connect an emit signal, but nothing appears on the other side

因此,我正在學習如何使用QThread ,並且在合理程度上理解它。

但是,我正在嘗試實現從 QThread 派生的 class 來執行基本的 function,然后在值發生更改時發出信號。

讓我發布HeaderSource文件:

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

#include "mythread.h"

#include <QTextEdit>
#include <QPushButton>
#include <QSpinBox>
class Dialog : public QDialog
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = nullptr);
    ~Dialog();

    void setGUI();

    void Start_Thread();
    void Stop_Thread();  //will be implemented later to stop all threads

public slots:
    void Update_O1(int);

private:
    MyThread *m_Thread1;

    QPushButton *START;
    QPushButton *STOP;

    QSpinBox *L1Input;
    QSpinBox *L2Input; //For when a second Thread is added
    QSpinBox *L3Input; //For when a third Thread is added

    QTextEdit *L1Out;
    QTextEdit *L2Out; //For when a second Thread is added
    QTextEdit *L3Out; //For when a third Thread is added
};
#endif // DIALOG_H

dialog.cpp

#include "dialog.h"
#include <QGridLayout>

Dialog::Dialog(QWidget *parent)
    : QDialog(parent), m_Thread1(new MyThread), START(new QPushButton("Start")),
      STOP(new QPushButton("Stop")), L1Input(new QSpinBox), L2Input(new QSpinBox),
      L3Input(new QSpinBox),L1Out(new QTextEdit),L2Out(new QTextEdit), L3Out(new QTextEdit)
{
    setGUI();
    connect(START, &QPushButton::clicked, this, &Dialog::Start_Thread);
    connect(STOP, &QPushButton::clicked, this, &Dialog::Stop_Thread);

    connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int))); 
//I know this is the old Syntax, but I don't know how else to do this...yet.
//I got a similar function to work on a different project using the same format
}

Dialog::~Dialog()
{
}

void Dialog::setGUI()
{
    setWindowTitle("Three Threads");

    resize(300,300);

    START->setMaximumWidth(100);
    STOP->setMaximumWidth(100);

    L1Input->setRange(1,100);
    L2Input->setRange(1,100);
    L3Input->setRange(1,100);

    L1Out->setReadOnly(true);
    L2Out->setReadOnly(true);
    L3Out->setReadOnly(true);

    QGridLayout *GLout = new QGridLayout;
    GLout->addWidget(START,0,1);
    GLout->addWidget(STOP,0,2);

    GLout->addWidget(L1Input,1,0);
    GLout->addWidget(L2Input,1,1);
    GLout->addWidget(L3Input,1,2);

    GLout->addWidget(L1Out,2,0);
    GLout->addWidget(L2Out,2,1);
    GLout->addWidget(L3Out,2,2);

    setLayout(GLout);
}

void Dialog::Start_Thread()
{
    qDebug() << "START works";  //this is just to let me know the Start_Thread function is called properly
    m_Thread1->start();
}

void Dialog::Stop_Thread()
{
}

void Dialog::Update_O1(int x)
{
    qDebug() << QString::number(x);
}

mythread.h

#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>
#include <QObject>

class MyThread : public QThread
{
    Q_OBJECT
public:
    explicit MyThread(QObject *parent = nullptr);

    void run();

    void setSnum(int); //setter function

    bool Stop = false; //will be used later to stop a thread

signals:
    void NumberChanged(int);

private:
    int Snum = 10; //temporary value. will later implement setter to use value from GUI
};

#endif // MYTHREAD_H

mythread.cpp

#include "mythread.h"
#include <QDebug>
#include <QtCore>

MyThread::MyThread(QObject *parent)
    : QThread{parent}
{
}

void MyThread::run()
{

    while(!(Snum == 1))
    {
        QMutex mutex;
        mutex.lock();
        if(this->Stop) break;
        mutex.unlock();

        if(Snum % 2 == 0)
        {
            Snum /= 2;
        }else{
            Snum *= 3;
            Snum += 1;
        }
        emit this->NumberChanged(Snum);
    }
}

void MyThread::setSnum(int startnum)
{
    Snum = startnum;
}

最后,一個非常基本的main.cpp文件

#include "dialog.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    return a.exec();
}

我面臨的問題是我似乎沒有從mythread.cpp的運行 function 中的發射 function 獲得信號。 我在 QtCreator 的qDebug()部分沒有得到QtCreator
我究竟做錯了什么?

我正在使用 Qt 6.3.0。

感謝所有評論的人。 這確實是一個錯字。 謝謝。

你有這個連接:

connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_01(int)));

但是您的方法實際上是這樣調用的:

void Dialog::Update_O1(int x)

注意 Update_ 0 1 vs. Update_ O 1。它們是不同的字符。 一個是零,另一個是大寫“o”。

所以,你應該有:

connect(m_Thread1, SIGNAL(NumberChanged(int)), this, SLOT(Update_O1(int)));

但是,最好的解決方案是使用“新的”信號槽語法

connect(m_Thread1, &QThread::NumberChanged, this, &QDialog::Update_O1);

實際上,如果插槽很短,您甚至可以使用 lambda:

connect(m_Thread1, &QThread::NumberChanged, this, [](int x) {qDebug() << QString::number(x);});

新的信號槽語法的主要優點是這類問題會在編譯時而不是運行時被發現。 因此,您可能不會發布帶有此錯誤的軟件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM