QTablewidget horizontalscrollbar should only appear when text is too large
-
Hello Team,
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?
-
Hi,
Looks like you want to set the widget minimum size so that it's at least as big as the columns sizes.
-
Hi,
Looks like you want to set the widget minimum size so that it's at least as big as the columns sizes.
@SGaist
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...
-
Then you should not set fixed column widths...
@Christian-Ehrlicher :-
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.?? -
@Christian-Ehrlicher :-
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.??@ankit-thakar
Assuming you mean/are happy with a scrollbar around the wholeQTableWidget
, not just somehow on an individual column or cell, put the whole table inside aQScrollArea
. I think scroll areas like a layout, you have to actually doQScollArea -> QLayout -> QTableWidget
. -
@ankit-thakar said in QTablewidget horizontalscrollbar should only appear when text is too large:
then what should I write such that whenever text is too big horizontal scrollbar appears automatically
Are you looking for QHeaderView::setSectionResizeMode() ?
-
@ankit-thakar said in QTablewidget horizontalscrollbar should only appear when text is too large:
then what should I write such that whenever text is too big horizontal scrollbar appears automatically
Are you looking for QHeaderView::setSectionResizeMode() ?
@Christian-Ehrlicher
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. -
@ankit-thakar
Assuming you mean/are happy with a scrollbar around the wholeQTableWidget
, not just somehow on an individual column or cell, put the whole table inside aQScrollArea
. I think scroll areas like a layout, you have to actually doQScollArea -> QLayout -> QTableWidget
.@JonB :-
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...
-
Did you read my post? Use the correct resize mode...
@Christian-Ehrlicher :-
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.ui->MytableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
But didn't worked.
-
@Christian-Ehrlicher :-
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.ui->MytableWidget->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
But didn't worked.
Any update on this???
-
@ankit-thakar said in QTablewidget horizontalscrollbar should only appear when text is too large:
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.