Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


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



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



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


Log in to reply