QTableView resize row to contents to much space
-
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:
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? -
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:
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? -
i also tryed it with
QSizePolicy::Preferred
but same result -
Which Qt version do you use and can you please provide a small example so we can try to reproduce it?
-
I use Qt5.12.4. I run the code under KDE Neon. About the the small example let me check.
-
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.
-
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.
-
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(); }
-
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.
-
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.
-
@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 ]'