QTableView and column resize problem
I just switched my app from using a QTableWidget to a QTableView and suddenly I cannot get word wrap to shut off.
My tableview has 5 columns 4 of which are hidden.
Code:
myTable->setModel( myModel );
myTable->verticalHeader()->setVisible( false );
myTable->setShowGrid( false );
myTable->setWordWrap( false );
myTable->horizontalHeader()->setStretchLastSection( false );
myTable
->horizontalHeader
()->resizeSections
( QHeaderView::ResizeToContents );
//
// I have also tried, instead of calling resizeSections, the following
// mySequenceTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Interactive );
// mySequenceTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
myTable->horizontalHeader()->setSectionHidden( 2, true );
myTable->horizontalHeader()->setSectionHidden( 3, true );
myTable->horizontalHeader()->setSectionHidden( 4, true );
myTable->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
The problem is that when an extremely long string is displayed in column 1 the text is wrapped. I'm need either a scroll bar (or if the scrollbar is set to off, the ellipses.) But I am always getting a wrapped string. The example above gives me a small column that I can drag to widden and when the column with becomes bigger than the QTableView then I do get the horizontal scrollbar, but I need the column to make itself bigger when the data appears in the model.
I have tried a few dozen iterations of this and am getting the same result. Can anyone offer a suggestion?
Re: QTableView and column resize problem
Re: QTableView and column resize problem
Have tried all three...with the same problem.
I am using a delegate derived from QItemDelegate. My thinking is that if there is no solution I'll have to modify the delegate to modify the display string based on the QRect size of the cell.
Re: QTableView and column resize problem
Isn't that working like you want with ellipses?
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
model.
setStringList(QStringList() <<
"Very very long line of text" << "Very very long line of text"
<< "Very very long line of text"
<< "Very very long line of text");
view.setModel(&model);
view.horizontalHeader()->setStretchLastSection(true);
view.show();
return a.exec();
}
Re: QTableView and column resize problem
Your example does work for me. However, the difference is that I need for column 2 to show the elides and column 4 to be hidden. What I'm doing is more:
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
model.
setStringList(QStringList() <<
"Very very long line of text" << "Very very long line of text"
<< "short text"
<< "short text");
view.setModel(&model);
view.
horizontalHeader()->setResizeMode
( 0,
QHeaderView::Interactive);
view.
horizontalHeader()->setResizeMode
( 1,
QHeaderView::Stretch );
view.setSectionHidden( 2, true );
view.setSectionHidden( 3, true );
view.show();
return a.exec();
}
For me this approach (I'm using c++ not python) the table view INSISTS on wrapping the contents of the column. If I don't stretch the second column, the text is still wrapped, but the user can resize the column. However, it has a huge block of dead space. ( there is a reason that those columns are hidden. )
It's looking like my only option is to handle the elide myself, in the column's delegate.
Re: QTableView and column resize problem
Hi,
you code is not working, and for what you trying to test you need a proper model! So here we go
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
model.setItem(row, column, item);
}
}
view.setModel(&model);
view.
horizontalHeader()->setResizeMode
( 0,
QHeaderView::Interactive);
view.
horizontalHeader()->setResizeMode
( 1,
QHeaderView::Stretch );
view.horizontalHeader()->setSectionHidden( 2, true );
view.horizontalHeader()->setSectionHidden( 3, true );
view.show();
return a.exec();
}
This elides all like one suppose.