QT widget

#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
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值