PDA

View Full Version : QTableView and column resize problem



winkle98
24th February 2012, 00:11
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.



myTable = new QTableView( this );
myTable->setModel( myModel );

myTable->verticalHeader()->setVisible( false );
myTable->setEditTriggers( QAbstractItemView::NoEditTriggers );
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->setSelectionBehavior( QAbstractItemView::SelectRows );
myTable->setSelectionMode( QAbstractItemView::SingleSelection );
myTable->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
myTable->setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );

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?

Lykurg
24th February 2012, 06:48
You can use QTableView::resizeRowToContents() or QHeaderView::stretchLastSection or QHeaderView::setResizeMode.

Which delegate do you use to display the text?

winkle98
24th February 2012, 21:50
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.

Lykurg
25th February 2012, 07:13
Isn't that working like you want with ellipses?

#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStringListModel model;
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");

QTableView view;
view.setModel(&model);
view.horizontalHeader()->setStretchLastSection(true);
view.show();

return a.exec();
}

winkle98
28th February 2012, 01:33
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:


#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStringListModel model;
model.setStringList(QStringList() << "Very very long line of text"
<< "Very very long line of text"
<< "short text"
<< "short text");

QTableView view;
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.

Lykurg
28th February 2012, 06:51
Hi,

you code is not working, and for what you trying to test you need a proper model! So here we go
#include <QtGui>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);


QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(QString("(%1) Very very long line of text").arg(column));
model.setItem(row, column, item);
}
}

QTableView view;
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.