Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Responsive layout with QFrame
QtWS25 Last Chance

Responsive layout with QFrame

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 6.2k 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.
  • T Offline
    T Offline
    Tyskie
    wrote on last edited by Tyskie
    #1

    Hello, I would like to achieve some kind of responsive design using a QLayout for my custom widgets.
    I would like my widgets to be placed like it would be done in HTML with inline elements in a block element for example my widget would be <span> elements, encapsulated in a <div> element. So when I resize my windows they got aligned or expand horizontally and when item is removed they all move automatically to fill the space missing (see picture).

    I tried to check it over the interweb, but couldn't find anything similar but H and V, and grid Layout which would be hard to maintain.

    Anyone would have some hints to start please ?

    0_1527462595174_good.png

    thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on you image and description, a QListView/ custom QStyledItemDelegate combo is likely what you are looking for.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • T Offline
        T Offline
        Tyskie
        wrote on last edited by Tyskie
        #3

        Will the QListView or the Delegate be able to work with defined width elements and move them not in a row manner?
        I would have to have the following (if I increase window width)

        0_1527521465866_test1.png

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          Well it has icon mode
          http://doc.qt.io/qt-5/qlistview.html#ViewMode-enum
          and does contract as you descipe but as far as i recall it was not super happy if
          each item is not square. Made it space the items. Maybe it just need tweaking.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            Tyskie
            wrote on last edited by
            #5

            @mrjj said in Responsive layout with QFrame:

            http://doc.qt.io/qt-5/qlistview.html#ViewMode-enum

            Alright thanks ! I am gonna give it a try.

            mrjjM 1 Reply Last reply
            0
            • T Tyskie

              @mrjj said in Responsive layout with QFrame:

              http://doc.qt.io/qt-5/qlistview.html#ViewMode-enum

              Alright thanks ! I am gonna give it a try.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Tyskie
              Hi
              You can use
              setGridSize(QSize(185,185));
              to define the layout if needed. ( also in Designer)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                Tyskie
                wrote on last edited by Tyskie
                #7

                I got the QListWidget working, but I am not quite sure how to use the QStyledItemDelegate.
                I suppose I have to override the paint method, but I have no idea how to tell it to 'redraw' my custom QWidgets is it even possible?
                Would you be able to guide me ?

                When the app starts, all is good they are shown as I want. But when I resize here is what I have
                (on left I resized bigger and on right I reduced width)

                0_1527545725141_dupa.png

                mrjjM 1 Reply Last reply
                0
                • T Tyskie

                  I got the QListWidget working, but I am not quite sure how to use the QStyledItemDelegate.
                  I suppose I have to override the paint method, but I have no idea how to tell it to 'redraw' my custom QWidgets is it even possible?
                  Would you be able to guide me ?

                  When the app starts, all is good they are shown as I want. But when I resize here is what I have
                  (on left I resized bigger and on right I reduced width)

                  0_1527545725141_dupa.png

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @Tyskie
                  Hi
                  what custom widget ?
                  did you use setItemWidget on the items ?

                  When using delegate, its as replacement for setItemWidget as it can get memory hungry and slow
                  with many items.
                  So the idea is to draw the widgets using calls to QStyle and
                  only when in edit mode for a cell, you create widgets ( In createEditor of the delegate)
                  That way it dont have to scroll around many real Widgets. All that is not being edited are just drawn.
                  see here for sample
                  http://doc.qt.io/qt-5/qtwidgets-itemviews-stardelegate-example.html

                  However, if you are only having like 50 items, setItemWidget works fine on desktop pc.

                  update:
                  use
                  setResizeMode(QListView::Adjust) to make it use all space.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    Tyskie
                    wrote on last edited by Tyskie
                    #9

                    Oh ! setResizeMode(QListView::Adjust) did the trick thank you so much :D
                    I actually don't need a QStyledItemDelegate with that (explaining below regarding you comment updates) !

                    About your questions, my custom widget is simply a QFrame with a QLabel and QButton in QHBoxLayout.
                    And yes I use addItem followed by setItemWidget with a QListWidgetItem.

                    Regarding performances, I think I will have near 50 items max, on the other hand those are using thread for the countdown, and as I limit the amount of threads, then yes, as you said, should not be a problem.

                    I am not yet very familiar with the framework, I have some trouble understanding how the delegate can draw my widget :D - need to dig that out. Thanks again.

                    mrjjM 1 Reply Last reply
                    0
                    • T Tyskie

                      Oh ! setResizeMode(QListView::Adjust) did the trick thank you so much :D
                      I actually don't need a QStyledItemDelegate with that (explaining below regarding you comment updates) !

                      About your questions, my custom widget is simply a QFrame with a QLabel and QButton in QHBoxLayout.
                      And yes I use addItem followed by setItemWidget with a QListWidgetItem.

                      Regarding performances, I think I will have near 50 items max, on the other hand those are using thread for the countdown, and as I limit the amount of threads, then yes, as you said, should not be a problem.

                      I am not yet very familiar with the framework, I have some trouble understanding how the delegate can draw my widget :D - need to dig that out. Thanks again.

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @Tyskie
                      Hi
                      Well then setItemWidget should work just fine.

                      Here is a delegate for combobox
                      http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate
                      Notice the paint. it uses QApplication::style()->drawControl to draw the item
                      There are many things, style()->drawControl and friends can draw. including all QWidgets
                      like QPushButton.

                      void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
                      {
                        QStyleOptionViewItemV4 myOption = option;
                        QString text = Items[index.row()].c_str();
                       
                        myOption.text = text;
                       
                        QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &myOption, painter);
                      }
                      

                      However, in Editmode, it uses a real combobox for selections.
                      (QWidget *ComboBoxDelegate::createEditor)

                      Its all explained here
                      http://doc.qt.io/qt-5/model-view-programming.html

                      However, if you use setItemWidget , i see no need for a delegate.

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        Tyskie
                        wrote on last edited by
                        #11

                        Well actually if one item gets removed, the others does not fill the place it left. More over when I remove an item and the resize the window, I got segment fault. I guess I am not yet done with ! hehe :)

                        Thank you for the delegate example I will check it out and learn from it.

                        mrjjM 1 Reply Last reply
                        0
                        • T Tyskie

                          Well actually if one item gets removed, the others does not fill the place it left. More over when I remove an item and the resize the window, I got segment fault. I guess I am not yet done with ! hehe :)

                          Thank you for the delegate example I will check it out and learn from it.

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @Tyskie
                          Hi
                          How do you remove the item ?
                          It should not crash. Also notice that Qt has a owner system that delete Widgets when parent is deleted
                          so its important not to delete the children manually.
                          I assume you simply remove the ListWidgetItem ?

                          http://doc.qt.io/qt-5/objecttrees.html

                          T 1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            Tyskie
                            wrote on last edited by Tyskie
                            #13

                            Hi,
                            even worse :D I have a button.clicked.connect which I connected to QFrame close().
                            so the button close the entire QFrame when X is pressed.

                            1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @Tyskie
                              Hi
                              How do you remove the item ?
                              It should not crash. Also notice that Qt has a owner system that delete Widgets when parent is deleted
                              so its important not to delete the children manually.
                              I assume you simply remove the ListWidgetItem ?

                              http://doc.qt.io/qt-5/objecttrees.html

                              T Offline
                              T Offline
                              Tyskie
                              wrote on last edited by
                              #14

                              Hi, @mrjj how would delete the widget if not by deleting itself (the widget directly - not ListWidgetItem)?

                              mrjjM 1 Reply Last reply
                              0
                              • T Tyskie

                                Hi, @mrjj how would delete the widget if not by deleting itself (the widget directly - not ListWidgetItem)?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Tyskie
                                Hi and welcome back
                                You mean take the widget back from the ListWidgetItem and delete it ?
                                (but leave ListWidgetItem alone ) ?

                                T 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  @Tyskie
                                  Hi and welcome back
                                  You mean take the widget back from the ListWidgetItem and delete it ?
                                  (but leave ListWidgetItem alone ) ?

                                  T Offline
                                  T Offline
                                  Tyskie
                                  wrote on last edited by Tyskie
                                  #16

                                  @mrjj thx, didn't had much time to get back to it yet.
                                  I meant to remove the whole 'item' from the list with ListWidgetItem and the widget/layout in it
                                  I have seem some people using takeItem/take and some with getting the model and removing item from it with removeRow, any best practice for that use case maybe?

                                  mrjjM 1 Reply Last reply
                                  0
                                  • T Tyskie

                                    @mrjj thx, didn't had much time to get back to it yet.
                                    I meant to remove the whole 'item' from the list with ListWidgetItem and the widget/layout in it
                                    I have seem some people using takeItem/take and some with getting the model and removing item from it with removeRow, any best practice for that use case maybe?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Tyskie
                                    there is
                                    http://doc.qt.io/qt-5/qlistwidget.html#removeItemWidget
                                    to remove whole widget ( and all it owns)
                                    or
                                    http://doc.qt.io/qt-5/qlistwidget.html#takeItem

                                    im not sure if u want both gone. (item and widget) or just widget

                                    1 Reply Last reply
                                    1
                                    • T Offline
                                      T Offline
                                      Tyskie
                                      wrote on last edited by
                                      #18

                                      Oh right I missed the removeItemWidget - my bad, thanks again !

                                      1 Reply Last reply
                                      1
                                      • T Offline
                                        T Offline
                                        Tyskie
                                        wrote on last edited by Tyskie
                                        #19

                                        mhm I start to think that QListWidget/View is not what I am looking for :D
                                        It's been really hard to handle the clicked.connect of my button in the widget that is associated with the QListWidgetItem.
                                        I am for example unable to find the QListWidgetItem which has the widget button in it that I am clicking on. I can surely click on the text to make it the selected QListWidgetItem and then click the button (having the delete slot method removing the current selected item) but that not really convenient.

                                        1 Reply Last reply
                                        0
                                        • T Offline
                                          T Offline
                                          Tyskie
                                          wrote on last edited by Tyskie
                                          #20

                                          OK I gave up too early - I finally succeed to make it work :D thanks a lot @mrjj

                                          mrjjM 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