-> 今天做了个小测试,如何使用代码控制QGridLayout中的窗体比例呢?
有两个函数可以用上:
QGridLayout::setColumnStretch(列码, 比例值);
QGridLayout::setRowStretch(行码, 比例值);
-> 下面是需要实现的比例效果图:
第一行和第二行是1:1,第一列和第二列是1:2
![Dumpt.com](http://www.dumpt.com/img/files/1mw33sknc14jgkh366ai_thumb.jpg)
->具体使用方法见示例代码:
-
-
- #include <QApplication>
- #include <QGridLayout>
- #include <QList>
- #include <QLabel>
- #include <QPalette>
- #include <QString>
-
- void setLayout2p1(QGridLayout * , QWidget * , QList<QLabel*> *);
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
-
-
- QPalette myPalette;
- myPalette.setColor(QPalette::Window, QColor(0, 255, 0));
- QList<QLabel *> * labelList = new QList<QLabel*>;
- for(int i = 0; i < 3; ++i){
- QLabel * temp1 = new QLabel("Label Num: " + QString::number(i + 1, 10));
- temp1 -> setAutoFillBackground(true);
- temp1->setPalette(myPalette);
- labelList->push_back(temp1);
- }
-
- QGridLayout * layout2p1 = new QGridLayout();
- QWidget * testWidget = new QWidget();
-
-
- setLayout2p1(layout2p1 , testWidget, labelList);
-
- int result = a.exec();
-
-
-
- while(!labelList->isEmpty()){
- QLabel * temp = labelList -> last();
- labelList -> removeLast();
- delete temp;
- }
- delete labelList;
-
- delete layout2p1;
-
- delete testWidget;
-
- return result;
- }
-
- void setLayout2p1(QGridLayout * layout2p1, QWidget * testWidget, QList<QLabel*> *labelList)
- {
-
- testWidget -> setLayout(layout2p1);
- layout2p1 -> addWidget(labelList->at(0), 0, 0);
- layout2p1 -> addWidget(labelList->at(1), 1, 0);
- layout2p1 -> addWidget(labelList ->at(2), 0, 1, 2, 1);
-
-
- layout2p1 -> setColumnStretch(0, 1);
- layout2p1 -> setColumnStretch(1, 2);
-
-
- layout2p1 -> setRowStretch(0, 1);
- layout2p1 -> setRowStretch(1, 1);
-
- testWidget -> show();
-
- return;
- }
本文出自 “Neicole — Data Library” 博客,请务必保留此出处http://neicole.blog.51cto.com/5621716/967329