Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing signal with parameters from qml to c++

Tags:

qt

qml

I dont know how to pass parameters from QML file to c++ file in Qt.

QML code:

import QtQuick 1.1

Rectangle{
    id:loin
    height: 272
    width:480
    property alias loguid:loginuid
    signal sigHome()
    Rectangle{
        id:rect1
        width:parent.width-80
        height:24
        TextInput {
            id:loginuid
            maximumLength: 16
            width: maximumLength * 20
            focus: false
            validator: RegExpValidator { regExp: /\d+/ }
            KeyNavigation.down: login1
        }
    }
    Button{
        id: login1
        x: 195
        y: 187
        height:30;
        focus:false
        border.color:"black"
        opacity: activeFocus ? 1.0 : 0.5
        Text{
        text:"LOGIN"
            anchors.horizontalCenter:login1.horizontalCenter;
            anchors.verticalCenter:login1.verticalCenter;
        }
        Keys.onReturnPressed: {
             if(loginuid.text  <  1000000000000000)
             {
                 text1.opacity=0.1
                 error1.visible=true
                 errorText.text="\n enter valid 16 digit number\n"
                 errorOk.focus=true
                 loginuid.focus=false
             }
             else{
                 loginuid.focus=false
                 loin.sigHome()
             }
        }
    }
}

c++ code:

#include <QApplication>
#include <QDeclarativeView>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    int uid;
    QDeclarativeView view;
    view.setSource(QUrl::fromLocalFile("main.qml"));
    view.show();
    return app.exec();
}

When I press the login button rect1.text content shud go to main.cpp file and uid in the main.cpp get dat value. Something like this uid=rect1.text.
How to do it?

like image 462
geek Avatar asked Jan 14 '23 13:01

geek


1 Answers

I wouldn't try to listen for a QML signal from the C++ side. Calling a C++ method with arguments is much easier and achieves the same:

To do so you have to:

  • define a slot or invokable method accepting the required arguments
  • register the class carrying the method with the declarative engine
  • then you can set an instance of this class as a property of your root context and finally call this method from QML

This topic is also well covered in the official documentation.

like image 135
sebasgo Avatar answered Jan 17 '23 19:01

sebasgo