Reputation: 63
I'm new in Qt and currently I'm having some trouble. I want to create a grid layout in Qt from the c++ code and to be able to manage it from there. I have tried many different things but none of them work.
Basically, what I want to do is to have a window with a button in it(done that), and by pressing the button the window should change into a grid of buttons with no fixed number of rows or columns. ( hope I was clear on what I want to do)
At this point, I have created two .qml , one qml file has the code for the first window(the one with the one and only button) and the other qml file has a grid layout with nothing in it. There are also two c++ files the first one is the main.cpp and on the second one is the code I am writing to "fill" the grid on the click of the button.
Again, I'm new to Qt so if you have any better ideas please let me know!
Upvotes: 0
Views: 2071
Reputation: 13691
You can use a Repeater
to create some amount of Button
s as children of the Grid
. The Grid
will automatically set the positions of the Button
s so you should not set any anchors
or coordinates.
To handle the signals, you define one signal somewhere, that you then connect to the buttons signal.
Grid {
id: buttonGrid
signal buttonClicked(int index)
Repeater {
model: 100 // or any integer number
delegate: Button {
onClicked: buttonGrid.buttonClicked(index)
}
}
}
Instead of using a integer model, you might also use e.g a ListModel
or any QAbstractItemModel
-descendent. Then you can also use their roles, to get stuff like textes or colors or functions...
A simple array with textes would also be possible.
Upvotes: 1