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. Add item to ComboBox from C++ at runtime
Forum Updated to NodeBB v4.3 + New Features

Add item to ComboBox from C++ at runtime

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 1.7k Views 1 Watching
  • 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.
  • F Offline
    F Offline
    federico.massimi
    wrote on last edited by
    #1

    hi, I'm a beginner with qt quick and qml.
    I am creating a small application to learn, this application has a comboBox that should be populated at runtime with the list of serial ports found.
    I created a class that scans the system and creates the list of serial ports, but how do I change the QML ComboBox at runtime from a C ++ class?

    someone can help me

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      Expose the list of your serials ports as model in c++ : https://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html

      Then use that model in your QML ComboBox.

      1 Reply Last reply
      1
      • F Offline
        F Offline
        federico.massimi
        wrote on last edited by
        #3

        ok, it works but I wanted to ask in general how to access the objects of the QML UI from the C ++ code.
        For example, if I simply wanted to change the text of a Label from C ++, how should I do it?

        GrecKoG 2 Replies Last reply
        0
        • IntruderExcluderI Offline
          IntruderExcluderI Offline
          IntruderExcluder
          wrote on last edited by
          #4

          There are several ways to achieve this. Read this: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

          F 1 Reply Last reply
          1
          • F federico.massimi

            ok, it works but I wanted to ask in general how to access the objects of the QML UI from the C ++ code.
            For example, if I simply wanted to change the text of a Label from C ++, how should I do it?

            GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • F federico.massimi

              ok, it works but I wanted to ask in general how to access the objects of the QML UI from the C ++ code.
              For example, if I simply wanted to change the text of a Label from C ++, how should I do it?

              GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              @federico-massimi You don't access QML objects from C++, you access C++ data from QML

              1 Reply Last reply
              0
              • F Offline
                F Offline
                federico.massimi
                wrote on last edited by
                #7
                This post is deleted!
                J.HilkJ 1 Reply Last reply
                0
                • IntruderExcluderI IntruderExcluder

                  There are several ways to achieve this. Read this: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

                  F Offline
                  F Offline
                  federico.massimi
                  wrote on last edited by federico.massimi
                  #8

                  @IntruderExcluder
                  I did exactly as in the link you sent me. But I have a problem, if I do it to set text of a label everything works fine, but if I try to create a
                  Q_PROPERTY(bool scanStatus READ scanStatus WRITE setScanStatus NOTIFY scanStatusChanged)
                  to set the state of a checkbox it doesn't work, or rather if I sets the state in the constructor of the Backend class it works, but after that it stops working. It seems that the bind works only in the constructor and not while I use the UI.

                  On the internet I found this solution:

                  scanControlSwitch.onClicked: {
                          backend.scanStatus = scanControlSwitch.checked;
                          scanControlSwitch.checked = Qt.binding(function () { // restore the binding
                              return backend.scanStatus;
                              }
                          );
                      }
                  

                  and it actually works, but it seems very cumbersome.
                  In other words, to set the status of a UI element I had to create:

                  • a property in the header of the class
                  • 2 c ++ functions (setter and getter for the property)
                  • register my backend class in the main.cpp and instantiate it in my qml
                  • 1 javacritp function to restore that bind after user click

                  the same thing with QtWidget was done with just one line, possible that there is no simpler way to interact with the UI?

                  1 Reply Last reply
                  0
                  • F federico.massimi

                    This post is deleted!

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    Hi @federico-massimi

                    Here‘s a pro tip for you 😉
                    Write your Q_Property Macro
                    Rightclick it
                    Select „Generate Missing Q_Properties“
                    And qt Creator will add all missing functions for you !


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1

                    • Login

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