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 vertical header resize
Qt 6.11 is out! See what's new in the release blog

QTableView vertical header resize

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.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.
  • I Offline
    I Offline
    imre.horvath
    wrote on last edited by imre.horvath
    #1

    Hi!

    I try to resize the verticalheader of a qtableview with setFixedWidth(w) in runtime.
    It works fine, but the grid not aligned after the resizing. I mean, when I increase the width of the header, it covers the left part of the first column. I think I need to repaint the tableview, but I don't know how.

    Here is my code:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QStringListModel *m = new QStringListModel(this);
        ui->tableView->setModel(m);
        m->setStringList({"a", "b"});
        // here it works
        ui->tableView->verticalHeader()->setFixedWidth(20);
    
        connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on1);
        connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on2);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on1()
    {
        // here not
        ui->tableView->verticalHeader()->setFixedWidth(50);
    }
    
    void MainWindow::on2()
    {
        // here not
        ui->tableView->verticalHeader()->setFixedWidth(10);
    }
    

    initial state:
    https://mega.nz/#!Dk0gxIDK!0F8E8PzO-r7MKqi2MlVgznIa7akwfqw-kysPsxFGYNU

    after on1:
    https://mega.nz/#!yk9DUTBC!ikm3cxMNNlYjsCq_AXf0uI44jfZVLHGw8F6ccbFQ-3Y

    after on2:
    https://mega.nz/#!mxEEWYTL!5PjGLSph3LGgas0ezSUimNgAhsQ4aipDjKWqR96_Rgg

    Thanks in advance:
    Imre Horvath

    Taz742T 1 Reply Last reply
    1
    • I imre.horvath

      Hi!

      I try to resize the verticalheader of a qtableview with setFixedWidth(w) in runtime.
      It works fine, but the grid not aligned after the resizing. I mean, when I increase the width of the header, it covers the left part of the first column. I think I need to repaint the tableview, but I don't know how.

      Here is my code:

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          QStringListModel *m = new QStringListModel(this);
          ui->tableView->setModel(m);
          m->setStringList({"a", "b"});
          // here it works
          ui->tableView->verticalHeader()->setFixedWidth(20);
      
          connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::on1);
          connect(ui->pushButton_2, &QPushButton::clicked, this, &MainWindow::on2);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on1()
      {
          // here not
          ui->tableView->verticalHeader()->setFixedWidth(50);
      }
      
      void MainWindow::on2()
      {
          // here not
          ui->tableView->verticalHeader()->setFixedWidth(10);
      }
      

      initial state:
      https://mega.nz/#!Dk0gxIDK!0F8E8PzO-r7MKqi2MlVgznIa7akwfqw-kysPsxFGYNU

      after on1:
      https://mega.nz/#!yk9DUTBC!ikm3cxMNNlYjsCq_AXf0uI44jfZVLHGw8F6ccbFQ-3Y

      after on2:
      https://mega.nz/#!mxEEWYTL!5PjGLSph3LGgas0ezSUimNgAhsQ4aipDjKWqR96_Rgg

      Thanks in advance:
      Imre Horvath

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2

      @imre.horvath
      ui->tableView->setFixedWidth(50); is not a same ui->tableView->verticalHeader()->setFixedWidth(50); ?

      Do what you want.

      I 1 Reply Last reply
      0
      • Taz742T Taz742

        @imre.horvath
        ui->tableView->setFixedWidth(50); is not a same ui->tableView->verticalHeader()->setFixedWidth(50); ?

        I Offline
        I Offline
        imre.horvath
        wrote on last edited by
        #3

        @Taz742 No, i want to resize only the vertical header, not the whole tableview. The tableview size remains the same.

        Imre

        Taz742T 1 Reply Last reply
        0
        • I imre.horvath

          @Taz742 No, i want to resize only the vertical header, not the whole tableview. The tableview size remains the same.

          Imre

          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #4

          @imre.horvath
          You are right sorry. on1 and on2 slot is called when enter a button?

          Do what you want.

          I 1 Reply Last reply
          0
          • Taz742T Taz742

            @imre.horvath
            You are right sorry. on1 and on2 slot is called when enter a button?

            I Offline
            I Offline
            imre.horvath
            wrote on last edited by
            #5

            @Taz742 Yes, they are. I attached some screenshots

            Imre

            DiracsbracketD 1 Reply Last reply
            0
            • I imre.horvath

              @Taz742 Yes, they are. I attached some screenshots

              Imre

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by
              #6

              @imre.horvath
              This seems to be a known issue with QTableView

              A simple workaround from https://forum.qt.io/topic/8370/solved-resizing-vertical-header
              is as follows:

                  ui->tableView->verticalHeader()->setFixedWidth(50);
              
                  int colw=ui->tableView->columnWidth(0);
                  ui->tableView->setColumnWidth(0,colw-1);
                  ui->tableView->setColumnWidth(0,colw);
              

              In the post above, there is a more elegant method involving the use of QMetaObject::invokeMethod() which is the only way to invoke the protected method
              updateGeometries() which is apparently needed.

              I 1 Reply Last reply
              1
              • DiracsbracketD Diracsbracket

                @imre.horvath
                This seems to be a known issue with QTableView

                A simple workaround from https://forum.qt.io/topic/8370/solved-resizing-vertical-header
                is as follows:

                    ui->tableView->verticalHeader()->setFixedWidth(50);
                
                    int colw=ui->tableView->columnWidth(0);
                    ui->tableView->setColumnWidth(0,colw-1);
                    ui->tableView->setColumnWidth(0,colw);
                

                In the post above, there is a more elegant method involving the use of QMetaObject::invokeMethod() which is the only way to invoke the protected method
                updateGeometries() which is apparently needed.

                I Offline
                I Offline
                imre.horvath
                wrote on last edited by
                #7

                @Diracsbracket said in QTableView vertical header resize:

                int colw=ui->tableView->columnWidth(0);
                ui->tableView->setColumnWidth(0,colw-1);
                ui->tableView->setColumnWidth(0,colw);

                Thank you, it works.

                Imre

                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