#include "mainwindow.h" #include "ui_mainwindow.h" #include <iostream> #include <QDebug> #include <QBoxLayout> #include <QGridLayout> #include <QFormLayout> #include <QLineEdit> #include <QStackedLayout> #include <unistd.h> #include <QSplitter> #include <QTextEdit> #include <QLabel> #include <QScrollArea> template<typename Y> void print_fun(const Y &_y) { qDebug()<<_y; } template<class Tag,typename Y> void print_fun(Tag tg,const Y &_y) { #ifdef __cplusplus std::cout<<tg<<_y<<std::endl; #endif } #define print_funcpp(var) print_fun("cpp ",var) MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); //step 1: QWidget *parent = new QWidget();QWidget::setParent QWidget *v_parent = new QWidget(); v_parent->setGeometry(0,0,500,500); v_parent->setParent(ui->centralWidget); //step 2:very important*******QBoxLayout(Direction,QWidget*parent) is valid,but QBoxlayout::setParent is invalid********** QBoxLayout *vboxlay = new QBoxLayout(QBoxLayout::TopToBottom,v_parent); QBoxLayout *hboxlay = new QBoxLayout(QBoxLayout::LeftToRight); //step 3:create some QPushButton ......QWidget,QWidget::setParent QPushButton *pt = new QPushButton(); pt->setFixedSize(100,40); pt->setText("top"); pt->setParent(v_parent); QPushButton *pb = new QPushButton(); pb->setFixedSize(100,40); pb->setText("bottom"); pb->setParent(v_parent); QPushButton *pc = new QPushButton(); pc->setFixedSize(100,40); pc->setText("center"); pc->setParent(v_parent); QPushButton *pl = new QPushButton(); pl->setFixedSize(100,40); pl->setText("left"); pl->setParent(v_parent); QPushButton *pr = new QPushButton(); pr->setFixedSize(100,40); pr->setText("right"); pr->setParent(v_parent); //step 4:QBoxlayout addWidget vboxlay->addWidget(pt,0,Qt::AlignTop|Qt::AlignCenter); hboxlay->addWidget(pl,0,Qt::AlignHCenter); hboxlay->addWidget(pc,0,Qt::AlignHCenter); hboxlay->addWidget(pr,0,Qt::AlignHCenter); vboxlay->addLayout(hboxlay); vboxlay->addWidget(pb,0,Qt::AlignBottom|Qt::AlignCenter); //step 5:QBoxlayout`s parentWidget setLaout(QBoxlayout) v_parent->setLayout(vboxlay); ///summury /// 1\QBoxlayout`s parentWidget-->QWidget::setParent /// 2\QBoxlayout-> """""""QBoxlayout`s constructor to setParent"""""""" /// 3\Widget->QWidget::setParent /// 4\QBoxlayout::addWidget /// 5\QBoxlayout`s parentWidget setLayout /// leran from 3 files: /// 1 qt designer page /// 2 mainwindow.ui /// 3 Ui::MainWindow.h /// summury } MainWindow::~MainWindow() { delete ui; }
标签:控件,QBoxlayout,Qt,parent,setParent,QWidget,QPushButton,coordinate,include From: https://www.cnblogs.com/cgybokeyuan/p/17036959.html