Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Declaring Variable

Declaring Variable

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 6 Posters 16.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eirnanG
    wrote on last edited by
    #1

    how to declare an Int or String in CPP or Header File that can be use and display in QML?

    MOved to Qt Quick, as it's no C++ prtoblem, more a Qt Quick problem, Gerolf

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You can declare int and string in c++ like this:
      @
      #include <string>
      void foo ()
      {
      int i;
      std :: string str;
      ...
      }
      @

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eirnanG
        wrote on last edited by
        #3

        hw cn i use or call that in qml..

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          If you use the qml tag on the right, you get a lot of info from devnet.
          This one can interest you:
          http://developer.qt.nokia.com/doc/qt-4.7/qdeclarativemodels.html

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0
          • E Offline
            E Offline
            eirnanG
            wrote on last edited by
            #5

            aside from using List.. is there any function that i can use?

            1 Reply Last reply
            0
            • EddyE Offline
              EddyE Offline
              Eddy
              wrote on last edited by
              #6

              "Here is a tutorial that uses a string :":http://developer.qt.nokia.com/wiki/Introduction_to_Qt_Quick_for_Cpp_developers#2bbc678dfb658d1ac1f900544fdfd9d8

              You can use a similar approch using int.

              What is it that you don't understand exactly?

              Qt Certified Specialist
              www.edalsolutions.be

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chuck Gao
                wrote on last edited by
                #7

                You can also have a look at the basic type in QML :-)

                Chuck

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  changsheng230
                  wrote on last edited by
                  #8

                  The key is

                  1. declare your Int or QString like the following :
                    @
                    class Foo : public QObject
                    {
                    Q_OBJECT
                    public:
                    Q_PROPERTY(int myInt READ myInt WRITE setMyInt)
                    Q_PROPERTY(int myString READ myString WRITE setMyString)
                    };
                    @

                  2. Expose your C++ Object to QML :
                    @
                    int main(int argc, char *argv[])
                    {
                    QApplication app(argc, argv);
                    QDeclarativeView view;
                    Foo foobar;
                    view.rootContext()->setContextProperty("myObject", &foobar;);
                    view.setSource(QUrl::fromLocalFile("passInt.qml"));
                    view.show();
                    return app.exec();
                    }
                    @

                  3. Use Exposed Int or String in QML:

                  @
                  Item {
                  width: myObject.myInt
                  height: myObject.myInt *2
                  }
                  @

                  Chang Sheng
                  常升

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #9

                    For completeness, you probably want to use the NOTIFY signal in your Q_PROPERTY declarations.

                    @
                    public:
                    Q_PROPERTY(int myInt READ myInt WRITE setMyInt NOTIFY myIntChanged)
                    Q_PROPERTY(int myString READ myString WRITE setMyString NOTIFY myStringChanged)
                    @

                    And be sure to emit myIntChanged(value) in setMyInt(), etc.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      eirnanG
                      wrote on last edited by
                      #10

                      @all thanks for your replies.. ill try it asap when i got home... thanks again..

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved