pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何確定 QTableWidget 的正確大小?

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

問(wèn)題描述

是否有任何方法來(lái)設(shè)置 QTableWidget 的正確"大小?(我是新手)這個(gè)測(cè)試代碼只有25行,分兩個(gè)文件,文件Test.h:

#include 類(lèi)測(cè)試:公共 QMainWindow {Q_OBJECT民眾:測(cè)試();};

和文件Test.cpp:

#include "Test.h"測(cè)試::測(cè)試() : 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);調(diào)整大小(1, 1);}int main(int argc, char *argv[]) {QApplication app(argc, argv);測(cè)試一下;測(cè)試顯示();app.exec();}

然后是命令:

 qmake -project &&qmake &&制作&&./測(cè)試

給出窗口:

但我們想要的當(dāng)然更像是:

使用 tbl->width() 似乎沒(méi)用,因?yàn)樗?test.show() 之前給出了 640 的默認(rèn)值,以及之后的 195 不需要的值.我已經(jīng)查看了 Qt 大小提示和策略,直到我頭暈?zāi)垦#⑶覈L試了 setResizeMode(QHeaderView::Fixed)setStretchLastSection(false).也許我錯(cuò)過(guò)了一些明顯的東西?如果這很重要,這是在 CentOS 5 上的 Qt 4.7.4.感謝您的幫助.

Edit:響應(yīng)DK,ifresize(1, 1);不是目前,存在相等相反的問(wèn)題:窗口太大.

為了回應(yīng) Donotalo,補(bǔ)充說(shuō):

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

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

Ton 對(duì) .

更新 #3:

  • 這是我的最終版本——但我對(duì)它不是很滿意.非常歡迎任何改進(jìn).

  • adjustSize()layout()->invalidate()Qt::WA_DontShowOnScreen 之類(lèi)的東西沒(méi)有似乎有幫助.

  • 博客 收縮將 Qt 小部件調(diào)整為所需的最小尺寸 很有趣,但使用 setSizeConstraint() 也一樣好.

  • setSizeConstraint()move() 方法是對(duì)表格的后續(xù)更改所必需的,此處未顯示.

  • 我在 CentOS 5 和 Qt 4.6.3 和 4.7.4 上進(jìn)行的測(cè)試中有一件奇怪的事情.對(duì)于 hide();show(); 序列,保存/恢復(fù)窗口位置.但是大約 25% 的時(shí)間(圖案不規(guī)則)恢復(fù)的位置在屏幕上 24 像素,大概是窗口標(biāo)題的高度.大約 10% 的時(shí)間 pos() 返回的值是 null.Qt 站點(diǎn) 說(shuō) X11 需要 漂亮的啟發(fā)式和聰明的代碼,但似乎某處壞了.

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.

這篇關(guān)于如何確定 QTableWidget 的正確大小?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 无菌实验室规划装修设计-一体化实验室承包-北京洁净净化工程建设施工-北京航天科恩实验室装备工程技术有限公司 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 刮板输送机,粉尘加湿搅拌机,螺旋输送机,布袋除尘器 | 润滑脂-高温润滑脂-轴承润滑脂-食品级润滑油-索科润滑油脂厂家 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 电动葫芦|环链电动葫芦-北京凌鹰名优起重葫芦 | 动物麻醉机-数显脑立体定位仪-北京易则佳科技有限公司 | 神超官网_焊接圆锯片_高速钢锯片_硬质合金锯片_浙江神超锯业制造有限公司 | 浇注料-高铝砖耐火砖-郑州凯瑞得窑炉耐火材料有限公司 | 耐高温风管_耐高温软管_食品级软管_吸尘管_钢丝软管_卫生级软管_塑料波纹管-东莞市鑫翔宇软管有限公司 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 行吊_电动单梁起重机_双梁起重机_合肥起重机_厂家_合肥市神雕起重机械有限公司 | 钢化玻璃膜|手机钢化膜|钢化膜厂家|手机保护膜-【东莞市大象电子科技有限公司】 | 深圳展厅设计_企业展馆设计_展厅设计公司_数字展厅设计_深圳百艺堂 | 深圳公司注册-工商注册公司-千百顺代理记账公司 | 蒸汽吸附分析仪-进口水分活度仪|康宝百科| 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 瓶盖扭矩仪(扭力值检测)-百科 | 南京租车,南京汽车租赁,南京包车,南京会议租车-南京七熹租车 | 缓蚀除垢剂_循环水阻垢剂_反渗透锅炉阻垢剂_有机硫化物-郑州威大水处理材料有限公司 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | Honsberg流量计-Greisinger真空表-气压计-上海欧臻机电设备有限公司 | 工控机-图像采集卡-PoE网卡-人工智能-工业主板-深圳朗锐智科 | 造价工程师网,考试时间查询,报名入口信息-网站首页 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | 北京网站建设公司_北京网站制作公司_北京网站设计公司-北京爱品特网站建站公司 | 密度电子天平-内校-外校电子天平-沈阳龙腾电子有限公司 | 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 | 欧景装饰设计工程有限公司-无锡欧景装饰官网 | 洗砂机械-球磨制砂机-洗沙制砂机械设备_青州冠诚重工机械有限公司 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 闪蒸干燥机-喷雾干燥机-带式干燥机-桨叶干燥机-[常州佳一干燥设备] | 商标转让-商标注册-商标查询-软著专利服务平台 - 赣江万网 |