Solved QTableView Stretch Headers but keep Interactive
-
I am struggling triying to accomodate my QTableView to make it easy to the user.
This functions works as i really need:
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
The issue with this is that the headers are not user adjustable anymore, are completely frozzen.I know we also have this function which allows the user to adjust the Headers, but after using it, the headers go back as if i had not used QHeaderView::Stretch before:
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
I have really looked for this on many websites with no luck of finding a understandable answer for my level.Thanks!
-
Hi Guys, after Struggling like 3 hours ive got it:
for(int c = 0;c<=4;c++){ ui->tableView->horizontalHeader()->resizeSection(c, 150); } ui->tableView->horizontalHeader()->setStretchLastSection(true);
Set first the width of each column to match the QTableView size and then Stretch the last section, thanks a lot!
-
@Adan895 said in QTableView Stretch Headers but keep Interactive:
That is what https://doc.qt.io/qt-5/qheaderview.html#ResizeMode-enum says,Interactive
(the user can resize) andStretch
(widths are set to fill view) are different from/incompatible with each other. So perhaps you will have to explain exactly what you mean by your "Stretch Headers but keep Interactive"? -
Hi and welcome to devnet,
Are you triggering a resize to content size somehere in your code ?
-
@JonB Hi, i want to Stretch the headers to the size of the QTableview, but allow the user to adjust them if necessary.
-
@Adan895
So I think you should leave the mode on interactive and do a once-off stretch to get going?setSectionResizeMode(QHeaderView::Interactive); resizeSections(QHeaderView::Stretch);
-
@SGaist Hello, no, i am just stretching the headers to the QTableview size using QHeaderView::Stretch, but if i do that the user cannot resize the Headers if necessary manually.
-
-
@Adan895
Have you tried/noted the difference betweensetSectionResizeMode()
andresizeSections()
, as per the suggested code? -
@JonB @Christian-Ehrlicher @SGaist Thanks, what you are suggesting sounds logical, i think it will work, once i arrive home later i will try it and i ll let you know.
-
@JonB @SGaist @Christian-Ehrlicher Hi guys, after triying using your advice, this is what happened, its not stretching.
-
Hi Guys, after Struggling like 3 hours ive got it:
for(int c = 0;c<=4;c++){ ui->tableView->horizontalHeader()->resizeSection(c, 150); } ui->tableView->horizontalHeader()->setStretchLastSection(true);
Set first the width of each column to match the QTableView size and then Stretch the last section, thanks a lot!