Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QTablewidget horizontalscrollbar should only appear when text is too large
I am working on qt 4.8 and using QTableWidget.
There are 2 columns in my table, in second column text can be large sometimes.
So I set columnwidth to 870. See below code
ui->MytableWidget->setColumnCount(2); ui->MytableWidget->setHorizontalHeaderLabels(QStringList() << "Number" << "Summary"); ui->MytableWidget->horizontalHeaderItem(0)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); ui->MytableWidget->horizontalHeaderItem(1)->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter); ui->MytableWidget->horizontalHeader()->setStretchLastSection(true); ui->MytableWidget->setColumnWidth(0,130); ui->MytableWidget->setColumnWidth(1,870);
Now the problem is that in my table by default horizontal scroll bar is coming because of columnwidth 870.
What I want is that it should only come when my text is too large (i.e. it is not fitting in the gui).
How can I achieve this?
Looks like you want to set the widget minimum size so that it's at least as big as the columns sizes.
Not exactly, what i want is that by default horizontal scrollbar should not appear. But if text size is too big so that it can't fit in gui then in that case. Scroll bar should appear.
If i set my minimum widget size as per your suggestion then my string will be displayed in small fonts.
Then you should not set fixed column widths...
Exactly, I am totally agree with you.
Let's suppose i haven't set columnwidth, then what should I write such that whenever text is too big horizontal scrollbar appears automatically. So that, user can see whole string.??
JonB last edited by JonB
Assuming you mean/are happy with a scrollbar around the whole
QTableWidget, not just somehow on an individual column or cell, put the whole table inside a
QScrollArea. I think scroll areas like a layout, you have to actually do
QScollArea -> QLayout -> QTableWidget.
then what should I write such that whenever text is too big horizontal scrollbar appears automatically
Are you looking for QHeaderView::setSectionResizeMode() ?
JonB last edited by
As I understand that, it will not introduce scroll bars. The way the OP phrases the question "when my text is too large (i.e. it is not fitting in the gui)", I take that to mean he does want a scrollbar when the text is too wide to fit into the whole UI.
Ok here it is, the description with images.
When I run my code by default it is coming like this. In which by default horizontal scroll bar is coming. (Obviously because I set column width).
What I want is that, by default horizontal scroll bar should not come. so, for that what I did is that I removed the code set column width. But after that if the string is too big at that time, in UI whole string is not displayed. (Please see below image).
In this case only I want horizontal scroll bar. How can I achieve that?
Did you read my post? Use the correct resize mode...
Yes, As I am working with Qt 4.8 QHeaderView::setSectionResizeMode() is not available instead of that I tried setResizeMode which is available in Qt 4.8 as below.
But didn't worked.
Any update on this???
Any update on this???
ResizeToContens works fine for me - if it does not work for you provide a minimal, compilable example. And upgrade to a supported version.