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. Need help resizing QTableView column
Forum Updated to NodeBB v4.3 + New Features

Need help resizing QTableView column

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 853 Views 1 Watching
  • 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.
  • T Offline
    T Offline
    thebeast44
    wrote on 10 Feb 2014, 08:05 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Feb 2014, 22:32 last edited by
      #2

      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)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      2/2

      10 Feb 2014, 22:32

      • Login

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