Results 1 to 6 of 6

Thread: QTableView and column resize problem

  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

    Qt Code:
    1. myTable = new QTableView( this );
    2. myTable->setModel( myModel );
    3.  
    4. myTable->verticalHeader()->setVisible( false );
    5. myTable->setEditTriggers( QAbstractItemView::NoEditTriggers );
    6. myTable->setShowGrid( false );
    7. myTable->setWordWrap( false );
    8. myTable->horizontalHeader()->setStretchLastSection( false );
    9. myTable->horizontalHeader()->resizeSections( QHeaderView::ResizeToContents );
    10. //
    11. // I have also tried, instead of calling resizeSections, the following
    12. // mySequenceTable->horizontalHeader()->setResizeMode( 0, QHeaderView::Interactive );
    13. // mySequenceTable->horizontalHeader()->setResizeMode( 1, QHeaderView::ResizeToContents );
    14.  
    15. myTable->horizontalHeader()->setSectionHidden( 2, true );
    16. myTable->horizontalHeader()->setSectionHidden( 3, true );
    17. myTable->horizontalHeader()->setSectionHidden( 4, true );
    18.  
    19. myTable->setSelectionBehavior( QAbstractItemView::SelectRows );
    20. myTable->setSelectionMode( QAbstractItemView::SingleSelection );
    21. myTable->setHorizontalScrollBarPolicy( Qt::ScrollBarAsNeeded );
    22. myTable->setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView and column resize problem

    You can use QTableView::resizeRowToContents() or QHeaderView::stretchLastSection or QHeaderView::setResizeMode.

    Which delegate do you use to display the text?

  3. #3
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableView and column resize problem

    Isn't that working like you want with ellipses?
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. model.setStringList(QStringList() << "Very very long line of text"
    8. << "Very very long line of text"
    9. << "Very very long line of text"
    10. << "Very very long line of text");
    11.  
    12. QTableView view;
    13. view.setModel(&model);
    14. view.horizontalHeader()->setStretchLastSection(true);
    15. view.show();
    16.  
    17. return a.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7. model.setStringList(QStringList() << "Very very long line of text"
    8. << "Very very long line of text"
    9. << "short text"
    10. << "short text");
    11.  
    12. QTableView view;
    13. view.setModel(&model);
    14. view.horizontalHeader()->setResizeMode( 0, QHeaderView::Interactive);
    15. view.horizontalHeader()->setResizeMode( 1, QHeaderView::Stretch );
    16. view.setSectionHidden( 2, true );
    17. view.setSectionHidden( 3, true );
    18. view.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    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.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default 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
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6.  
    7.  
    8. QStandardItemModel model(4, 4);
    9. for (int row = 0; row < 4; ++row) {
    10. for (int column = 0; column < 4; ++column) {
    11. QStandardItem *item = new QStandardItem(QString("(%1) Very very long line of text").arg(column));
    12. model.setItem(row, column, item);
    13. }
    14. }
    15.  
    16. QTableView view;
    17. view.setModel(&model);
    18. view.horizontalHeader()->setResizeMode( 0, QHeaderView::Interactive);
    19. view.horizontalHeader()->setResizeMode( 1, QHeaderView::Stretch );
    20. view.horizontalHeader()->setSectionHidden( 2, true );
    21. view.horizontalHeader()->setSectionHidden( 3, true );
    22. view.show();
    23.  
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 
    This elides all like one suppose.

Similar Threads

  1. QTableView with a column of images
    By elanari in forum Qt Programming
    Replies: 13
    Last Post: 7th February 2012, 12:25
  2. Qtableview column get increasing
    By maarvi in forum Newbie
    Replies: 0
    Last Post: 28th June 2011, 19:21
  3. Replies: 1
    Last Post: 8th May 2011, 23:13
  4. Replies: 2
    Last Post: 18th October 2010, 17:58
  5. Replies: 1
    Last Post: 1st May 2010, 23:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.