qt c++ 所有按钮ui调整

qt c++ 所有按钮ui调整

#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QList>
#include <QDebug>


class MyWindow : public QWidget {
public:
    MyWindow(QWidget *parent = nullptr) : QWidget(parent) {
        // 创建一些按钮作为示例

       this->resize(2000, 1000);
        QPushButton *button1 = new QPushButton("Button 1", this);
        QPushButton *button2 = new QPushButton("Button 2", this);
        QPushButton *button3 = new QPushButton("Button 3", this);

        // 布局按钮
        button1->setGeometry(30, 10, 100, 50);
        button2->setGeometry(30, 70, 100, 50);
        button3->setGeometry(30, 130, 100, 50);

        // 调用函数扩大所有按钮的大小
        // resizeButtons();
    }

    void resizeButtons() {
        // 找到所有 QPushButton 的实例
        QList<QPushButton *> buttons = findChildren<QPushButton *>();

        // 遍历并扩大每个按钮的大小
        for (QPushButton *button : buttons) {

            QRect currentGeometry = button->geometry();  // 获取当前几何信息
            int newX = currentGeometry.x() * 2;  // 位置 x 坐标扩大一倍
            int newY = currentGeometry.y() * 2;  // 位置 y 坐标扩大一倍
            int newWidth = currentGeometry.width() * 2;  // 宽度扩大一倍
            int newHeight = currentGeometry.height() * 2;  // 高度扩大一倍

            button->setGeometry(newX, newY, newWidth, newHeight);  // 设置新的几何信息


            currentGeometry = button->geometry();  // 获取当前几何信息

            // qDebug()>>currentGeometry.x()>>" ">>currentGeometry.y();
            // QSize currentSize = button->size();


            // button->resize(currentSize.width() * 2, currentSize.height() * 2);
        }
    }
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    MyWindow window;
    window.show();

    return app.exec();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值