1

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() }
                     }
                 }
Jerwin Prabu
  • 336
  • 1
  • 3
  • 16

3 Answers3

0

Create a function in your QML file to enable/disable your label

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

Then you need to connect your Qt signal to the QML slot like e.g. (connector inherits QObject) QObject::connect(&connector, SIGNAL(enableStart_2(QVariant)), window, SLOT(enableStart_2(QVariant)));

This is how you emit the signal to your QML slot emit enableStart_2(true);

UPDATE

As an example: Your

onClicked: { dialog1.startThread() }

must be something like

onClicked: { startThread() }

with startThread() being a QML signal defined in your QML file like signal startThread().

You need to connect your QML signal to a slot in your C++ code:

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

Now you need to define a slot inside of your Foo class:

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

In the definition of function onStartThread you start your thread.

Wum
  • 306
  • 1
  • 10
  • I have tried but same output i got. I have connected with signal and slot. In qt widget we can use this->ui->Start_2->setEnabled(true); in dialog.cpp. In qml How to connect using source file ? – Jerwin Prabu Aug 19 '16 at 11:40
  • Where are you stuck? Have you seen [this](http://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html), especially section 'Connecting to QML Signals'? – Wum Aug 19 '16 at 12:36
  • Ya I saw that. Connecting to QML Signals – Jerwin Prabu Aug 19 '16 at 12:51
  • And did it help you? If not, where are you stuck? – Wum Aug 19 '16 at 12:56
  • Problem is not in signal and slot. I have connected signal and slot, when I press the start button from dashboard, It was reached to source file and signal slot connected also printed. (Question)Now I want set enable other label. – Jerwin Prabu Aug 19 '16 at 15:02
0

I have found a solution for that. using his can enable the specific button.

 Connections {
               target:dialogObj
               onStart_2EnabledChanged: {
               startBtn.enabled = state
              }
                onStop_2EnabledChanged: {
                stopBtn.enabled = state
              }
         }
Jerwin Prabu
  • 336
  • 1
  • 3
  • 16
0

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