Results 1 to 5 of 5

Thread: c++ Send data from and back to another qml file?

  1. #1
    Join Date
    Jan 2016
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default c++ Send data from and back to another qml file?

    I have 2 qml files:
    1 is main.qml when user write his name and click go button send the name to main.cpp file and main.cpp send the name statment to
    2 showName.qml and hide main.qml and open showName.qml
    how to do that?
    main.qml
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Window 2.2
    3. import QtQuick.Controls 1.2
    4.  
    5. Window {
    6. visible: true
    7.  
    8. TextField {
    9. id: aName
    10. placeholderText: qsTr("Enter your name")
    11. }
    12.  
    13. Button {
    14. id: go
    15. y: 50
    16. text: qsTr("Go")
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    showName.qml
    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Window 2.2
    3. import QtQuick.Controls 1.2
    4.  
    5. Item {
    6. Text {
    7. id: aName
    8. text: qsTr( aName )// aName come from main.cpp
    9. font.pixelSize: 12
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QQmlApplicationEngine>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QGuiApplication app(argc, argv);
    7.  
    8. QQmlApplicationEngine engine;
    9. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    10. // i want here to get aName from main.cpp, send aName to showName.qml, hide main.qml and show showName.qml
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ Send data from and back to another qml file?

    If you want the scene in showName.qml to be shown in a new window, it also needs to have a Window as its root element.

    If you rename it to ShowName.qml, you can simply instantiate it in main.qml and call show() on that instance and simultantiously call hide() on the current window.

    ShowName's top level element simply needs a property to get the text, e.g. an alias to the internal Text element's text property.
    You can then simply set this in TextField's onEditingFinished signal handler.
    No need to involve C++ here at all.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    umnbr (27th January 2016)

  4. #3
    Join Date
    Jan 2016
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ Send data from and back to another qml file?

    thank you anda_skoa,
    i just want to learn sending and receiving data with c++.
    could you help me? or give me a link tutorial at this point?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: c++ Send data from and back to another qml file?

    Ah, that is something different then.

    There are a couple of ways for different use cases, but all of them require you to have a QObject derived class.

    The approach you probably want to experiement with first is creating a QObject derived class and add properties to it
    http://doc.qt.io/qt-5/properties.html

    Then you create an instance of that in main() and set it as a context property on the engine's root context
    Qt Code:
    1. engine.rootContext()->setContextProperty("_myCppObject", &myObjectInstance);
    To copy to clipboard, switch view to plain text mode 
    and then access it in QML like properties of a QML element with an id
    Qt Code:
    1. Text {
    2. text: _myCppObject.nameOfTheCppProperty
    3. }
    4. TextField {
    5. onEditingFinished: _myCppObject.nameOfTheCppProperty = text;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    umnbr (27th January 2016)

  7. #5
    Join Date
    Jan 2016
    Posts
    13
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: c++ Send data from and back to another qml file?

    thank you anda_skoa,
    i have to learn qml basics first

Similar Threads

  1. Replies: 3
    Last Post: 12th January 2016, 11:52
  2. Replies: 9
    Last Post: 10th October 2012, 23:55
  3. Bring To Front Send To Back, where's the inbetween?
    By InterFiction in forum Newbie
    Replies: 1
    Last Post: 25th November 2011, 00:54
  4. Thread started from main does send signal back.
    By edxxgardo in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2011, 15:36
  5. How to convert image data type to plain text and back ?
    By aircraftstories in forum Qt Programming
    Replies: 3
    Last Post: 13th April 2011, 17:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.