簡體   English   中英

QT/C++ 使用插槽調用小部件的 function

[英]QT/C++ Call function of a widget using a slot

考慮以下main.cpp (整個 Qt 項目的唯一文件):

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QObject>
#pragma once

class collectedFunctions : public QObject
{
    Q_OBJECT

public:
    collectedFunctions(QObject* parent = 0) {};
    ~collectedFunctions() {};

public slots:
    void setFunc() {
        //Calculate 2+2 and change text of the label accordingly
    }
};

#include "main.moc"

int main(int argc, char* argv[]) {

    QApplication app(argc, argv);

    QWidget window;
    window.resize(200, 200);
    window.setWindowTitle("Test-GUI");
    QVBoxLayout layout(&window);

    QPushButton button(&window);
    button.setText("Test");
    button.show();
    
    QLabel *label = new QLabel(&window);
    label->setText("Hello");

    collectedFunctions testingFuncs;

    QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);
    
    layout.addWidget(&button);
    layout.addWidget(label);
    
    window.show();
    return app.exec();
}

我使用此代碼的目標是創建一個運行程序員定義的 function 的插槽(類似於信號和插槽系統的 pyqt 實現)。 上面的代碼成功編譯,沒有拋出任何錯誤,到目前為止符合我的期望。 然而,只要我想操作一個小部件(在這種情況下是標簽), setfunc -slot 就找不到定義的 label。 如何使用此插槽操作小部件(例如,在 label 上使用 setText() 或在組合框上使用 addItems())或通常讓它們在插槽中被識別。

您不能在main.cpp中使用Q_OBJECT宏。

Q_OBJECT 宏必須出現在 class 定義的私有部分中,該定義聲明自己的信號和插槽或使用 Qt 元對象系統提供的其他服務。

moc 工具讀取 C++ header文件。 如果它找到一個或多個包含Q_OBJECT 宏的 class 聲明,它會生成一個包含這些類的元對象代碼的 C++ 源文件。 除此之外,信號和槽機制、運行時類型信息和動態屬性系統都需要元對象代碼。

moc 生成的 C++ 源文件必須與 class 的實現進行編譯和鏈接。

當您在.cpp文件而不是.h文件中定義 class 時,moc無法正確處理它。

您需要一個單獨的 class 文件:

在collectedfunctions.h

#pragma once
#include <QObject>

class collectedFunctions: public QObject
{
    Q_OBJECT

public:
    collectedFunctions(QObject *parent = 0);

    ~collectedFunctions();

public slots:
    void  setFunc();
};

在collectedfunctions.cpp中

#include "collectedfunctions.h"
#include <QDebug>

collectedFunctions::collectedFunctions(QObject *parent)
{
}

collectedFunctions::~collectedFunctions()
{
}

void  collectedFunctions::setFunc()
{
    qDebug() << "2+2 = " << 2 + 2;
}

在你的 main.cpp

 #include <QApplication>
 #include <QWidget>
 #include <QPushButton>
 #include <QVBoxLayout>
 #include <QLabel>

#include "collectedfunctions.h"

int  main(int argc, char *argv[])
{
    QApplication  app(argc, argv);
    QWidget       window;

    window.resize(200, 200);
    window.setWindowTitle("Test-GUI");
    QVBoxLayout  layout(&window);
    QPushButton  button(&window);
    button.setText("Test");
    button.show();

    QLabel *label = new QLabel(&window);
    label->setText("Hello");

    collectedFunctions  testingFuncs;

    QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);

    layout.addWidget(&button);
    layout.addWidget(label);

    window.show();

    return app.exec();
}

不要忘記在您的.pro文件中添加QT +=core gui widgets

結果:

在此處輸入圖像描述

如果要更改 QLabel 文本:

在collectedfunctions.h

#pragma once
#include <QObject>

class collectedFunctions: public QObject
{
    Q_OBJECT

public:
    explicit collectedFunctions(QObject *parent = 0);

    ~collectedFunctions();

signals:
    void  updateLable(int num);

public slots:
    void  setFunc();

private:
    int  num = 0;
};

在collectedfunctions.cpp中

#include "collectedfunctions.h"
#include <QDebug>

collectedFunctions::collectedFunctions(QObject *parent)
{
}

collectedFunctions::~collectedFunctions()
{
}

void  collectedFunctions::setFunc()
{
    qDebug() << "2+2 = " << 2 + 2;

    num++;
    emit  updateLable(num);
}

在你的 main.cpp

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>

#include "collectedfunctions.h"

int  main(int argc, char *argv[])
{
    QApplication  app(argc, argv);
    QWidget       window;

    window.resize(200, 200);
    window.setWindowTitle("Test-GUI");
    QVBoxLayout  layout(&window);
    QPushButton  button(&window);
    button.setText("Test");
    button.show();

    QLabel *label = new QLabel(&window);
    label->setText("Hello");

    collectedFunctions  testingFuncs;

    QObject::connect(&button, &QPushButton::clicked, &testingFuncs, &collectedFunctions::setFunc);

    QObject::connect(&testingFuncs, &collectedFunctions::updateLable, [&label](int num)
    {
        qDebug() << "Number = " << num;
        label->setText(QString::number(num));
    });

    layout.addWidget(&button);
    layout.addWidget(label);

    window.show();

    return app.exec();
}

你可以看到:

在此處輸入圖像描述

暫無
暫無

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

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