[solved]: Simple View made out of QButtons
-
Hi there!
I have a simple model comparable to a QStringList Model: It's 1-dimentional and does not have a tree stucture. The model is a QStandardItemModel filled with a custom subclass of QStandardItem where the data with DiaplayRole is simply a QString. No other roles are defined.
Now, I want to display a vertical list of QButtons or QCommandLinkButtons in a container. The list has to correspond exactly to the model: For example, if the model contains three items with data->DisplayRole^="one","two" and "three", there need to be exactly three buttons with the texts "one", "two" and "three" appearing in an area of the screen.
In order to keep things as simple as possible, I'd like to create a new view class to realize this list of buttons. The view Widget would have the following properties:
-> There is no header.
-> For each item in the model, there is exactly one button.
-> Like any view, this view adjusts itself automatically at any change of the model (update, insert, delete).
-> If necessary, scroll bars are displayed allow to scroll through the view.
-> The view provides a clicked(QWidget) signal (or similar) which contains a QWidget reference to the one QStandardItem in the model whose corresponding button was clicked.
-> The view cannot edit the model.
-> The active item is the button that was last clicked.
-> The view does not handle selections.
-> The view does not support drag&drop.
As you can see, this is an extremely simple type of view. Unfortunately, I have never gone past the 3 standard Qt view types, so I have no idea how to realize this.
What would be the very easiest, most straightforward and most convenient way to implement such a view? Note that efficiency is not a problem, since the view will not contain more than like 20 items.
Thx in advance!
Kalsan -
Hi and welcome to devnet,
You can make it in several ways, e.g. creating your own view based on QAbstractItemView. You can find an example of that in the Chart Example. Or go the widget way using QWidget + QScrollArea + QVBoxLayout and QPushButton. With this one, you'll have to connected to the model yourself but it's not extremely hard.
Hope it helps
-
Thank you SGaist for your reply.
I've already tried the first option and I broke my neck. Therefore I'd much rather go the second way.
I have a few questions regarding the details, as I'm still very new to Qt and not yet familiar with its conventions:- Which signals of the model will I have to listen to?
- Which functions of the model provide the state-of-the-art way to get information from it?
- Should I implement a general update function that regenerates the entire "view" widget at any change, or is it easier to have three distinct slots for insert / remove / update? If the second option is to be preferred, what is the right way to delete a button both from its containing QList and from the screen?
- Is a signal mapper the right way to emit a signal when a button is clicked?
Cheers,
Kalsan
-
- dataChanged
- modelReset
- rowsInserted
- rowsMoved
- rowsRemoved
should be enough
- data
- if you connect and handle correctly the signals from 1, it will be automatically done when removing an entry from the model.
- That depends on what you want to happen when a button is clicked
-
Thank you so much! I think with the information above I will be able to implement everything.
Cheers,
Kalsan -
You're welcome !
If this answers your question, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)
Happy Coding !
-
If anyone wants to know the exact implementation: I finally didn't make a custom Widget, but I created a new class that takes a parent QWidget (for automatic Qt deletion) and a target QLayout. The call of setModel associates the model and adds the initial buttons to the QLayout. Then, the five signals above are each connected to a slot which alters the buttons in the QLayout.