c++qtqmlqt-creatorqtquick-designer

Qt c++ and Qml signal and slot connection


I have created a qt widget for my project. Button, signal and slot, thread everything working properly also I have taken output.

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();
}

For creating dashboard purpose I have developed same ui in qml, signal and slot everything connected when I press the button signal and slot connected. But I don't know how to connect the label button, set enabled.

that is qt widget code. below that dialog.cpp and qml

dialog.cpp

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

    emit startbuttonclicked();
}

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

    emit stopbuttonclicked();
}

dashboard.qml : (Like all buttons same)

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

Solution

  • Specifies that type of variable the function returns main() must return an integer

    No scaling before QApplication is instantiated.

         QGuiApplication app(argc, argv);
    
        QFontDatabase::addApplicationFont(":/fonts/DejaVuSans.ttf");
        app.setFont(QFont("DejaVu Sans"));
    // Now I am using Dialog like context property in qml
    // qmlRegisterType<Dialog>("CustomExtensions",1,0,"Dialog");
    Dialog dlg;
    QQmlApplicationEngine engine;
    
    // Now in qml we can access to dlg by "dialogObj" name
    
    engine.rootContext()->setContextProperty("dialogObj", &dlg);
    engine.load(QUrl(QStringLiteral("qrc:/qml/dashboard.qml")));
    
    //QQmlApplicationEngine engine(QUrl("qrc:/qml/dashboard.qml"));       ``
    

    Using this you can access Qt signal and slot with qml