2

我正在使用 GUI 构建一个简单的 C++ 应用程序。我正在使用最新的 Qt (5.2)。所以我的问题 - 我想在我的窗口上构建类似扫雷字段的东西,我想问我如何在程序代码中创建按钮,以便我可以创建不同大小的字段。必须有一种方法,否则我将不得不将 25、64 和 144 按钮放在三个单独的窗口中,这是不正确的。

编辑:我知道它会带有一些循环,但我缺少关于“创建按钮”的代码以及关于将其放置在窗口上并定位它的代码。

先感谢您

4

1 回答 1

2

你有两种不同的方法来解决这个问题:

1) Qt 小部件

在具有所需迭代计数的循环中使用QPushButton创建。

QVector<QPushButton> pushButtons1(25);
foreach (QPushButton &pushButton, pushButtons1)
    pushButton.setText("pushButtons1");

QVector<QPushButton> pushButtons2(64);
foreach (QPushButton &pushButton, pushButtons2)
    pushButton.setText("pushButtons2");

QVector<QPushButton> pushButtons2(144);
foreach (QPushButton &pushButton, pushButtons3)
    pushButton.setText("pushButtons3");

在不了解您的上下文和使用案例的情况下,很难提供任何更具体的细节。

2) QtQuickControls

根据您对布局的确切需求,使用带有 Repeater 和/或 Grid 的Button组件。

import QtQuick 2.0

Row {
    Repeater {
        model: 25
        Button {
            text: "foo1"
        }
    }
}


Row {
    Repeater {
        model: 44
        Button {
            text: "foo2"
        }
    }
}

Row {
    Repeater {
        model: 144
        Button {
            text: "foo3"
        }
    }
}
于 2014-01-07T02:42:01.500 回答