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. QTableView fit to size but keep last one fully stretched
Forum Updated to NodeBB v4.3 + New Features

QTableView fit to size but keep last one fully stretched

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 385 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.
  • T Offline
    T Offline
    tilz0R
    wrote on last edited by tilz0R
    #1

    I have a QTableView and custom model, that sets the background role. Colors are different for odd/even rows.

    Table configuration

        ui->table->horizontalHeader()->setDefaultAlignment(Qt::AlignmentFlag::AlignLeft);
        ui->table->horizontalHeader()->setStretchLastSection(true);
        ui->table->resizeColumnsToContents();
        ui->table->resizeRowsToContents();
        ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
        ui->table->verticalHeader()->setVisible(true);
        ui->table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::Custom);
        ui->table->verticalHeader()->setDefaultSectionSize(16);
        ui->table->setShowGrid(false);
    

    When table is popped with data, and I press a custom button to execute clicked action, code is executed:

        ui->table_can_logger->resizeColumnsToContents();
        ui->table->resizeRowsToContents();
        ui->table->horizontalHeader()->setStretchLastSection(true);
    

    Table fits to size, but last column does not show background anymore:
    d3c4bc7d-665b-4f78-98d7-80300e03dd0f-image.png

    Unless I go above spacer on the right of text Data and try to drag it. Then I immediately get:
    a4d1844a-e9fc-44e3-859d-3f13a290c122-image.png

    How can I programmatically keep the last column to stretch as on above picture?

    Christian EhrlicherC 1 Reply Last reply
    0
    • T tilz0R

      I have a QTableView and custom model, that sets the background role. Colors are different for odd/even rows.

      Table configuration

          ui->table->horizontalHeader()->setDefaultAlignment(Qt::AlignmentFlag::AlignLeft);
          ui->table->horizontalHeader()->setStretchLastSection(true);
          ui->table->resizeColumnsToContents();
          ui->table->resizeRowsToContents();
          ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
          ui->table->verticalHeader()->setVisible(true);
          ui->table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::Custom);
          ui->table->verticalHeader()->setDefaultSectionSize(16);
          ui->table->setShowGrid(false);
      

      When table is popped with data, and I press a custom button to execute clicked action, code is executed:

          ui->table_can_logger->resizeColumnsToContents();
          ui->table->resizeRowsToContents();
          ui->table->horizontalHeader()->setStretchLastSection(true);
      

      Table fits to size, but last column does not show background anymore:
      d3c4bc7d-665b-4f78-98d7-80300e03dd0f-image.png

      Unless I go above spacer on the right of text Data and try to drag it. Then I immediately get:
      a4d1844a-e9fc-44e3-859d-3f13a290c122-image.png

      How can I programmatically keep the last column to stretch as on above picture?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @tilz0R said in QTableView fit to size but keep last one fully stretched:

      How can I programmatically keep the last column to stretch as on above picture?

      Don't call resizeColumnsToContents() for the last column

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      T 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @tilz0R said in QTableView fit to size but keep last one fully stretched:

        How can I programmatically keep the last column to stretch as on above picture?

        Don't call resizeColumnsToContents() for the last column

        T Offline
        T Offline
        tilz0R
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks - that worked:

            QAbstractItemModel* tableModel = ui->table->model();
        
            /* Fit to size all but last item */
            ui->table->horizontalHeader()->setStretchLastSection(true);
            for (int i = 0; i < tableModel->columnCount() - 1; ++i) {
                ui->table->resizeColumnToContents(i);
            }
            ui->table->resizeRowsToContents();
        
        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