Is there any way to set the "correct" size of a QTableWidget? (I'm a newbie) This test code is only 25 lines long, in two files, with the file Test.h:
#include <QtGui>
class Test : public QMainWindow {
Q_OBJECT
public:
Test();
};
and the file Test.cpp:
#include "Test.h"
Test::Test() : QMainWindow() {
QVBoxLayout *vbox = new QVBoxLayout;
QPushButton *btn = new QPushButton("Hello World etc etc etc etc etc");
QTableWidget *tbl = new QTableWidget(2, 2);
vbox->addWidget(btn);
vbox->addWidget(tbl);
QWidget *w = new QWidget;
setCentralWidget(w);
w->setLayout(vbox);
resize(1, 1);
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Test test;
test.show();
app.exec();
}
Then the command:
qmake -project && qmake && make && ./Test
gives the window:
But what we want of course is something more like:
Using tbl->width()
seems to be useless, as it gives a default of 640
before test.show()
, and the unwanted value of 195
after. I've looked at the Qt Size Hints and Policies until my head spun, and I've tried setResizeMode(QHeaderView::Fixed)
and setStretchLastSection(false)
. Maybe I'm missing something obvious? This is with Qt 4.7.4 on CentOS 5, if this matters. Thank you for any help.
Edit: In response to DK, if the line resize(1, 1);
is not present, there is the equal and opposite problem: the window is too large.
And in response to Donotalo, adding:
tbl->setMaximumWidth(222);
tbl->setMinimumWidth(222);
tbl->setMaximumHeight(88);
tbl->setMinimumHeight(88);
will give the desired window size (at least on my machine), but not in the desired way. How should we calculate the 'constants' 222
and 88?
And Ton's answer to Qt: How to force a hidden widget to calculate its layout? doesn't seem to work here: the addition of tbl->setAttribute(Qt::WA_DontShowOnScreen); tbl->show();
left the value of tbl->width()
unchanged at 640.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…