In an old program of mine, I have 2-column QTableWidget inside of a QDockWidget.
Building it in modern times (Qt 5.10) I seem to have to override the table's sizeHint() to get it to be a width besides 100px or something. Which was a bit annoying (didn't need this before--why isn't the table's width via headers the default?)
But with that tweaked, there's now another nuisance: if the user does any moving of the splitter for the dock widget, it will be lost on any window resize. I made a little MCVE to show the effect...here's the startup situation:
Then let's say the user drags the splitter a bit (could be expansion, could be contraction--doesn't matter):
Now they drag the bottom edge of the window down a bit, and snap, it jumps back to the preferred size (the sudden jump occurs right when the mouse first starts dragging):
Changing the horizontal size policy from QSizePolicy::Preferred
to QSizePolicy::Ignored
doesn't seem to influence this behavior, not calling setStretchLastSection()
doesn't seem to help either.
I'd prefer if the window be resized vertically that a horizontal splitter not move at all...and if it's resized horizontally that something more gradual be done than a jump. Any ideas? Qt Creator doesn't seem to have this problem with its watchlist, but I'm not familiar enough with the source to go digging into why.
mainwindow.h:
#include <QMainWindow>
QT_BEGIN_NAMESPACE
class QTextEdit;
class SizedTableWidget;
QT_END_NAMESPACE
class SizedTableWidget;
class MainWindow : public QMainWindow {
Q_OBJECT
QTextEdit *textEdit;
SizedTableWidget *table;
public:
MainWindow ();
};
mainwindow.cpp:
#include <QtWidgets>
#include "mainwindow.h"
class SizedTableWidget : public QTableWidget {
public:
SizedTableWidget (int rows, int columns, QWidget *parent) :
QTableWidget (rows, columns, parent) {}
QSize sizeHint() const {
return QSize (
verticalHeader()->width()
+ horizontalHeader()->length()
+ frameWidth() * 2,
horizontalHeader()->height()
+ verticalHeader()->length()
+ frameWidth() * 2
);
}
};
MainWindow::MainWindow() : textEdit(new QTextEdit) {
setCentralWidget(textEdit);
QDockWidget *dock = new QDockWidget(tr("Table"), this);
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
table = new SizedTableWidget (1 /* rows */, 2 /* cols */, dock);
table->setHorizontalHeaderLabels(QStringList() << "name" << "value");
QTableWidgetItem *nameItem = new QTableWidgetItem;
nameItem->setText("foo");
table->setItem(0, 0, nameItem);
QTableWidgetItem *valueItem = new QTableWidgetItem;
valueItem->setText("10");
table->setItem(0, 1, valueItem);
table->horizontalHeader()->setStretchLastSection(true);
table->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
dock->setWidget(table);
addDockWidget(Qt::RightDockWidgetArea, dock);
}
P.S. if anyone knows why the column and row headers on Windows don't have any styling on them, I'd be interested to know...is this on purpose? Looks fine on Linux.
System information:
- Qt 5.10.0
- Qt Creator 4.5.0
- Windows 10 Home, 1709 (Build 16299.125) and MSVC 2017
- Kubuntu Linux 17.10 and gcc 7.2.0
This appears to be a bug--now confirmed by multiple parties--that appeared in Qt 5.10:
https://bugreports.qt.io/browse/QTBUG-65592
A proposed workaround until it is fixed is given in the comments: