QTableWidget contents height issue
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.
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?
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) );
AndyBrice last edited by 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.
JonB last edited by JonB
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.
@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.
void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
ah its protected.
JonB last edited by
I'm not a C++ expert, as you know, but inheritance hierachy
QAbstractScrollArea, so doesn't that mean he can override a
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.
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.
That was what i feared.
I wondered if we used
and always had an empty last row if that could work.
However, even if Delegate draws nothing, the cell borders would still show.