Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Set Column Width ot QTableView
QtWS25 Last Chance

[Solved] Set Column Width ot QTableView

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 5.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    compyler
    wrote on last edited by
    #1

    Hello,

    I have a QTableView which receives my custom model called PointSet (PointSet derives from QAbstractTableModel).
    Now I want to set different widths for the columns in my model. Therefore I have overwritten headerData() method like this:

    @QVariant PointSet::headerData(int section, Qt::Orientation orientation, int role) const
    {
    if(orientation == Qt::Horizontal) {

        if(role == Qt::DisplayRole) {        
            switch(section) {
                case 0:
                    return QString("Symbol");
                case 1:
                    return QString("Description");
            }
        }
        else if(role == Qt::SizeHintRole) {
             return QSize(300, 50);
        }
        
    }
    
    return QAbstractTableModel::headerData(section, orientation, role);
    

    }@

    When I execute this code, the column headers of my table indeed have a height of 50 pixels, but no column has a size of 300. The 300 seems to be ignored!
    Anyone knows why setting column width with my approach does not work?

    Thanks!

    1 Reply Last reply
    0
    • EddyE Offline
      EddyE Offline
      Eddy
      wrote on last edited by
      #2

      Hi,

      I would try calling "resizecolumtocontents":http://qt-project.org/doc/qt-5.0/qtwidgets/qtableview.html#resizeColumnToContents

      after setting the sizehint

      Qt Certified Specialist
      www.edalsolutions.be

      1 Reply Last reply
      0
      • C Offline
        C Offline
        compyler
        wrote on last edited by
        #3

        That really worked. Thanks a lot Eddy!:)

        1 Reply Last reply
        0
        • EddyE Offline
          EddyE Offline
          Eddy
          wrote on last edited by
          #4

          My pleasure.

          Could you please edit your first post and add [solved] to it.

          Qt Certified Specialist
          www.edalsolutions.be

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved