Results 1 to 4 of 4

Thread: Resizing vertical header in QTableView

  1. #1
    Join Date
    Oct 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Resizing vertical header in QTableView

    Hello all,

    How to resize the vertical header of a QTableView to make it wide enough so it does not clip the text?

    I guess it must go via QHeaderView gained using the verticalHeader() method of QTableView. However what ever I try it does not have any effect.

    If the vertical header could resize itself to fit the contents as the columns do that would be just perfect.

    I see a lot about this issue on google around the time Qt4 came out, however I could not find a solution.

    Please help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing vertical header in QTableView

    The vertical header resizes to accommodate the row headings for me. Can you post a minimal compilable example showing the behaviour you are trying to change?
    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class View: public QTableView {
    5. Q_OBJECT
    6. public:
    7. View(QWidget *p = 0): QTableView(p) {
    8. connect(&t, SIGNAL(timeout()), this, SLOT(changeIt()));
    9. t.start(5000);
    10. }
    11. public slots:
    12. void changeIt() {
    13. model()->setHeaderData(2, Qt::Vertical, "Really long test");
    14. };
    15. private:
    16. QTimer t;
    17. };
    18.  
    19. int main(int argc, char *argv[])
    20. {
    21. QApplication app(argc, argv);
    22.  
    23. QStandardItemModel model(4, 4);
    24. for (int row = 0; row < 4; ++row) {
    25. for (int column = 0; column < 4; ++column) {
    26. QString("row %0, column %1").arg(row).arg(column)
    27. );
    28. model.setItem(row, column, item);
    29. }
    30. }
    31. // change before display
    32. model.setHeaderData(0, Qt::Vertical, "Test");
    33.  
    34. View v;
    35. v.setModel(&model);
    36. v.show();
    37.  
    38. // change after display
    39. model.setHeaderData(1, Qt::Vertical, "Long Test");
    40.  
    41. return app.exec();
    42. }
    43. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2010
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resizing vertical header in QTableView

    Hello and thanks for the response.

    I have a heavily styled widget with rounded borders and padding. The vertical header text is added automatically by when I insert a new row. These are numbers starting from 1.

    The single digit numbers cannot be seen. The header resizes if there is more then 9 rows - double digit header text -, which makes the single digit number visible and part of the second digit of the double digit ones. It works fine without styling.

    I use this code to insert the rows:

    Qt Code:
    1. QStandardItemModel * model =( ( QStandardItemModel * ) ( this->model() ) ); model->insertRows ( 0, 1 );
    2.  
    3. QBrush brush=QBrush(color);
    4. AddTextItem ( displayqueue_item.item1, ITEM1, 0, brush );
    5. AddPixmapItem ( "", displayqueue_item.item2, ITEM2, 0, brush );
    To copy to clipboard, switch view to plain text mode 

    As a workaround, I added an extra space to every header text after calling insertRow().

    Qt Code:
    1. for( int i=model->rowCount(); i>0; --i)
    2. model->setHeaderData(i-1, Qt::Vertical, QString("%1 ").arg(i));
    To copy to clipboard, switch view to plain text mode 

    This is now looks right until the style is changed again. Is this a bug in the QT resizing algorithm?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Resizing vertical header in QTableView

    Possibly, but you haven't really given us anything to go on. How is your heavily styled widget achieved; style sheets, global custom style, derived widget paint method ? Do you override QHeaderView and return a revised size hint? Do you style QHeaderView or just the QTableView (or whatever)?

Similar Threads

  1. Vertical Header For QTreeView
    By wirasto in forum Qt-based Software
    Replies: 1
    Last Post: 12th October 2010, 06:53
  2. Qtableview Vertical header order
    By Darthspawn in forum Qt Programming
    Replies: 4
    Last Post: 26th March 2010, 16:10
  3. No Sort Vertical Header
    By wirasto in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 11:54
  4. Replies: 3
    Last Post: 1st February 2008, 19:18
  5. Showing Icon in vertical header of a table
    By vishal.chauhan in forum Qt Programming
    Replies: 7
    Last Post: 15th January 2007, 11:44

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.