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. ScrollArea intervers with expand policy
Forum Updated to NodeBB v4.3 + New Features

ScrollArea intervers with expand policy

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 3 Posters 1.1k Views 2 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.
  • M mpergand

    @buhtz
    I already gave you the solution in my previous post.
    Looking at your code, in the showEvent in LanguageDialog, you can set the minimum width of the scrollArea to the width of languageWdget.

    B Offline
    B Offline
    buhtz
    wrote on last edited by
    #7

    @mpergand said in ScrollArea intervers with expand policy:

    Looking at your code, in the showEvent in LanguageDialog, you can set the minimum width of the scrollArea to the width of languageWdget.

    You mean this line?

    scr->resize(size.width()+20,200);
    

    But where comes the 20 and 200 from? Just guessing? I assume the dialog will behave different on different screen resolutions and DPIs. Isn't there a solution without explicit numbers?

    M 1 Reply Last reply
    0
    • B buhtz

      @mpergand said in ScrollArea intervers with expand policy:

      Looking at your code, in the showEvent in LanguageDialog, you can set the minimum width of the scrollArea to the width of languageWdget.

      You mean this line?

      scr->resize(size.width()+20,200);
      

      But where comes the 20 and 200 from? Just guessing? I assume the dialog will behave different on different screen resolutions and DPIs. Isn't there a solution without explicit numbers?

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #8

      @buhtz said in ScrollArea intervers with expand policy:

      Isn't there a solution without explicit numbers?

      You can get the width of the scrollbar from QStyle:
      https://forum.qt.io/topic/333/how-to-get-width-and-height-of-qscrollbars-arrow-widgets
      it is not worth the trouble IMA
      The best user experience is to save the geometry of the dialog the user set and restore it in dialog show .

      B 1 Reply Last reply
      0
      • M mpergand

        @buhtz said in ScrollArea intervers with expand policy:

        Isn't there a solution without explicit numbers?

        You can get the width of the scrollbar from QStyle:
        https://forum.qt.io/topic/333/how-to-get-width-and-height-of-qscrollbars-arrow-widgets
        it is not worth the trouble IMA
        The best user experience is to save the geometry of the dialog the user set and restore it in dialog show .

        B Offline
        B Offline
        buhtz
        wrote on last edited by buhtz
        #9

        @mpergand said in ScrollArea intervers with expand policy:

        You can get the width of the scrollbar from QStyle:
        https://forum.qt.io/topic/333/how-to-get-width-and-height-of-qscrollbars-arrow-widgets

        That doesn't answer where your 20 and 200 comes from.
        The 20 is the scrollbar width?
        And what is the 200? If it is the height: I don't want to modify the height.
        And I also don't want to calculate the width. The width is given by the content of the widget and its childs in the scrollarea.

        And what I wonder about:

        scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        

        I deactivated that horizontal scrollbar. So the area should "know itself" that it should expand to its full width.

        M 1 Reply Last reply
        0
        • B buhtz

          @mpergand said in ScrollArea intervers with expand policy:

          You can get the width of the scrollbar from QStyle:
          https://forum.qt.io/topic/333/how-to-get-width-and-height-of-qscrollbars-arrow-widgets

          That doesn't answer where your 20 and 200 comes from.
          The 20 is the scrollbar width?
          And what is the 200? If it is the height: I don't want to modify the height.
          And I also don't want to calculate the width. The width is given by the content of the widget and its childs in the scrollarea.

          And what I wonder about:

          scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
          

          I deactivated that horizontal scrollbar. So the area should "know itself" that it should expand to its full width.

          M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #10

          @buhtz said in ScrollArea intervers with expand policy:

          I deactivated that horizontal scrollbar. So the area should "know itself" that it should expand to its full width.

          It doesn't work like that, the magic function you're looking for doesn't exist.

          B 1 Reply Last reply
          0
          • M mpergand

            @buhtz said in ScrollArea intervers with expand policy:

            I deactivated that horizontal scrollbar. So the area should "know itself" that it should expand to its full width.

            It doesn't work like that, the magic function you're looking for doesn't exist.

            B Offline
            B Offline
            buhtz
            wrote on last edited by
            #11

            I might have found a solution. Would be glad about a review from you more experienced folks.

            https://stackoverflow.com/a/76738806/4865723

            M 1 Reply Last reply
            0
            • B buhtz

              I might have found a solution. Would be glad about a review from you more experienced folks.

              https://stackoverflow.com/a/76738806/4865723

              M Offline
              M Offline
              mpergand
              wrote on last edited by mpergand
              #12

              @buhtz
              That's what i said:

              Looking at your code, in the showEvent in LanguageDialog, you can set the minimum width of the scrollArea to the width of languageWdget.

              and yes i missed this easy way:
              scrollbar_width = self._scroll.verticalScrollBar().sizeHint().width()

              I'm not sure, sizeHint() is relevant here.

              def _calculate_scroll_area_width(self):
                  widget_width =  self._scroll.widget().sizeHint().width()
                  scrollbar_width = self._scroll.verticalScrollBar().sizeHint().width()
              
                  return widget_width + scrollbar_width
              
              def showEvent(self, e):
                  super().showEvent(e)
                  self._scroll.setMinimumWidth(self._calculate_scroll_area_width())
              

              I know nothing about python, but it looks OK to me.

              B 1 Reply Last reply
              0
              • M mpergand

                @buhtz
                That's what i said:

                Looking at your code, in the showEvent in LanguageDialog, you can set the minimum width of the scrollArea to the width of languageWdget.

                and yes i missed this easy way:
                scrollbar_width = self._scroll.verticalScrollBar().sizeHint().width()

                I'm not sure, sizeHint() is relevant here.

                def _calculate_scroll_area_width(self):
                    widget_width =  self._scroll.widget().sizeHint().width()
                    scrollbar_width = self._scroll.verticalScrollBar().sizeHint().width()
                
                    return widget_width + scrollbar_width
                
                def showEvent(self, e):
                    super().showEvent(e)
                    self._scroll.setMinimumWidth(self._calculate_scroll_area_width())
                

                I know nothing about python, but it looks OK to me.

                B Offline
                B Offline
                buhtz
                wrote on last edited by
                #13

                Now I have a new problem when combining this expand-solution with centering the dialog to the screen.

                M 1 Reply Last reply
                0
                • B buhtz

                  Now I have a new problem when combining this expand-solution with centering the dialog to the screen.

                  M Offline
                  M Offline
                  mpergand
                  wrote on last edited by mpergand
                  #14

                  @buhtz
                  maybe to call in sequence:

                  • show
                  • move
                  • exec
                  1 Reply Last reply
                  0
                  • B buhtz

                    Thanks for your reply. My apologize for not offering an MWE. It was late yesterday.

                    @ChrisW67 said in ScrollArea intervers with expand policy:

                    Could I suggest that a QListWidget (or QListView and model) may be a better solution for this list.
                    Might be technical better. But I want to present my users how many languages I do support. ;) The dialog need to be a bit "boom & bling bling". ;)

                    This is the result of my MWE, still having the problem that the widget/dialog/scrollarea is not horizontally expanded.

                    03f710a3-c484-4988-a3a4-21a45d0e96f5-image.png

                    This is the MWE in Python.

                    #!/usr/bin/env python3
                    import sys
                    from PyQt5.QtCore import *
                    from PyQt5.QtWidgets import *
                    
                    class LanguageDialog(QDialog):
                        def __init__(self):
                            super().__init__()
                    
                            # Language widget
                            wdg_lang = self._language_widget()
                    
                            # Scroll area
                            scroll = QScrollArea(self)
                            # scroll.setWidgetResizable(True)
                            # scroll.setFrameStyle(QFrame.NoFrame)
                            scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
                            scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
                    
                            # Here I'm not sure what to do.
                            scroll.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
                    
                            scroll.setWidget(wdg_lang)
                    
                            # Button "Apply"
                            button = QDialogButtonBox(QDialogButtonBox.Apply)
                            button.clicked.connect(self.accept)
                    
                            # Dialog layout
                            layout = QVBoxLayout(self)
                            layout.addWidget(scroll)
                            layout.addWidget(button)
                    
                        def _language_widget(self):
                            grid = QGridLayout()
                            wdg = QWidget(self)
                            wdg.setLayout(grid)
                    
                            for col in range(1, 4):
                                for row in range(1, 24):
                    
                                    r = QRadioButton(f'{col} x {row} | Lorem ipsum dolor sit amet', self)
                                    r.toggled.connect(self.slot_radio)
                                    r.mydata = (col, row)
                    
                                    grid.addWidget(r, row, col)
                    
                            # ???
                            # wdg.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
                    
                            return wdg
                    
                        def slot_radio(self, val):
                            btn = self.sender()
                            if btn.isChecked():
                                print(f'{btn.mydata=}')
                    
                    
                    class Window(QMainWindow):
                        def __init__(self, parent=None):
                            super().__init__(parent)
                    
                        def showEvent(self, e):
                            dlg = LanguageDialog()
                            dlg.exec()
                    
                    
                    if __name__ == "__main__":
                        app = QApplication(sys.argv)
                        win = Window()
                        win.show()
                        sys.exit(app.exec())
                    
                    C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #15

                    @buhtz said in ScrollArea intervers with expand policy:

                    @ChrisW67 said in ScrollArea intervers with expand policy:
                    Could I suggest that a QListWidget (or QListView and model) may be a better solution for this list.

                    Might be technical better. But I want to present my users how many languages I do support. ;) The dialog need to be a bit "boom & bling bling". ;)

                    This is the sort of thing I had in mind. It resizes itself to fit as much space as is available, show as many entries as possible, and does not have the child resizing its parent (i.e. the tail wagging the dog).

                    #include <QApplication>
                    #include <QWidget>
                    #include <QLabel>
                    #include <QListWidget>
                    #include <QListWidgetItem>
                    #include <QVBoxLayout>
                    
                    int main(int argc, char **argv) {
                            QApplication app(argc, argv);
                    
                            QWidget widget;
                            QListWidget *list = new QListWidget(&widget);
                            list->setWrapping(true);
                            list->setGridSize(QSize(100, 20));
                            list->setFlow(QListView::LeftToRight);
                            list->setResizeMode(QListView::Adjust);
                            for (int i = 0; i< 52; ++i) {
                                    QListWidgetItem *item = new QListWidgetItem(QString("Language %1").arg(i), list);
                                    item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
                                    item->setCheckState(Qt::Unchecked);
                            }
                    
                            QLabel *label = new QLabel("Resize me", &widget);
                    
                            QVBoxLayout *layout = new QVBoxLayout(&widget);
                            layout->addWidget(list);
                            layout->addWidget(label);
                    
                            widget.show();
                            return app.exec();
                    }
                    

                    e3cdd939-3b5a-4c02-a387-1afe52e75c4c-image.png
                    e235e955-2d15-4e6b-9d55-25b65e8d4f98-image.png

                    1 Reply Last reply
                    1
                    • B Offline
                      B Offline
                      buhtz
                      wrote on last edited by
                      #16

                      Great. Thanks in advance. I wasn't aware that the listview can have mutliple columns.

                      B 1 Reply Last reply
                      0
                      • B buhtz

                        Great. Thanks in advance. I wasn't aware that the listview can have mutliple columns.

                        B Offline
                        B Offline
                        buhtz
                        wrote on last edited by
                        #17

                        Based on your example I'm not sure how you realized the multi columns. I thought it was setWrapping(True) and setGridSize(). But it does not work in my example where I try to create a 2 columns with 2 rows.

                        62a96135-c58e-4ca7-a931-e704b4e4dc6b-image.png

                        #!/usr/bin/env python3
                        import sys
                        from PyQt5.QtCore import *
                        from PyQt5.QtWidgets import *
                        
                        
                        class CenteredDialog(QDialog):
                            def __init__(self):
                                super().__init__()
                        
                                wdg = QListWidget(self)
                                wdg.setWrapping(True)
                                wdg.setResizeMode(QListView.Adjust)
                                wdg.setFlow(QListView.TopToBottom)
                                wdg.setGridSize(QSize(2, 2))
                                for idx in range(1, 3):
                                    wdg.addItem(f'item {idx}')
                        
                                # Button "Apply"
                                button = QDialogButtonBox(QDialogButtonBox.Apply)
                                button.clicked.connect(self.accept)
                        
                                # Dialog layout
                                layout = QVBoxLayout(self)
                                layout.addWidget(wdg)
                                layout.addWidget(button)
                        
                        
                        if __name__ == "__main__":
                            app = QApplication(sys.argv)
                            dlg = CenteredDialog()
                            dlg.show()
                            sys.exit(app.exec())
                        
                        C 1 Reply Last reply
                        0
                        • B buhtz

                          Based on your example I'm not sure how you realized the multi columns. I thought it was setWrapping(True) and setGridSize(). But it does not work in my example where I try to create a 2 columns with 2 rows.

                          62a96135-c58e-4ca7-a931-e704b4e4dc6b-image.png

                          #!/usr/bin/env python3
                          import sys
                          from PyQt5.QtCore import *
                          from PyQt5.QtWidgets import *
                          
                          
                          class CenteredDialog(QDialog):
                              def __init__(self):
                                  super().__init__()
                          
                                  wdg = QListWidget(self)
                                  wdg.setWrapping(True)
                                  wdg.setResizeMode(QListView.Adjust)
                                  wdg.setFlow(QListView.TopToBottom)
                                  wdg.setGridSize(QSize(2, 2))
                                  for idx in range(1, 3):
                                      wdg.addItem(f'item {idx}')
                          
                                  # Button "Apply"
                                  button = QDialogButtonBox(QDialogButtonBox.Apply)
                                  button.clicked.connect(self.accept)
                          
                                  # Dialog layout
                                  layout = QVBoxLayout(self)
                                  layout.addWidget(wdg)
                                  layout.addWidget(button)
                          
                          
                          if __name__ == "__main__":
                              app = QApplication(sys.argv)
                              dlg = CenteredDialog()
                              dlg.show()
                              sys.exit(app.exec())
                          
                          C Offline
                          C Offline
                          ChrisW67
                          wrote on last edited by ChrisW67
                          #18

                          @buhtz The grid size is a QSize in pixels x and y, not a number of rows and columns. Items will align on this notional grid when rendered. You can leave it out and the wrapping will be compact.

                          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