Qt c ++和Qml信号和插槽连接

时间:2016-08-19 09:46:05

标签: c++ qt qml qt-creator qtquick-designer

我为我的项目创建了一个qt小部件。按钮,信号和插槽,线程一切正常工作,我也采取了输出。

Dialog.h

public slots:

    void startThread();
    void stopThread();
};

#endif // DIALOG_H

dialog.cpp

void  Dialog:: startThread(){

    if(!startThreadflag){

    this->ui->Start_2->setEnabled(false);
    this->ui-> Stop_2->setEnabled(true);

    startThreadflag = true;
    stopThreadflag = false;

    }
}

void Dialog:: stopThread(){

    if(!stopThreadflag){
            cout << "Closing threads"<< endl;

        this->ui->Start_2->setEnabled(true);
        this->ui->Stop_2->setEnabled(false);

        stopThreadflag= true;
        startThreadflag = false;
    }
}

void Dialog::on_Close_clicked()
{
    cout<<"close_clicked"<<endl;

    this->close();
}

为了创建仪表板目的,我在qml,信号和插槽中开发了相同的ui,当我按下按钮信号和插槽连接时,所有连接。但我不知道如何连接标签按钮,设置已启用。

这是qt小部件代码。在那个dialog.cpp和qml下面

dialog.cpp

void Dialog::startThread()
{
    cout<<"Start Clicked"<<endl;

    emit startbuttonclicked();
}

void Dialog::stopThread()
{
    cout<<"Stop Clicked"<<endl;

    emit stopbuttonclicked();
}

dashboard.qml :(像所有按钮一样)

               Item {
              Dialog { 
                 id: dialog1;
                  }
                 Button {
                     x:220
                     y:295
                     text: "Start"
                     onClicked: { dialog1.startThread() }
                     }
                 }

3 个答案:

答案 0 :(得分:0)

在QML文件中创建一个功能以启用/禁用标签

function enableStart_2(enable)
{
    Start_2.enabled = enable;
}

然后你需要将你的Qt信号连接到QML插槽,例如(连接器继承QObjectQObject::connect(&connector, SIGNAL(enableStart_2(QVariant)), window, SLOT(enableStart_2(QVariant)));

这是您向QML插槽发出信号的方式 emit enableStart_2(true);

<强>更新

举个例子:你的

onClicked: { dialog1.startThread() }

必须是

之类的东西
onClicked: { startThread() }

startThread()是QML文件中定义的QML信号,如signal startThread()

您需要将QML信号连接到C ++代码中的插槽:

QObject::connect(window, SIGNAL(startThread()), &foo, SLOT(onStartThread()));

现在你需要在你的Foo类中定义一个插槽:

class Foo : public QObject
{
    Q_OBJECT
public slots:
    void onStartThread(void);
};

在函数onStartThread的定义中,您可以启动线程。

答案 1 :(得分:0)

我找到了解决方法。使用他可以启用特定按钮。

  keytool -exportcert -list -v \-alias androiddebugkey -keystore ~/.android/debug.keystore  

答案 2 :(得分:0)

指定函数返回的变量类型 main()必须返回一个整数

在实例化QApplication之前不进行缩放。

try {
            rapidjson::Document document;
            document.Parse(connection.data.description);
            connection.openTime = document["openFrom"].GetInt();
            connection.closeTime = document["openTo"].GetInt();
            return true;
        } catch (std::exception e) {
            connection.openTime = 0;
            connection.closeTime = 0;
            return false;
        }

使用此功能,您可以使用qml

访问Qt信号和插槽