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 resize row to contents to much space
Qt 6.11 is out! See what's new in the release blog

QTableView resize row to contents to much space

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 7.6k 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.
  • S sandro4912

    I have a QTabkeView with cells were text is displayed. I want the text to be word wrapped and only show then the rows with minimum height of the text.

    So i did this:

    questionsView->setModel(questionModel);

    questionsView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    questionsView->horizontalHeader()
            ->setSectionResizeMode(QHeaderView::Stretch);
    
    questionsView->setWordWrap(true);
    questionsView->verticalHeader()
            ->setSectionResizeMode(QHeaderView::ResizeToContents);
    
    questionsView->setColumnHidden(0, true);
    

    The result looks like this:

    0_1566726421186_resize.png

    I have expected that ResizeToContents renders the rows with only the minum heigth so that the text fits but not these hughe gaps get created . How can i solve this?

    T Offline
    T Offline
    Tink
    wrote on last edited by
    #2

    Perhaps because of: QSizePolicy::Expanding ?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sandro4912
      wrote on last edited by
      #3

      i also tryed it with QSizePolicy::Preferred but same result

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Tink
        wrote on last edited by
        #4

        And is resizeRowsToContents() not useful?

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #5

          Which Qt version do you use and can you please provide a small example so we can try to reproduce it?

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

          1 Reply Last reply
          1
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #6

            I use Qt5.12.4. I run the code under KDE Neon. About the the small example let me check.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              sandro4912
              wrote on last edited by
              #7

              it looks like it kinda works but not directly after the table is shown first i have to click into the vertical header

              My code is like this.

              In the Constructor i do this:

              MainWindow::MainWindow()
              {
                  questionModel = new QuestionModel;
              
                  questionsView = new QTableView;
              
                  questionsView->setModel(questionModel);
              
                  questionsView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
              
                  questionsView->horizontalHeader()
                          ->setSectionResizeMode(QHeaderView::Stretch);
              
                  questionsView->setWordWrap(true);
                  questionsView->verticalHeader()
                          ->setSectionResizeMode(QHeaderView::ResizeToContents);
              
                  questionsView->setColumnHidden(0, true);
              
                ....
              }
              

              Then later when a QAction is pressed i set the central widget to the view:

              void MainWindow::editQuestions()
              {
                  setCentralWidget(questionsView);
              }
              

              As soon as i click on the header of the row it resizes but not before.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #8

                I did not saw such a behavior in one of my tableviews so I think you must do something wrong. Please try to create a minimal example program so we can check what's going wrong here. Clicking a small QTableWidget together and add the appropriate calls shouldn't be that difficult.

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

                1 Reply Last reply
                4
                • S Offline
                  S Offline
                  sandro4912
                  wrote on last edited by
                  #9

                  Here an minimal programm were the issue also occurs. If you click on the second row it resizes. Thats what should happen immidiately after calling mainwindow.

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  
                  class QTableWidget;
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  public:
                      explicit MainWindow(QWidget *parent = nullptr);
                  
                  private:
                      QTableWidget *table;
                  };
                  
                  #endif // MAINWINDOW_H
                  
                  #include "mainwindow.h"
                  
                  #include <QTableWidget>
                  #include <QGuiApplication>
                  #include <QScreen>
                  #include <QHeaderView>
                  
                  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
                  {
                      setFixedSize(QGuiApplication::primaryScreen()->availableGeometry().size()
                                  * 0.6);
                  
                      table = new QTableWidget{ 10, 7 };
                  
                      QStringList header{
                          "id", "Question", "Answer1",
                          "Answer2", "Answer3", "Answer4", "CorrectAnswer"
                      };
                  
                      table->setHorizontalHeaderLabels(header);
                  
                      auto column0 = new QTableWidgetItem(tr("1"));
                      auto column1 = new QTableWidgetItem(tr(
                          "Wahlen in Deutschland sind frei. Was bedeutet das?"));
                  
                      auto column2 = new QTableWidgetItem(tr(
                          "Man darf Geld annehmen, wenn man dafür einen bestimmten Kandidaten / "
                          "eine bestimmte Kandidatin wählt."));
                      auto column3 = new QTableWidgetItem(tr(
                          "Nur Personen, die noch nie im Gefängnis waren, dürfen wählen."));
                      auto column4 = new QTableWidgetItem(tr(
                          "Der Wähler darf bei der Wahl weder beeinflusst noch zu einer bestimmten"
                          " Stimmabgabe gezwungen werden und keine Nachteile durch die Wahl haben."));
                      auto column5 = new QTableWidgetItem(tr(
                          "Alle wahlberechtigten Personen müssen wählen."));
                      auto column6 = new QTableWidgetItem(tr("3"));
                  
                      table->setItem(0, 0, column0);
                      table->setItem(0, 1, column1);
                      table->setItem(0, 2, column2);
                      table->setItem(0, 3, column3);
                      table->setItem(0, 4, column4);
                      table->setItem(0, 5, column5);
                      table->setItem(0, 6, column6);
                  
                      table->setSizePolicy(
                                  QSizePolicy::Expanding, QSizePolicy::Preferred);
                      table->setWordWrap(true);
                      table->horizontalHeader()
                              ->setSectionResizeMode(QHeaderView::Stretch );
                      table->verticalHeader()
                              ->setSectionResizeMode(QHeaderView::ResizeToContents);
                      table->setColumnHidden(0, true);
                  
                      setCentralWidget(table);
                  }
                  
                  
                  #include <QApplication>
                  
                  #include "mainwindow.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication app{ argc, argv };
                  
                      MainWindow win;
                      win.show();
                  
                      return QApplication::exec();
                  }
                  
                  
                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by Christian Ehrlicher
                    #10

                    It's a initialization problem - one of the headers must be calculated first. And in your case it's the vertical header. Therefore QHeaderView::ResizeToContents is evaluated first and then QHeaderView::Stretch. There is nothing we can do against it since when we change the order it will not work the other way round. One idea is to delay the verticalHeader resize mode

                    QTimer::singleShot(10, this, this { table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); });

                    And what you see with the resize is due to the fact that the click on the second rows triggers a resize because the (invisible) sort indicator must be flipped which triggers also a recalculation.

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

                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      sandro4912
                      wrote on last edited by
                      #11

                      This works perfectly thanks for the explanation. However in your statement seems to be a typo it does give syntax errors with the second this( i couldn't figure our was wrong). I changed it to a lamda likes this:

                          QTimer::singleShot(10, this, [=]() {
                              table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
                          }  );
                      

                      Now it resizes as expected.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #12

                        @sandro4912 said in QTableView resize row to contents to much space:

                        However in your statement seems to be a typo

                        It's the forum software, the blue this is '[ this ]'

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

                        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