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. QGridLayout - Fixed Number of Rows and Columns
Forum Update on Monday, May 27th 2025

QGridLayout - Fixed Number of Rows and Columns

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 3 Posters 7.6k 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.
  • W Offline
    W Offline
    webzoid
    wrote on 8 Mar 2018, 11:52 last edited by
    #1

    Is there any way to force a QGridLayout to adopt a fixed number of rows and columns of equal size?

    I'm looking for a way of dragging a QWidget onto the layout, but then allowing it to be resized while snapping to the rows, columns to maintain a regular size.

    R 1 Reply Last reply 8 Mar 2018, 11:55
    0
    • W webzoid
      8 Mar 2018, 11:52

      Is there any way to force a QGridLayout to adopt a fixed number of rows and columns of equal size?

      I'm looking for a way of dragging a QWidget onto the layout, but then allowing it to be resized while snapping to the rows, columns to maintain a regular size.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 8 Mar 2018, 11:55 last edited by raven-worx 3 Aug 2018, 11:57
      #2

      @webzoid
      You could pre-insert fixed sized plain widgets into the grid layout. Those widgets then accept the drop events and place the widget inside them.
      I think thats the easiest solution without much "hacking".

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      3
      • W Offline
        W Offline
        webzoid
        wrote on 8 Mar 2018, 11:58 last edited by
        #3

        @raven-worx Thanks for your quick reply.

        This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?

        I don't mind having to do a bit of hacking - maybe a QWidget or custom QLayout to do the layout logic would be a more elegant solution?

        R 1 Reply Last reply 8 Mar 2018, 12:13
        0
        • W webzoid
          8 Mar 2018, 11:58

          @raven-worx Thanks for your quick reply.

          This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?

          I don't mind having to do a bit of hacking - maybe a QWidget or custom QLayout to do the layout logic would be a more elegant solution?

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 8 Mar 2018, 12:13 last edited by
          #4

          @webzoid said in QGridLayout - Fixed Number of Rows and Columns:

          This sounds ok so far, but, what would happen when I want to resize a widget to cover, say, 3 columns and 2 rows? What would happen to those pre-inserted widgets?

          I can't tell you straight from head, but just evaluate it with a small example. If the behavior is not as desired you will have to go with a custom layout. Which might be hard work though.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0
          • W Offline
            W Offline
            webzoid
            wrote on 8 Mar 2018, 12:17 last edited by
            #5

            @raven-worx Sorry, I didn't mean to put you on the spot... haha.

            I've actually been thinking about a custom layout and that it may not be so difficult. If I could specify a number of rows/columns then I can calculate height/width of each cell so that when a widget is resized, I can dynamically compute its size based on its column/row spans.

            I'm sure its not a trivial exercise mind but it seems a cute way of doing it. Google searches tell me that plenty of people have tried before but ultimately failed - maybe this is my time to shine!

            R 1 Reply Last reply 8 Mar 2018, 12:19
            0
            • W webzoid
              8 Mar 2018, 12:17

              @raven-worx Sorry, I didn't mean to put you on the spot... haha.

              I've actually been thinking about a custom layout and that it may not be so difficult. If I could specify a number of rows/columns then I can calculate height/width of each cell so that when a widget is resized, I can dynamically compute its size based on its column/row spans.

              I'm sure its not a trivial exercise mind but it seems a cute way of doing it. Google searches tell me that plenty of people have tried before but ultimately failed - maybe this is my time to shine!

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 8 Mar 2018, 12:19 last edited by
              #6

              @webzoid
              :)
              Difficulty is relative ;)

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • W Offline
                W Offline
                webzoid
                wrote on 8 Mar 2018, 15:03 last edited by
                #7

                Ok, so I'm mocking something up using a QTableWidget which, surprisingly, seems to be allowing me to do what I want so far. I can set up a fixed number of rows/columns and I can add widgets to a cell which then can be spanned across cols/rows.

                Drag and drop seems to be working nicely, just need to figure out the runtime widget resizing and spanning...

                1 Reply Last reply
                1
                • W Offline
                  W Offline
                  webzoid
                  wrote on 12 Mar 2018, 17:11 last edited by
                  #8

                  Is there any way of preventing a QWidget which has been bound to a QTableWidget cell (using the setCellWidget function) from being destroyed when calling removeCellWidget?

                  I'm wanting to move a QWidget to another cell, but, when dragging it to another cell (and subsequently calling setCellWidget), the "old" cell still holds a pointer to the widget even though it has been moved to another cell.

                  1 Reply Last reply
                  0
                  • W Offline
                    W Offline
                    webzoid
                    wrote on 13 Mar 2018, 09:39 last edited by
                    #9

                    Ok, so using a blood-and-thunder method, I've managed to achieve what I want but basically, its a crappy solution so I'm going to need to look at another way of doing things.

                    The QTableWidget is just far too slow when large numbers of rows/columns are used.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 13 Mar 2018, 09:46 last edited by
                      #10

                      setCellWidget is notoriously painfully slow. It's not a problem of QTableWidget
                      Are the widget the same type in all cells?

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      W 1 Reply Last reply 13 Mar 2018, 09:56
                      0
                      • V VRonin
                        13 Mar 2018, 09:46

                        setCellWidget is notoriously painfully slow. It's not a problem of QTableWidget
                        Are the widget the same type in all cells?

                        W Offline
                        W Offline
                        webzoid
                        wrote on 13 Mar 2018, 09:56 last edited by
                        #11

                        @VRonin Yeah, I appreciate that its an issue with the underlying implementation - my fumbling has had be digging into the QAbstractItemView class and implementing my own setIndexWidget function in a custom QTableView class.

                        Ultimately, the widgets dropped into the cells will have the same base class but the derivatives will be very different.

                        1 Reply Last reply
                        0

                        1/11

                        8 Mar 2018, 11:52

                        • Login

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