QT中代码布局

1.文本框与按钮布局

    // 创建一个新的QWidget作为QStackedWidget的页面
    QWidget *pagewidget = new QWidget();
    // 创建一个文本标签
    QLabel *label = new QLabel("屏幕设置", pagewidget);
    label->setAlignment(Qt::AlignLeft | Qt::AlignVCenter); // 明确设置左对齐
    // 创建三个按钮
    QPushButton *button1 = new QPushButton("上半屏", pagewidget);
    QPushButton *button2 = new QPushButton("下半屏", pagewidget);
    QPushButton *button3 = new QPushButton("全屏", pagewidget);
    // 设置按钮的固定大小为60x40像素
    button1->setFixedSize(60, 40);
    button2->setFixedSize(60, 40);
    button3->setFixedSize(60, 40);
    label->setFixedSize(100, 40);
    // 创建一个子布局来放置三个按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    // 添加按钮到按钮布局中
    buttonLayout->addWidget(button1);
    buttonLayout->addWidget(button2);
    buttonLayout->addWidget(button3);
    // 设置按钮之间的间隔(如果需要)
    buttonLayout->setSpacing(100); // 例如,5像素的间隔
    // 创建一个主布局来管理整个pagewidget
    QHBoxLayout *mainLayout = new QHBoxLayout(pagewidget);
    // 将文本标签添加到主布局中
    mainLayout->addWidget(label);
    // 在标签和按钮布局之间添加一个固定的间隔
    mainLayout->addSpacing(300); // 例如,20像素的间隔
    // 将按钮布局作为一个整体添加到主布局中
    mainLayout->addLayout(buttonLayout);
    // 设置边距(如果需要)
    mainLayout->setContentsMargins(10, 10, 10, 10);
    // 设置文本框(或QLineEdit)字体颜色为橙色
    label->setStyleSheet("QLabel { border: 1px solid black; padding: 2px; color: orange; }");
    // 设置按钮字体颜色为白色
    button1->setStyleSheet("QPushButton { color: white; }");
    button2->setStyleSheet("QPushButton { color: white; }");
    button3->setStyleSheet("QPushButton { color: white; }");
    // 将布局设置给QWidget
    pagewidget->setLayout(mainLayout);
    // 将QWidget添加到QStackedWidget中,并获取其索引
    int index = ui->stackedWidget->addWidget(pagewidget);
    qDebug()<<"index-->"<<index;
    // 设置QStackedWidget的当前页面为新添加的页面
    ui->stackedWidget->setCurrentIndex(index);

    // 使用QOverload和lambda表达式连接按钮的clicked信号
    connect(button1, &QPushButton::clicked, this, [=]() {
        wavesetpopupPage=0;
                        qDebug() << "按钮1被点击,标志位:" << wavesetpopupPage;
    });
    connect(button2, &QPushButton::clicked, this, [=]() {
        wavesetpopupPage=1;
                        qDebug() << "按钮1被点击,标志位:" << wavesetpopupPage;
    });
    connect(button3, &QPushButton::clicked, this, [=]() {
        wavesetpopupPage=2;
                        qDebug() << "按钮1被点击,标志位:" << wavesetpopupPage;
    });

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值