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. QTableWidget contents height issue
Forum Updated to NodeBB v4.3 + New Features

QTableWidget contents height issue

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 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.
  • AndyBriceA Offline
    AndyBriceA Offline
    AndyBrice
    wrote on last edited by
    #1

    I am dynamically adding a QPushButton to the bottom of a QTableWidget contents. It is a convenient place for the user to click to add a new row.

    0_1555066876396_2019-04-12_11-59-06.png

    I haven't used a layout. I just move the button as the user adds and removes rows. It works fine when there are only a few rows. But when there are too many rows to see at once you can't scroll down far enough to see the button.

    Is there a simple way to tell the QTableWidget that the contents height is a button height greater than the table height?

    --
    Andy Brice
    http://www.hyperplan.com

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

      Hi
      Maybe something like. very fast made. so need some tweaking.

          QWidget *viewport = ui->tableWidget->viewport();
          QPushButton *button = new QPushButton("+", ui->tableWidget);
          button->move(0, ui->tableWidget->height() - button->height() );
          viewport->setMaximumHeight( ui->tableWidget->height() - ( button->height() * 2) );
      

      alt text

      1 Reply Last reply
      2
      • AndyBriceA Offline
        AndyBriceA Offline
        AndyBrice
        wrote on last edited by AndyBrice
        #3

        Thanks, but I was looking for something where the button sat underneath the last row and scrolled with the last row. Rather than staying fixed at the bottom of the viewport. I didn't make that clear. Sorry.

        JonBJ 1 Reply Last reply
        0
        • AndyBriceA AndyBrice

          Thanks, but I was looking for something where the button sat underneath the last row and scrolled with the last row. Rather than staying fixed at the bottom of the viewport. I didn't make that clear. Sorry.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @AndyBrice
          I understand what you would prefer. I don't the answer as to whether you can do that neatly (will keep an eye open).

          But just to say: if you do not find any other solution, you could perhaps use @mrjj's principle. You want to alter so that the extra button/height is only shown when the final row is in view, or has scrolled to the bottom (not the top), or similar, which I believe you can detect(?) , else it's not to be there. Just a thought....

          Your question has been asked before, and I'm not finding much to properly answer it using resize/scrolling if that's possible. You might like to have a read through https://codereview.stackexchange.com/questions/125622/insert-new-row-to-a-qtableview-by-double-clicking-the-last-row, https://stackoverflow.com/questions/1628126/in-qt-create-a-table-with-an-blank-editable-row, for ideas about how others seem to have addressed this.

          AndyBriceA 1 Reply Last reply
          2
          • JonBJ JonB

            @AndyBrice
            I understand what you would prefer. I don't the answer as to whether you can do that neatly (will keep an eye open).

            But just to say: if you do not find any other solution, you could perhaps use @mrjj's principle. You want to alter so that the extra button/height is only shown when the final row is in view, or has scrolled to the bottom (not the top), or similar, which I believe you can detect(?) , else it's not to be there. Just a thought....

            Your question has been asked before, and I'm not finding much to properly answer it using resize/scrolling if that's possible. You might like to have a read through https://codereview.stackexchange.com/questions/125622/insert-new-row-to-a-qtableview-by-double-clicking-the-last-row, https://stackoverflow.com/questions/1628126/in-qt-create-a-table-with-an-blank-editable-row, for ideas about how others seem to have addressed this.

            AndyBriceA Offline
            AndyBriceA Offline
            AndyBrice
            wrote on last edited by
            #5

            @JonB Thanks. I have managed to get it to add a row if they Tab on the last cell. That works fine. The issue here is how to tell the QTableWidget that the scrolling contents are slightly bigger than the table header + rows. I Googled quite a bit and couldn't find the answer.

            mrjjM 1 Reply Last reply
            0
            • AndyBriceA AndyBrice

              @JonB Thanks. I have managed to get it to add a row if they Tab on the last cell. That works fine. The issue here is how to tell the QTableWidget that the scrolling contents are slightly bigger than the table header + rows. I Googled quite a bit and couldn't find the answer.

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

              @AndyBrice
              Hi
              What about
              void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
              ah its protected.

              JonBJ AndyBriceA 2 Replies Last reply
              0
              • mrjjM mrjj

                @AndyBrice
                Hi
                What about
                void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
                ah its protected.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @mrjj
                I'm not a C++ expert, as you know, but inheritance hierachy QTableWidget <- QTableView <- QAbstractItemView <- QAbstractScrollArea, so doesn't that mean he can override a protected from there?

                mrjjM 1 Reply Last reply
                0
                • JonBJ JonB

                  @mrjj
                  I'm not a C++ expert, as you know, but inheritance hierachy QTableWidget <- QTableView <- QAbstractItemView <- QAbstractScrollArea, so doesn't that mean he can override a protected from there?

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

                  @JonB
                  Indeed it does.
                  so maybe a custom QAbstractScrollArea and setViewport could work.
                  but never tried it. Also still now sure it would scroll with last row.

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @AndyBrice
                    Hi
                    What about
                    void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
                    ah its protected.

                    AndyBriceA Offline
                    AndyBriceA Offline
                    AndyBrice
                    wrote on last edited by
                    #9

                    @mrjj said in QTableWidget contents height issue:

                    setViewportMargins(int left, int top, int right, int bottom)

                    I can call setViewportMargins() from my QTableWidget derived class. It doesn't seem to make any difference. I still can't scroll down any further than the last row.

                    mrjjM 1 Reply Last reply
                    0
                    • AndyBriceA AndyBrice

                      @mrjj said in QTableWidget contents height issue:

                      setViewportMargins(int left, int top, int right, int bottom)

                      I can call setViewportMargins() from my QTableWidget derived class. It doesn't seem to make any difference. I still can't scroll down any further than the last row.

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

                      @AndyBrice
                      That was what i feared.

                      I wondered if we used
                      https://doc.qt.io/qt-5/qabstractitemview.html#setItemDelegateForRow
                      and always had an empty last row if that could work.
                      However, even if Delegate draws nothing, the cell borders would still show.

                      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