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?
--
Andy Brice
http://www.hyperplan.com -
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) );
-
@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.
-
@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.
-
@AndyBrice
Hi
What about
void QAbstractScrollArea::setViewportMargins(int left, int top, int right, int bottom)
ah its protected. -
@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.
-
@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.