布局 Layout
QT 中使用QLayout 进行布局
自动控制计算机控件的大小和位置
父窗口调整后 根据策略调整各个子窗口的位置
QHBoxLayout:横向布局
QVBoxLayout:纵向布局
main.c
#include <QtWidgets/QApplication>
#include<MyWin.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWin *w = new MyWin(NULL);
w->resize(400, 300);
w->move(100, 100);
w->show();
return a.exec();
}
MyWin.cpp
#include"MyWin.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
MyWin::MyWin(QWidget*parent) :QWidget(parent)
{
/*创建文件*/
m_button = new QPushButton(this);
m_button->setText("ok");
m_lineEdit = new QLineEdit(this);
m_textEdit = new QPlainTextEdit(this);
/*创建布局器*/
QVBoxLayout* layout1 = new QVBoxLayout(this);
layout1->addWidget(m_button);
layout1->addWidget(m_textEdit);/*将第一个box添加到布局器*/
layout1->addWidget(m_button);
this->setLayout(layout1);
}
MyWin::~MyWin()
{
}
MyWin.h
#ifndef __MYWIN_H_
#define __MYWIN_H_
#include<QWidget> /*窗口类*/
//#include<QVBoxLayout>
#include<QLineEdit>/*文本框*/
#include<QPlainTextEdit> /*多行文本框*/
#include<QPushButton>
class MyWin :public QWidget
{
Q_OBJECT
public:
MyWin(QWidget*parent);
~MyWin();
private:
QLineEdit* m_lineEdit;
QPlainTextEdit* m_textEdit;
QPushButton*m_button;
};
#endif
- ui文件生成文件
创建一个ui文件
然后在UI文件里面编辑一些东西
保存就会发现 自己的工程里面多了一个
ui_MyWin.h的文件,而且这个文件随着ui文件改变而自己改变。
/********************************************************************************
** Form generated from reading UI file 'MyWin.ui'
**
** Created by: Qt User Interface Compiler version 5.8.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_MYWIN_H
#define UI_MYWIN_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTextEdit>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralwidget;
QWidget *widget;
QVBoxLayout *verticalLayout;
QHBoxLayout *horizontalLayout;
QLabel *label;
QPushButton *pushButton;
QHBoxLayout *horizontalLayout_2;
QTextEdit *textEdit;
QPushButton *pushButton_2;
QMenuBar *menubar;
QStatusBar *statusbar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QStringLiteral("MainWindow"));
MainWindow->resize(424, 344);
centralwidget = new QWidget(MainWindow);
centralwidget->setObjectName(QStringLiteral("centralwidget"));
widget = new QWidget(centralwidget);
widget->setObjectName(QStringLiteral("widget"));
widget->setGeometry(QRect(30, 60, 341, 227));
verticalLayout = new QVBoxLayout(widget);
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
verticalLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
label = new QLabel(widget);
label->setObjectName(QStringLiteral("label"));
horizontalLayout->addWidget(label);
pushButton = new QPushButton(widget);
pushButton->setObjectName(QStringLiteral("pushButton"));
horizontalLayout->addWidget(pushButton);
verticalLayout->addLayout(horizontalLayout);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
textEdit = new QTextEdit(widget);
textEdit->setObjectName(QStringLiteral("textEdit"));
horizontalLayout_2->addWidget(textEdit);
pushButton_2 = new QPushButton(widget);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
horizontalLayout_2->addWidget(pushButton_2);
verticalLayout->addLayout(horizontalLayout_2);
MainWindow->setCentralWidget(centralwidget);
menubar = new QMenuBar(MainWindow);
menubar->setObjectName(QStringLiteral("menubar"));
menubar->setGeometry(QRect(0, 0, 424, 23));
MainWindow->setMenuBar(menubar);
statusbar = new QStatusBar(MainWindow);
statusbar->setObjectName(QStringLiteral("statusbar"));
MainWindow->setStatusBar(statusbar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", Q_NULLPTR));
label->setText(QApplication::translate("MainWindow", "hello", Q_NULLPTR));
pushButton->setText(QApplication::translate("MainWindow", "hello", Q_NULLPTR));
pushButton_2->setText(QApplication::translate("MainWindow", "tom", Q_NULLPTR));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_MYWIN_H
结论:ui文件,编译后,编译器会给你生成一个界面文件。
发现自己很多东西边学边忘记,这是个很恐怖的事情,一直以来都没有刻意去背一些东西,习惯要改变 啊。