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. Sizing QTableview to its content

Sizing QTableview to its content

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.6k Views 3 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.
  • B Offline
    B Offline
    Bert59
    wrote on last edited by
    #1

    Hello,

    I have a QTableView which content (nb of columns) can change programatricaly. I would like to adjust the size of the tableview to the content.
    Below you can see the result I have so far

    f53a1100-9e53-4f17-9105-d780c9664399-image.png

    In Qt Creator it looks like below
    da7eb74b-f8ad-465b-93af-69a262f55b1f-image.png

    How can I adjust the size of the tableview to fit to the content?
    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You have to resize it yourself by requesting its sizeHint after you modify the number of columns.

      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
      2
      • B Offline
        B Offline
        Bert59
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bert59
          wrote on last edited by
          #4

          Thank you for your feedback.
          I tried the following but is not working.

              model.insertRows(0, 4);
              model.insertColumns(0, 4);
          
              QSize size = ui->table1->sizeHint();
              size.setWidth(350);
          

          Can you give me more details?

          jsulmJ 1 Reply Last reply
          0
          • B Bert59

            Thank you for your feedback.
            I tried the following but is not working.

                model.insertRows(0, 4);
                model.insertColumns(0, 4);
            
                QSize size = ui->table1->sizeHint();
                size.setWidth(350);
            

            Can you give me more details?

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Bert59 said in Sizing QTableview to its content:

            size.setWidth(350);

            What do expect this to do? It just changes width in QSize instance "size" nothing more - it does not resize anything.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • B Offline
              B Offline
              Bert59
              wrote on last edited by
              #6

              I try to resize a tableview to its content as described in the first post in this topic

              JonBJ 1 Reply Last reply
              0
              • B Bert59

                I try to resize a tableview to its content as described in the first post in this topic

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

                @Bert59
                What method are you calling to apply the newly calculated size to the tableview?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bert59
                  wrote on last edited by
                  #8

                  I would like that a tableview keeps the size of it's content.
                  If I add columns the tableview should expand to show all columns as long as it is smaller than the window.
                  If I resize the window the tableview should keep the same size and should stay centered in the window.
                  For testing the possibility of setting the width of the tableview to a given value I tried the following code:

                  ui->table1->setModel(&model); //table1 is the name of the tableview
                  model.insertRows(0, 4);
                  model.insertColumns(0, 4);

                  int size = ui->table1->sizeHint().width();
                  qInfo() << size; //Result is 256
                  
                  ui->table1->sizeHint().setWidth(350);
                  size = ui->table1->sizeHint().width();
                  qInfo() << size; //Result remains 256
                  

                  I couldn' find in the documentation how to achive this behaviour.
                  I hope this clarfies what I'm looking for.

                  JonBJ 1 Reply Last reply
                  0
                  • B Bert59

                    I would like that a tableview keeps the size of it's content.
                    If I add columns the tableview should expand to show all columns as long as it is smaller than the window.
                    If I resize the window the tableview should keep the same size and should stay centered in the window.
                    For testing the possibility of setting the width of the tableview to a given value I tried the following code:

                    ui->table1->setModel(&model); //table1 is the name of the tableview
                    model.insertRows(0, 4);
                    model.insertColumns(0, 4);

                    int size = ui->table1->sizeHint().width();
                    qInfo() << size; //Result is 256
                    
                    ui->table1->sizeHint().setWidth(350);
                    size = ui->table1->sizeHint().width();
                    qInfo() << size; //Result remains 256
                    

                    I couldn' find in the documentation how to achive this behaviour.
                    I hope this clarfies what I'm looking for.

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

                    @Bert59
                    What I/ @jsulm meant was: you can't just alter the QSize returned by a sizeHint() method and expect that to alter a widget's size hint!

                    You were supposed to use methods like QWidget::setMinimumSizeHint() or QWidget::resize()....

                    Whether that will do all that you want I do not know. But that is what @SGaist meant by

                    You have to resize it yourself by requesting its sizeHint after you modify the number of columns.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Bert59
                      wrote on last edited by
                      #10

                      Hi,
                      I got the behaviour I was looking for by using the following code.

                      model.insertColumns(0, 4);
                      ui->table1->setModel(&model);
                      
                      int colWidth = ui->table1->columnWidth(1);
                      int vHeaderWidth = ui->table1->verticalHeader()->width();
                      int tableWidth = vHeaderWidth + 4*colWidth;
                      ui->table1->setFixedWidth(tableWidth);
                      

                      As I'm a beginner with Qt I don't know if it is the recommended way of doing it, but for the time being it works as expected.

                      Thank you for your comments

                      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