QgridLayout例子,在qlayout添加button实例(添加窗体过程类似)
生活随笔
收集整理的這篇文章主要介紹了
QgridLayout例子,在qlayout添加button实例(添加窗体过程类似)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "mainwindow.h"
#include <QApplication>
#include <QtGui>
#include "qwidget.h"
#include "qgridlayout.h"
#include "qpushbutton.h"
//qt layout例子,qt layout用法,qt qgridlayout demo
//編譯環境:qt5.5,mingGW,windows 7 旗艦版,x64
//建一個沒有ui的qt工程,最簡單的qt工程,把下面代碼拷貝到main.c里面編譯及可通過
int main(int argc, char *argv[])
{QApplication app(argc, argv);QMainWindow *window = new QMainWindow();window->setWindowTitle(QString::fromUtf8("QGridLayout Col and Row Span"));window->resize(330, 220);QWidget *centralWidget = new QWidget(window);QGridLayout *layout = new QGridLayout();centralWidget->setLayout(layout);//qt添加控件方法,qt窗口添加按鈕,qt添加按鈕,qt添加layout//ROW 1//Add Items to QGridLayout Here//Row and Column counts are set Automaticallylayout->addWidget(new QPushButton("Button 1"),0,0);layout->addWidget(new QPushButton("Button 2"),0,1);layout->addWidget(new QPushButton("Button 3"),0,2);layout->addWidget(new QPushButton("Button 4"),0,3);
//qt控件添加,Qt gridlayout合并,widget添加按鈕,widget添加按鍵//ROW 2layout->addWidget(new QPushButton("Button 5"),1,0);//set Row and Col Span of "Button 6" to 2//Set SizePolicy of "Button 6" to Expanding for Both Directions//So that Row and Col Span can be seen clearlyQPushButton* button6 = new QPushButton("Button 6");//中間的按鈕6 button 6占領了框中間兩行兩列。變成一個大按鈕button6->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);layout->addWidget(button6,1,1,2,2);//前面量個1,表示第一行,和第一列,//qt的tab,layout計數都是0開始。第一行,合并的函數是2行,第一列,合并的列數是2列layout->addWidget(new QPushButton("Button 7"),1,3);//ROW 3layout->addWidget(new QPushButton("Button 8"),2,0);layout->addWidget(new QPushButton("Button 9"),2,3);//ROW 4layout->addWidget(new QPushButton("Button 10"),3,0);layout->addWidget(new QPushButton("Button 11"),3,1);layout->addWidget(new QPushButton("Button 12"),3,2);layout->addWidget(new QPushButton("Button 13"),3,3);window->setCentralWidget(centralWidget);window->show();return app.exec();
}
總結
以上是生活随笔為你收集整理的QgridLayout例子,在qlayout添加button实例(添加窗体过程类似)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap 中: data-to
- 下一篇: QT信号与槽(自定义带参数的信号)