#include <QApplication>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QWidget>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
// 静态标签
QLabel *infoLabel = new QLabel;
QLabel *openLabel = new QLabel;
// 编辑框
QLineEdit* cmdLineEdit = new QLineEdit;
// 按钮
QPushButton* cancelButton = new QPushButton;
QPushButton* commitButton = new QPushButton;
// 静态标签设置文本
infoLabel->setText("input cmd:");
openLabel->setText("open");
// 按钮设置文本
cancelButton->setText("cancel");
commitButton->setText("commit");
// 水平布局
QHBoxLayout *cmdLayout = new QHBoxLayout;
// 将openLabel和cmdLineEdit添加到水平布局中
cmdLayout->addWidget(openLabel);
cmdLayout->addWidget(cmdLineEdit);
// 将commitButton和cacelButton按钮也添加到水平布局中
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(commitButton);
buttonLayout->addWidget(cancelButton);
// 垂直布局,将几个水平布局添加到垂直布局中
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(infoLabel);
mainLayout->addLayout(cmdLayout);
mainLayout->addLayout(buttonLayout);
// QWidget窗口,目前有MainWndow/QWidget/QDialog
QWidget w;
// 将垂直布局和QWidget窗口进行关联
w.setLayout(mainLayout);
w.show(); // 显示窗口
return app.exec(); // 事件循环
}
// 编译
/**
qmake -project
qmake hello.pro
mingw32-make会报错: fatal error: QApplication: No such file or directory
需要在hello.pro最后一行加上
QT += widgets gui
再指向mingw32-make
*/
QT widget
最新推荐文章于 2025-11-24 19:29:42 发布
4299

被折叠的 条评论
为什么被折叠?



