I created a model which list the existing configurations (let's say it lists "files", as this doesn't really matter here). So far, it works well when attached to a QListView
.
Example:
--- ListView ---
- file #1 -
- file #2 -
- file #3 -
- file #4 -
----------------
Is it possible to use the same model for a dynamically updated QMenu
?
Something like:
Menu
-> Submenu #1
-> Submenu #2
-> File-submenu
-> file #1
-> file #2
-> file #3
-> file #4
-> Submenu #3
In short: is there any way to create a list of dynamicaly updated QAction
s (grouped into the same QMenu
) depending on a model (derived from QAbstractListModel
) ?
If your objective is just to update your menu actons with the item text that are available in the QAbstractListModel
, then the answer is Yes.
Here is a way..
Individual item's index can be obtained by using the following function.
QModelIndex QAbstractListModel::index ( int row, int column = 0,
const QModelIndex & parent = QModelIndex() ) const [virtual]
With the obtained index, the data can be obtained by,
QVariant QModelIndex::data ( int role = Qt::DisplayRole ) const
Then the text availalble in the index can be obtained by using,
QString QVariant::toString () const
Now with the obtained QString you can add an action to the menu.
QAction * QMenu::addAction ( const QString & text )
The thing you have to make sure is that, you should be able to traverse through all the items in the Model, so that you can obtain the index of the each and every item. Hope it helps..