- 类名的命名除Q外开头首字母大写;
- 函数命名,开头首字母小写;
- 单个部件有
setText()
函数为设置部件表面文字; - 布局
HBoxLayout
、VBoxLayout
可以添加窗口部件,也可以添加子布局
xxLayout->addWidget(B);
xxLayout->addLayout(C);
- 主窗口
QWidget
有设置总体布局函数setLayout()
;还有添加命名函数setWindowTitle()
;显示主窗口函数show()
;
示例代码如下:
#include <QApplication>
#include <QWidget>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
int main(int argc, char** argv)
QApplication app(argc,argv)
QWidget *window = new QWidget;
QLabel *infoLabel=new QLabel;
QLabel *cmdLabel=new QLabel;
QLineEdit *cmdLineEdit=new QLineEdit;
QPushButton *button1=new QPushButton;
QPushButton *button2=new QPushButton;
QPushButton *button3=new QPushButton;
infoLabel->setText("This is information");
cmdLabel->setText("Print:");
cmdLineEdit->clear();
button1->setText("One");
button2->setText("Two");
button3->setText("Three");
QHBoxLayout *qh1=new QHBoxLayout;
QHBoxLayout *qh2=new QHBoxLayout;
QVBoxLayout *mainLayout=new QVBoxLayout;
/*
布局类可以新加窗口子类的任何部件,如按钮,标签;也可以加已经子布局,层层包含。
*/
qh1->addWidget(cmdLabel);
qh1->addWidget(cmdLineEdit);
qh2->addWidget(button1);
qh2->addWidget(button2);
qh2->addWidget(button3);
mainLayout->addWidget(infoLabel);
mainLayout->addLayout(qh1);
mainLayout->addLayout(qh2);
window->setLayout(mainLayout);
window->setWindowTitle("Hello");
window->show();
return app.exec();
}
- ui 文件里右键
Chang objectName
可以用来修改文件在代码里的命名。 Ctrl+H
为水平布局快捷键;Ctrl+V
为垂直布局快捷键,Alt+Shift+R
为预览窗口效果。- 文本右键
Change rich Text
,可以常规快速修改文本属性 - 修改布局的时候,可以选中窗口右侧的文件列表,然后利用快捷键进行操作。
- 主窗口进行垂直布局设置,可以使窗口在调整大小使保持部件一同增大和变小;
- 对于想设置水平布局空隙,可以用
Vertical spacer
、Horizontal spacer
.