Need help resizing QTableView column



  • Hi,

    I've got a QTableView with two columns.

    First column has the page number (as QString)
    Second column has the actual text.

    Here's my current code

    @
    void SearchWidget::setSearchResult(QList<SearchResultItem> &list) {
    int count = list.size();
    QStandardItemModel *model = new QStandardItemModel(count, 2);
    for (int row = 0; row < count; row++) {
    SearchResultItem resultItem = list.at(row);
    QStandardItem *page = new QStandardItem(QString::number(resultItem.pageIndex));
    QStandardItem *text = new QStandardItem(resultItem.searchedText);

            model->setItem(row + 1, 0, page);
            model->setItem(row + 1, 1, text);
    
            page->setData(resultItem.pageIndex, RESULT_ITEM_PAGE);
            text->setData(resultItem.pageIndex, RESULT_ITEM_PAGE);
        }
        SearchResultItem resultItem = list.at(count - 1);
        QStandardItem *page = new QStandardItem(QString::number(resultItem.pageIndex));
        QStandardItem *text = new QStandardItem(resultItem.searchedText);
        model->setItem(0, 0, page);
        model->setItem(0, 1, text);
    }
    
    ui->ResultList->setModel(model);
    ui->ResultList->setShowGrid(false);
    ui->ResultList->resizeColumnToContents(0);
    ui->ResultList->horizontalHeader()->setStretchLastSection(true);
    
    if (list.size() != 0) {
        model->removeRow(0);
    }
    

    }
    @

    Here's the problem I had:

    When I have small amount of search results, the page column gets resized correctly,
    but when I have thousands of them the page column is too small to fit the page number strings.

    After bit of googling, I concluded that it might be using first 256 items to do the resize.
    I've done more googling and tried many different methods but nothing really helped.

    So my temporary fix in the code above, is to add the last element (which has longest page number string) in row 0 before calling 'resizeColumnToContents(0);' then removing it.

    This gets me the result I need, but I was wondering if there's a better (or correct) way to handle this.

    Thank you


  • Lifetime Qt Champion

    Hi,

    Since this behavior is probably related to the current implementation of the resizing. You should ask this question on the interest mailing list, you'll find there Qt's developers/maintainers (this forum is more user oriented)


Log in to reply
 

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