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 horizontalHeader()->setStretchLastSection(true) and resizeColumnsToContents
Forum Updated to NodeBB v4.3 + New Features

QTableView horizontalHeader()->setStretchLastSection(true) and resizeColumnsToContents

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 2.1k 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.
  • A Offline
    A Offline
    Abdurahman_Gulamkadirov
    wrote on last edited by Abdurahman_Gulamkadirov
    #1

    Hi there.
    I'm confusing in this situation: There's a QTableView in GUI. And it takes my custom table model. When GUI is shown, my program loads data from rest api to the table:

    QJsonArray arr = QJsonDocument::fromJson(reply->readAll()).array();
           for(int i = 0; i < arr.size(); ++i)
           {
               QJsonObject obj = arr[i].toObject();
               modelInvoiceItem->insertProduct(obj);
           }
           ui->tableInvoiceItem->resizeColumnsToContents();
           ui->tableInvoiceItem->horizontalHeader()->setStretchLastSection(true);
    

    but the last horizontal header doesn't stretch.

    JonBJ 1 Reply Last reply
    0
    • A Abdurahman_Gulamkadirov

      @JonB yes, it's in a layout

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @Abdurahman_Gulamkadirov
      Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment out resizeColumnsToContents(); while you see how it behaves?

      Also, I don't think resizeColumnsToContents(); has any effect until the table view is shown/resized. I think it's usually used in widget's resize or resizeEvent.

      A 1 Reply Last reply
      1
      • A Abdurahman_Gulamkadirov

        Hi there.
        I'm confusing in this situation: There's a QTableView in GUI. And it takes my custom table model. When GUI is shown, my program loads data from rest api to the table:

        QJsonArray arr = QJsonDocument::fromJson(reply->readAll()).array();
               for(int i = 0; i < arr.size(); ++i)
               {
                   QJsonObject obj = arr[i].toObject();
                   modelInvoiceItem->insertProduct(obj);
               }
               ui->tableInvoiceItem->resizeColumnsToContents();
               ui->tableInvoiceItem->horizontalHeader()->setStretchLastSection(true);
        

        but the last horizontal header doesn't stretch.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Abdurahman_Gulamkadirov
        I don't know, but does it make any difference if you swap the order of the last two lines?

        A 1 Reply Last reply
        0
        • JonBJ JonB

          @Abdurahman_Gulamkadirov
          I don't know, but does it make any difference if you swap the order of the last two lines?

          A Offline
          A Offline
          Abdurahman_Gulamkadirov
          wrote on last edited by Abdurahman_Gulamkadirov
          #3

          @JonB No, it didn't help

          JonBJ 1 Reply Last reply
          0
          • A Abdurahman_Gulamkadirov

            @JonB No, it didn't help

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @Abdurahman_Gulamkadirov
            Like all other widgets, your QTableView is on a layout, isn't it? Nothing will work unless it is.

            A 1 Reply Last reply
            1
            • JonBJ JonB

              @Abdurahman_Gulamkadirov
              Like all other widgets, your QTableView is on a layout, isn't it? Nothing will work unless it is.

              A Offline
              A Offline
              Abdurahman_Gulamkadirov
              wrote on last edited by
              #5

              @JonB yes, it's in a layout

              JonBJ 1 Reply Last reply
              0
              • A Offline
                A Offline
                Abdurahman_Gulamkadirov
                wrote on last edited by
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • A Abdurahman_Gulamkadirov

                  @JonB yes, it's in a layout

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #7

                  @Abdurahman_Gulamkadirov
                  Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment out resizeColumnsToContents(); while you see how it behaves?

                  Also, I don't think resizeColumnsToContents(); has any effect until the table view is shown/resized. I think it's usually used in widget's resize or resizeEvent.

                  A 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Abdurahman_Gulamkadirov
                    Can only suggest: look at your setting for enum QHeaderView::ResizeMode on the whole view/individual sections., and maybe comment out resizeColumnsToContents(); while you see how it behaves?

                    Also, I don't think resizeColumnsToContents(); has any effect until the table view is shown/resized. I think it's usually used in widget's resize or resizeEvent.

                    A Offline
                    A Offline
                    Abdurahman_Gulamkadirov
                    wrote on last edited by Abdurahman_Gulamkadirov
                    #8

                    @JonB I tested it after inserting row:

                    qInfo() << ui->tableInvoiceItem->horizontalHeader()->sectionResizeMode(
                                    ui->tableInvoiceItem->horizontalHeader()->count() - 1);
                    

                    it shows QHeaderView::Stretch. But the last section doesn't fill extra spaces. When I resize window, it automatically fills.

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Abdurahman_Gulamkadirov
                      wrote on last edited by Abdurahman_Gulamkadirov
                      #9

                      @JonB setting ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); helped, thanks a lot!

                      1 Reply Last reply
                      1

                      • Login

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