是否有任何方法来设置 QTableWidget 的正确"大小?(我是新手)这个测试代码只有25行,分两个文件,文件Test.h:
#include 类测试:公共 QMainWindow {Q_OBJECT民众:测试();};
和文件Test.cpp:
#include "Test.h"测试::测试() : QMainWindow() {QVBoxLayout *vbox = 新的 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 = 新的 QWidget;setCentralWidget(w);w->setLayout(vbox);调整大小(1, 1);}int main(int argc, char *argv[]) {QApplication app(argc, argv);测试一下;测试显示();app.exec();}
然后是命令:
qmake -project &&qmake &&制作&&./测试
给出窗口:
但我们想要的当然更像是:
使用 tbl->width()
似乎没用,因为它在 test.show()
之前给出了 640
的默认值,以及之后的 195
不需要的值.我已经查看了 Qt 大小提示和策略,直到我头晕目眩,并且尝试了 setResizeMode(QHeaderView::Fixed)
和 setStretchLastSection(false)
.也许我错过了一些明显的东西?如果这很重要,这是在 CentOS 5 上的 Qt 4.7.4.感谢您的帮助.
Edit:响应DK,if行resize(1, 1);
不是目前,存在相等和相反的问题:窗口太大.
为了回应 Donotalo,补充说:
tbl->setMaximumWidth(222);tbl->setMinimumWidth(222);tbl->setMaximumHeight(88);tbl->setMinimumHeight(88);
将给出所需的窗口大小(至少在我的机器上),但不会以所需的方式.我们应该如何计算常数"222
和 88?
Ton 对 .
更新 #3:
这是我的最终版本——但我对它不是很满意.非常欢迎任何改进.
adjustSize()
和 layout()->invalidate()
和 Qt::WA_DontShowOnScreen
之类的东西没有似乎有帮助.
博客 收缩将 Qt 小部件调整为所需的最小尺寸 很有趣,但使用 setSizeConstraint()
也一样好.
setSizeConstraint()
和 move()
方法是对表格的后续更改所必需的,此处未显示.
我在 CentOS 5 和 Qt 4.6.3 和 4.7.4 上进行的测试中有一件奇怪的事情.对于 hide();show();
序列,保存/恢复窗口位置.但是大约 25% 的时间(图案不规则)恢复的位置在屏幕上 24 像素,大概是窗口标题的高度.大约 10% 的时间 pos()
返回的值是 null.Qt 站点 说 X11 需要 漂亮的启发式和聪明的代码
,但似乎某处坏了.
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.
The thread How to set a precise size of QTableWidget to prevent from having scroll bars? (Qt-interest Archive, June 2007) between Lingfa Yang and Susan Macchia seems to resolve my question. I will post more details shortly, if my testing works.
Update #1: My test now generates the nice-looking window:
The complete test code for this, with Test.h unchanged, is:
#include "Test.h"
static QSize myGetQTableWidgetSize(QTableWidget *t) {
int w = t->verticalHeader()->width() + 4; // +4 seems to be needed
for (int i = 0; i < t->columnCount(); i++)
w += t->columnWidth(i); // seems to include gridline (on my machine)
int h = t->horizontalHeader()->height() + 4;
for (int i = 0; i < t->rowCount(); i++)
h += t->rowHeight(i);
return QSize(w, h);
}
static void myRedoGeometry(QWidget *w) {
const bool vis = w->isVisible();
const QPoint pos = w->pos();
w->hide();
w->show();
w->setVisible(vis);
if (vis && !pos.isNull())
w->move(pos);
}
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);
setCentralWidget(new QWidget);
centralWidget()->setLayout(vbox);
layout()->setSizeConstraint(QLayout::SetMinimumSize); // or SetFixedSize
tbl->setVerticalHeaderItem(1, new QTableWidgetItem("two")); // change size
myRedoGeometry(this);
tbl->setMaximumSize(myGetQTableWidgetSize(tbl));
tbl->setMinimumSize(tbl->maximumSize()); // optional
}
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Test test;
test.show();
app.exec();
}
Some notes:
The above thread's inclusion of verticalScrollBar()->width()
seems to be wrong. And, in my testing, this was always either a default value of 100, or the value 15, if the scrollbar had been displayed.
Applying the show(); hide();
sequence just to the QTableWidget was not sufficient to force Qt to recalculate the geometry, in this test. I needed to apply it to the whole window.
Any suggestions for improvements would be welome. (I'll wait a bit before accepting my own answer, in case there are better solutions.)
Update #2:
The Qt-interest thread may be wrong (or, at least, it disagrees with my version of Qt running my machine) regarding details on how to calculate the size: the +1 for each gridline is unnecessary, but an overall +4 is needed.
I'm still working through layout()->invalidate()
vs. e.g. QT: How to preview sizes of widgets in layout BEFORE a show().
Update #3:
This is my final version--but I'm not very happy with it. Any improvements would be very welcome.
Things like adjustSize()
and layout()->invalidate()
and Qt::WA_DontShowOnScreen
don't seem to help.
The blog Shrinking Qt widgets to minimum needed size is interesting, but using setSizeConstraint()
is just as good.
The setSizeConstraint()
and move()
methods are needed for subsequent changes to the table, not shown here.
There was an odd thing in my testing, done on CentOS 5 with Qt 4.6.3 and 4.7.4. For the hide(); show();
sequence, the window position is saved/restored. But about 25% of the time (the patterning was irregular) the restored position would be 24 pixels higher on the screen, presumably the height of the window title. And about 10% of the time the value returned by pos()
would be null. The Qt site says for X11 it needs nifty heuristics and clever code
for this, but something seems to be broken somewhere.
这篇关于如何确定 QTableWidget 的正确大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!