質問

I am building a simple C++ application with GUI. I'm using the latest Qt (5.2). So my problem - I want to build something like minesweeper field on my window and I want to ask how can I create buttons within the code of the program so I can create different sizes of the field. There must be a way, ohterwise I will have to place 25, 64 and 144 buttons in three separate windows which is not correct.

Edit: I know it will be with some loop but I'm missing the code about "Create button" and the code about placing it on the windows and positioning it.

Thank you in advance

役に立ちましたか?

解決

You have two different ways to approach the problem:

1) Qt widgets

Use QPushButton creation within a loop with the desired iteration count.

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");

It is difficult to give any more concrete details without knowing more about your context and ues case.

2) QtQuickControls

Use the Button component with Repeater and/or Grid depending on your exact desire about the layout.

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"
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top