Qt Quick QuickStart Qml and C + + interaction

Source: Internet
Author: User
Tags emit

Use the Qml object in C + +, use Findchild to get the Qml object directly, and then call the SetProperty method to set the property, of course it must be loaded QML before it can be used, or Findchild cannot find the object, use the following.

    Engine.load (Qurl (Qstringliteral ("qrc:/main.qml"));     * text_msg = Engine.rootobjects () [0]->findchild<qobject*> ("text_msg" );    Text_msg->setproperty ("color","red" );

 

QML uses C + + objects, which is also the implementation of Model/view in Qt, here is an example.

First, classes need to inherit from QOBEJCT

classUser: Publicqobject{q_object q_property (QString name READ name WRITE setName NOTIFY namechanged) q_property (intAge READ-Age WRITE setage NOTIFY agechanged) Q_property (QString message READ message WRITE setmessage NOTIFY messagechanged) Public: User (); User (stringNameintAge );    QString Name (); voidsetName (QString name); intAge (); voidSetage (intAge );    QString Message (); voidsetmessage (QString message); Signals:voidnamechanged (); voidagechanged (); voidmessagechanged (); PublicSlots:voidEditok (); Private: QString m_name; intM_age; QString m_message;};

  

User::user () { This->setname ("");  This->setage (0);} User::user (stringNameintAge ) {QString Q_name=qstring::fromstdstring (name);  This-SetName (q_name);  This-Setage (age);} QString User::name () {returnM_name;}voiduser::setname (QString name) {M_name=name;  Emit namechanged (); Setmessage (QString ("name changed to:%1"). Arg (name));}intUser::age () {returnm_age;}voidUser::setage (intAge ) {M_age=Age ;  Emit agechanged (); Setmessage (QString ("age changed to:%1"). Arg (age);} QString User::message () {returnm_message;}voiduser::setmessage (QString message) {M_message=message; Emit messagechanged ();}voidUser::editok () {Setmessage ("you clicked the OK button");}

Then, you need to register this class with QML so that we can import this class in QML.

Qmlregistertype<user> ("models.user",1,0,"  Usermodel");

  

1.0

Typically, you set the object directly to the context of the Qml object, and then use the properties of the C + + object in QML

    qqmlapplicationengine engine;    Qqmlcontext* context = engine.rootcontext ();    UserNew User (" test ");    Context->setcontextproperty ("testusermodel", Usermodel);
GridLayout{Anchors.centerin:parent width:300 columnspacing:10 rowspacing:10 rows:4 columns:2 item{                layout.row:0 layout.column:0 width:100 height:30 text{ Text: "Name:" Anchors.centerIn:parent}} Item{Layout.row:0 layout.column:1 Layout.fillWidth:true height:30 textfield{                Anchors.verticalCenter:parent.verticalCenter Width:parent.width height:24 Id:textfield_name text:testUserModel.Name oneditingfinished: {testus Ermodel.name = Textfield_name.text; }}} Item{Layout.row:1 layout.column:0 width:100 height:30 text{Text: "Age:" Anchors.centerIn:parent}} Item{Layout.row:1 layout.column:1 Layout.fillWidth:true height:30 textfield{                Anchors.verticalCenter:parent.verticalCenter Width:parent.width height:24 Id:textfield_age Text:testUserModel.Age oneditingfinished: {testuserm Odel. Age = Textfield_age.text; }}} Item{Layout.row:2 layout.column:0 Layout.fillWidth:true height:50 Layout.columnspan:2                    rowlayout{anchors.centerIn:parent button{ID:BUTTON_OK                        Text: "OK" action:button_ok_action onclicked:{ Testusermodel.editok (); }} Button{ID:button_cancel text: "Cancel" onclicked: {qt.quit (); }}}} Item{Layout.row:3 layout.column:0 Layout.fillWidth:true height:30 Layout.columnspan:2 Text {id:text_msg objectName: "Text_msg" text:testUserModel.Me Ssage anchors.fill:parent VerticalAlignment:Qt.AlignVCenter}        }    }

When you bind a C + + object's properties in QML, it is actually the Read method that executes the properties of the C + + object, and when the property is set, the Write method is executed; The Notify method of the C + + property is used for property change notification when we call the method (usually preceded by the emit to indicate that this is a signaling method) , the property binding in QML executes the Read method again.

The following is the effect, when the edit box loses focus, the property is set, the Write method of the C + + object is called, and the other properties are updated in the Write method.

Qt Quick QuickStart Qml and C + + interaction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.