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. Class derivated from QTableView, setcolumnwidth(col,width) doesn't do anything
Qt 6.11 is out! See what's new in the release blog

Class derivated from QTableView, setcolumnwidth(col,width) doesn't do anything

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 1 Posters 1.3k 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.
  • S Offline
    S Offline
    stephane78
    wrote on last edited by stephane78
    #1

    Hi,
    I have an object tableview from a class derivated from QTableView and SetColumnWidth(col,size) doesn't do anything.tableview has always the same size and each column has the same size and I cannot change that with setcolumnwidth.
    Is it because I use a custom delegate to paint one column and in the case of other columns I call ```
    QStyledItemDelegate::paint(painter, option, index);

    ? 
    What Can I do to change the size of each column ?
    tableview is inside a QGroupbox.and I use here Qt5.6.0.
    S 1 Reply Last reply
    0
    • S stephane78

      Hi,
      I have an object tableview from a class derivated from QTableView and SetColumnWidth(col,size) doesn't do anything.tableview has always the same size and each column has the same size and I cannot change that with setcolumnwidth.
      Is it because I use a custom delegate to paint one column and in the case of other columns I call ```
      QStyledItemDelegate::paint(painter, option, index);

      ? 
      What Can I do to change the size of each column ?
      tableview is inside a QGroupbox.and I use here Qt5.6.0.
      S Offline
      S Offline
      stephane78
      wrote on last edited by
      #2

      I have put

      else if (role == Qt::SizeHintRole) {
              switch(nColumn)
              {
                  case 0: return QSize(40,40);
                  case 1: return QSize(50,40);
                  case 2: return QSize(50,40);
                  case 3: return QSize(130,40);
                  case 4: return QSize(130,40);
                  case 5: return QSize(40,40);
                  case 6: return QSize(40,40);
              default:
                  break;
              }
      
      

      in the data and Headerdata method of my model class and it is better now because tableview is not too large anymore, but each column has always the same width although....

      S 1 Reply Last reply
      0
      • S stephane78

        I have put

        else if (role == Qt::SizeHintRole) {
                switch(nColumn)
                {
                    case 0: return QSize(40,40);
                    case 1: return QSize(50,40);
                    case 2: return QSize(50,40);
                    case 3: return QSize(130,40);
                    case 4: return QSize(130,40);
                    case 5: return QSize(40,40);
                    case 6: return QSize(40,40);
                default:
                    break;
                }
        
        

        in the data and Headerdata method of my model class and it is better now because tableview is not too large anymore, but each column has always the same width although....

        S Offline
        S Offline
        stephane78
        wrote on last edited by
        #3

        I have put that :

        if (qobject_cast<QHeaderView *>(parent()) != 0)
            {
             
                if ((index.column()==n_file)||(index.column()==n_execinfo))
                {
        
                width = 150;
                height =20;
                return QSize(width,height);
                }
                else return QSize(40,20);
            }
            else if (qobject_cast<QTableView *>(parent()) != 0)
            {
                QTableView *v = qobject_cast<QTableView *>(parent());
                width = v->columnWidth(index.column());
                height=v->rowHeight(0);
                return QSize(width,height);
        }
        

        in the overloading of the method sizehint of my delegate but it is useless...

        S 1 Reply Last reply
        0
        • S stephane78

          I have put that :

          if (qobject_cast<QHeaderView *>(parent()) != 0)
              {
               
                  if ((index.column()==n_file)||(index.column()==n_execinfo))
                  {
          
                  width = 150;
                  height =20;
                  return QSize(width,height);
                  }
                  else return QSize(40,20);
              }
              else if (qobject_cast<QTableView *>(parent()) != 0)
              {
                  QTableView *v = qobject_cast<QTableView *>(parent());
                  width = v->columnWidth(index.column());
                  height=v->rowHeight(0);
                  return QSize(width,height);
          }
          

          in the overloading of the method sizehint of my delegate but it is useless...

          S Offline
          S Offline
          stephane78
          wrote on last edited by
          #4

          I think I should use a QTableWidget without delegate and with multiple inheritance to set the width of each column.....

          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