Results 1 to 4 of 4

Thread: QTableView Horizontal Header's Width

  1. #1
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTableView Horizontal Header's Width

    Users of my application are able to resize header's columns as they want.
    And resized widths of columns are to save in QSettings to restore user's favorite size next time. But something wrong with next code, please help:

    First of all, here is a part of constructor of MainWindow:
    Qt Code:
    1. eveModel = new EveModel(this);
    2. header = tableView->horizontalHeader();
    3. proxyModel->setSourceModel(eveModel);
    4. proxyModel->setDynamicSortFilter(true);
    5. header->setResizeMode(QHeaderView::Interactive);
    6. header->setStretchLastSection(true);
    7. tableView->setModel(proxyModel);
    To copy to clipboard, switch view to plain text mode 

    There is no effect from any methods like QHeaderView::resizeSection() or ...setSectionHidden(). But ...setStretchLastSection() is work. I don't understand why!

    Then headerData(), a part of EveModel:
    Qt Code:
    1. QVariant EveModel::headerData(int section, Qt::Orientation orientation, int role /* =Qt::DisplayRole */ ) const {
    2.  
    3. if (orientation == Qt::Horizontal) {
    4. if (role == Qt::DisplayRole)
    5. return m_headers->count() > section ? m_headers->at(section) : QAbstractTableModel::headerData(section, orientation, role);
    6. if (role == Qt::SizeHintRole) {
    7. return QSize(1, 30);
    8. }
    9. }
    10. else if (orientation == Qt::Vertical) {
    11. if (role == Qt::DisplayRole)
    12. return QVariant();
    13. }
    14. return QAbstractTableModel::headerData(section, orientation, role);
    15. }
    To copy to clipboard, switch view to plain text mode 

    In the code above I added "return QSize(1,30);" string, but it actually has influence only at header's height , not width (regardless of params).

    I think that I misunderstand the whole concept of such kind of operation in Qt. Would you please explain me?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView Horizontal Header's Width

    SizeHintRole is only a hint, it might not be followed. In general Qt will try to treat it as minimum width/height when arranging items. If you want to change the behaviour of headers, I suggest doing that after you apply the model, not before.

  3. The following user says thank you to wysota for this useful post:

    araglin (20th December 2008)

  4. #3
    Join Date
    Dec 2008
    Posts
    32
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Horizontal Header's Width

    Thank you!

    I've added
    Qt Code:
    1. header->setModel(eveModel);
    To copy to clipboard, switch view to plain text mode 
    and now it's going better!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView Horizontal Header's Width

    Simply add the model to the view (not to the header) before you modify the header. setModel() on the view sets the model on the header as well. You shouldn't do it manually.

  6. The following user says thank you to wysota for this useful post:

    araglin (21st December 2008)

Similar Threads

  1. spaning the headers in the QTableView
    By ru_core in forum Qt Programming
    Replies: 6
    Last Post: 11th June 2013, 13:17
  2. How to remove headers from QTableView.
    By rajeshs in forum Qt Programming
    Replies: 7
    Last Post: 18th November 2011, 20:00
  3. Width QTableView for QCombobox with multiple columns
    By visor_ua in forum Qt Programming
    Replies: 7
    Last Post: 21st June 2011, 10:54
  4. How to set QTableView width to width of horizontal header?
    By martinb0820 in forum Qt Programming
    Replies: 0
    Last Post: 2nd December 2008, 20:51
  5. set the width of the columns of QTableView
    By zhanglr in forum Qt Programming
    Replies: 6
    Last Post: 31st July 2008, 15:29

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.