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. How to create QtQuick widgets using a python "for" loop
Forum Updated to NodeBB v4.3 + New Features

How to create QtQuick widgets using a python "for" loop

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
10 Posts 2 Posters 1.0k 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.
  • C Offline
    C Offline
    cheesecake_factory
    wrote on 12 Jul 2021, 00:36 last edited by
    #1

    I've been trying to develop an application similar to a browser (offline, shows scraped results of specific websites). Does the app need to display the corresponding results when the user searches for something how do I do this? I was thinking of making it so that a python loop creates widgets for each item in the list but I do not know how to set up the signal and slot for it.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 12 Jul 2021, 05:09 last edited by
      #2

      From your description it seems like you need a QListView with a custom QAbstractListModel attached to it. And to add search & filter functionality, you may need to take a look at QSortFilterProxyModel.

      I don't think generating widgets in a loop will be necessary here. But just in case you need it - yes it's possible.

      (Z(:^

      1 Reply Last reply
      1
      • C Offline
        C Offline
        cheesecake_factory
        wrote on 12 Jul 2021, 11:06 last edited by
        #3

        Is it possible for QML in QtQuick? I think that solution is for QtWidgets

        S 1 Reply Last reply 12 Jul 2021, 11:07
        0
        • C cheesecake_factory
          12 Jul 2021, 11:06

          Is it possible for QML in QtQuick? I think that solution is for QtWidgets

          S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 12 Jul 2021, 11:07 last edited by
          #4

          @cheesecake_factory said in How to create QtQuick widgets using a python "for" loop:

          Is it possible for QML in QtQuick? I think that solution is for QtWidgets

          Yes, just use ListView instead of QListView. The rest (models) stay the same.

          (Z(:^

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cheesecake_factory
            wrote on 12 Jul 2021, 11:47 last edited by
            #5

            Thank you very much for the help. I just have one more question: Can you set signals and events for the items within that list? So just like the urls appearing after a google search would I be able to mae it so that it has an underline when I hover and change colors (signals for each button too)?

            S 1 Reply Last reply 12 Jul 2021, 11:50
            0
            • C cheesecake_factory
              12 Jul 2021, 11:47

              Thank you very much for the help. I just have one more question: Can you set signals and events for the items within that list? So just like the urls appearing after a google search would I be able to mae it so that it has an underline when I hover and change colors (signals for each button too)?

              S Offline
              S Offline
              sierdzio
              Moderators
              wrote on 12 Jul 2021, 11:50 last edited by
              #6

              @cheesecake_factory said in How to create QtQuick widgets using a python "for" loop:

              Thank you very much for the help. I just have one more question: Can you set signals and events for the items within that list? So just like the urls appearing after a google search would I be able to mae it so that it has an underline when I hover and change colors (signals for each button too)?

              Yes, of course you have full control over the items (the visual part is called a Delegate), and since you will have a subclass of QAbstractListModel, you will also have full control over the data which Delegates display. Se a general description of the system here: https://doc.qt.io/qt-5/qtquick-modelviewsdata-modelview.html#view-delegates

              (Z(:^

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cheesecake_factory
                wrote on 12 Jul 2021, 13:49 last edited by cheesecake_factory 7 Dec 2021, 13:51
                #7

                I see. Thanks very much for the help again. Just out of curiosity, how can you create widgets through a loop?

                S 1 Reply Last reply 13 Jul 2021, 04:27
                0
                • C cheesecake_factory
                  12 Jul 2021, 13:49

                  I see. Thanks very much for the help again. Just out of curiosity, how can you create widgets through a loop?

                  S Offline
                  S Offline
                  sierdzio
                  Moderators
                  wrote on 13 Jul 2021, 04:27 last edited by
                  #8

                  @cheesecake_factory said in How to create QtQuick widgets using a python "for" loop:

                  I see. Thanks very much for the help again. Just out of curiosity, how can you create widgets through a loop?

                  You mean QWidget?

                  for (int i = 0; i < 5; ++i) {
                    new QWidget(this);
                  }
                  
                  qDebug() << "Widgets:" << children();
                  

                  But that would not look good at all, of course. Better to use some QLayout to position them nicely.

                  If you mean QML, then you should be saying "components" not "widgets". There are many ways to do it, for example using a repeater:

                  Column {
                    Repeater {
                      model: 5
                      Text {
                        text: "Oh hi! " + index
                      }
                    }
                  }
                  

                  Or you can use ListView with a simple ListModel. Or use JS: https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

                  (Z(:^

                  C 1 Reply Last reply 13 Jul 2021, 16:59
                  1
                  • S sierdzio
                    13 Jul 2021, 04:27

                    @cheesecake_factory said in How to create QtQuick widgets using a python "for" loop:

                    I see. Thanks very much for the help again. Just out of curiosity, how can you create widgets through a loop?

                    You mean QWidget?

                    for (int i = 0; i < 5; ++i) {
                      new QWidget(this);
                    }
                    
                    qDebug() << "Widgets:" << children();
                    

                    But that would not look good at all, of course. Better to use some QLayout to position them nicely.

                    If you mean QML, then you should be saying "components" not "widgets". There are many ways to do it, for example using a repeater:

                    Column {
                      Repeater {
                        model: 5
                        Text {
                          text: "Oh hi! " + index
                        }
                      }
                    }
                    

                    Or you can use ListView with a simple ListModel. Or use JS: https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

                    C Offline
                    C Offline
                    cheesecake_factory
                    wrote on 13 Jul 2021, 16:59 last edited by
                    #9

                    @sierdzio I meant how to set up the signal and slot and thanks for pointing out my mistakes. "Repeater" could be used in this case but I want to send data to the QML file. For example, say I have a dictionary of an image link and its respective site link I want the app to display the image. How can I set it up like that?

                    S 1 Reply Last reply 14 Jul 2021, 05:10
                    0
                    • C cheesecake_factory
                      13 Jul 2021, 16:59

                      @sierdzio I meant how to set up the signal and slot and thanks for pointing out my mistakes. "Repeater" could be used in this case but I want to send data to the QML file. For example, say I have a dictionary of an image link and its respective site link I want the app to display the image. How can I set it up like that?

                      S Offline
                      S Offline
                      sierdzio
                      Moderators
                      wrote on 14 Jul 2021, 05:10 last edited by
                      #10

                      @cheesecake_factory said in How to create QtQuick widgets using a python "for" loop:

                      @sierdzio I meant how to set up the signal and slot and thanks for pointing out my mistakes. "Repeater" could be used in this case but I want to send data to the QML file. For example, say I have a dictionary of an image link and its respective site link I want the app to display the image. How can I set it up like that?

                      I don't know Python so I can't help on that front. But in QML, if you have some model or QObject attached as context root property to the engine, it can be quite easy.

                      Column {
                        Repeater {
                          model: yourContextProperty.link.length
                          Row {
                            Text {
                              text: "Oh hi! " + yourContextProperty.link[index]
                            }
                            Image {
                              source: yourContextProperty.image[index]
                            }
                          }
                        }
                      }
                      

                      Where yourContextProperty is a QObject like:

                      class MyObject : public QObject
                      {
                        Q_OBJECT
                      
                        Q_PROPERTY(QStringList link READ link NOTIFY linkChanged)
                        Q_PROPERTY(QStringList image READ image NOTIFY imageChanged)
                      
                        public:
                          // blah blah, the usual stuff
                      
                        QStringList link() const
                        {
                          return { "aaa.com", "bbb.com" };
                        }
                      
                        QStringList image() const;
                        {
                          return { ":/imageA,png", ":/imageB.png" };
                        }
                      
                        signals:
                        void linkChanged() const;
                        void imageChanged() const;
                      };
                      

                      And you add this object to engine like this:

                      // main.cpp
                      
                      QQmlEngine engine;
                      auto myObject = new MyObject(&engine);
                      engine.rootContext()->setContextProperty("yourContextProperty", myObject);
                      

                      (Z(:^

                      1 Reply Last reply
                      0

                      1/10

                      12 Jul 2021, 00:36

                      • Login

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