久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何確定 QTableWidget 的正確大小?

How to determine the correct size of a QTableWidget?(如何確定 QTableWidget 的正確大小?)
本文介紹了如何確定 QTableWidget 的正確大小?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否有任何方法來設置 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,ifresize(1, 1);不是目前,存在相等相反的問題:窗口太大.

為了回應 Donotalo,補充說:

tbl->setMaximumWidth(222);tbl->setMinimumWidth(222);tbl->setMaximumHeight(88);tbl->setMinimumHeight(88);

將給出所需的窗口大小(至少在我的機器上),但不會以所需的方式.我們應該如何計算常數"22288?

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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 一区二区三区在线电影 | 国产97视频在线观看 | 久久久久久毛片免费观看 | 亚洲一区国产精品 | 中文字幕在线视频网站 | 欧美成年黄网站色视频 | 美女黄视频网站 | 五月免费视频 | 久久婷婷麻豆国产91天堂 | 欧美中文在线 | 怡红院怡春院一级毛片 | 午夜精品一区二区三区在线播放 | 高清国产一区二区 | 久久精品视频一区二区三区 | 九九九国产 | 久久精品亚洲成在人线av网址 | 欧美国产日韩在线 | 网站黄色在线免费观看 | 国产大片黄色 | 成人国内精品久久久久一区 | 国产一区二区三区视频在线观看 | 黄色在线免费观看 | 一区二区三区视频在线免费观看 | 一区二区在线免费观看 | 国产精品高潮呻吟久久久久 | 日日操夜夜摸 | 欧美日韩高清免费 | 香蕉久久网 | 久久精品亚洲精品国产欧美 | 久久国产精品免费一区二区三区 | 欧洲高清转码区一二区 | 日韩高清一区 | 成人午夜在线 | 亚洲国产一区在线 | 国产欧美在线 | 精品国产免费一区二区三区五区 | 男女羞羞视频免费看 | 99免费在线视频 | 成人在线免费观看视频 | 久久精品中文 | 国产欧美在线 |