setColumnWidth() not working



  • This is just a test:

        ui->tableView->setColumnWidth(18, 60);
        for (int i = 0; i <= 20; ++i)
        {
            int j = ui->tableView->columnWidth(i);
            if (j > 0)
            {
                qDebug() << i << j;
            }
        }
    

    Output is:

    7 105
    8 105
    12 104
    18 104
    

    (Four columns are visible.) Can someone see what I'm doing wrong? Thanks.



  • Hi, if you're on Qt 5.11 or later it could be because of this feature


  • Qt Champions 2017

    Try doing that after you've populated the view with the data. At least that's what an old thread on QtCentre claims to have worked.



  • I tried the workaround mentioned in the article that hskoglund referenced; that didn't change anything. I've also already set the model on the table view prior to this.

    This line appears to have been the culprit:

        ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    

    Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.


  • Qt Champions 2017

    @mzimmers said in setColumnWidth() not working:

    Removing it allows the column resizing, but...then I don't get the stretch. I guess I can't have everything. I just wanted to make the first column a little wider, as its data contains more characters.

    Then you may want QHeaderView::setResizeMode with a QHeaderView::ResizeToContents argument, or QTableView::resizeColumnToContents.



  • @kshegunov that didn't work quite as I'd hoped:

    0_1542756101043_wifi.PNG

    I'm guessing that the problem now is that when the UI starts, the first row (not the header row) is unpopulated, so it uses the length of the header fields to set the column widths. Not sure what to do about this -- maybe call the resize routine again in a slot that is invoked when the model emits a dataChanged() signal?



  • If you want the first column wider set this column to be wider:
    ui->tableView->setColumnWidth(nfirst column is 0, expected width of the column);

    if you want to resize all the columns to customised size:

    auto model = new QStandardItemModel();
    ui->tableView->setModel(model);
    this ->setCentralWidget(ui->tableView);
    QHeaderView *headerView = new QHeaderView(Qt::Horizontal, this);
    ui->tableView->setHorizontalHeader(headerView);

    for(int i = 0; i < 20; i++)
    {
        model->setHorizontalHeaderItem(i, new QStandardItem(QString("Column %1").arg(i)));
    
        ui->tableView->setColumnWidth(i, 80);
        int j = ui->tableView->columnWidth(i);
        qDebug() << i << j;
    }
    

    Debuger data: column number width of the column
    0 80
    1 80
    2 80
    3 80
    4 80
    5 80
    6 80
    7 80
    8 80
    9 80
    10 80
    11 80
    12 80
    13 80
    14 80
    15 80
    16 80
    17 80
    18 80
    19 80
    ...and the view:

    0_1542757362096_Tab.png

    You can also to do only some column to be the same size.


  • Qt Champions 2017

    @mzimmers said in setColumnWidth() not working:

    maybe call the resize routine again in a slot that is invoked when the model emits a dataChanged() signal?

    Yes, that can be done, but it'd depend on how your model is structured. If it reloads itself every time you could connect it to the QAbstractItemModel::modelReset signal. That usually isn't the case though. Connecting to the data changed signal is possible, but if your table is large this may be rather expensive.



  • The table isn't too big (~30 columns, and probably at most 30 rows), but maybe this isn't a good idea. I can live with statically setting the widths. I realize that's not a good idea generally, but since I know with a high degree of precision what the contents will be, in this case it's probably OK.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.