views:

58

answers:

3

Hi,

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 QActions (grouped into the same QMenu) depending on a model (derived from QAbstractListModel) ?

A: 

No. Models can only be used with Views, as per the Model-View framework that Qt uses.

Gianni
+1  A: 

To answer your short question, yes, there is. But you'll have to write it yourself.

The easy part would be to create a subclass of QAbstractListModel.

The hard part would be when you create your own view. Qt will let you create your own view, just like if you were to create your own model, but it would become so much more complex, since you've got to handle everything yourself.

It's entirely doable for a specific purpose, but it's also much more work than I think you want. So, like Gianni was saying, Qt's model-view framework isn't meant to be used this way.

ianmac45
+1  A: 

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..

liaK