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();
}