[SOLVED] QMap key order and QTabWidget



  • Hi,
    in my application I need to display a variable set of widgets within a QTabWidget. The variable number is defined by a QMap<QString, QString>. In particular the first parameter of the qmap represents a context (something that is tied to permissions, etc.) and the second the human readable title of the tab. So producing the tabs from the map is quite simple: foreach key in the map add a table with the entry value as title.
    The problem then is that when a user selects a different tab I need to know which context he is selecting, that is I need a reverse lookup on the map. Since qtabwidget provides me an event with the numeric index of the tab, I need to convert such number in a position within the key set of the map.
    Now, is it safe to assume that

    @
    // iterate on each key and build the tabs
    foreach(QString key, map->keys() )
    tabWidget->addTab( this, map->value( key );

    // in the slot
    map->keys()->at( tabIndex );@

    can be used in this case? In other words, if I build the tabs following the keys set, is it guarantee that the i-key will correspond to the i-tab? Otherwise, is there a different way to do an inverse lookup? Please note that I cannot use the tab title since it is not guaranteed to be unique.
    Any idea?



  • As long as you do not modify the map, it should be the same. But I would not bet my algorithm on that.

    To be on the safe side, you could store the QList of keys returned by keys().

    Another option could be to subclass QTabWidget, in that subclass you have access to the internal [[Doc:QTabBar]]. This provides a means to store arbitrary data associated with a tab with setTabData.



  • At the moment I've solved implementing a private class with a few fields:

    @class MapData{
    public:
    QString context;
    QString title;
    int index;
    };@

    and I convert the map that is passed to me to a list of MapData where I store also the index of the tab when I build it. In this way, using TabWidget::currentIndex() as the current index of the list I can get out the context for the selected tab. So, in the end, I adapted my data structure to a linear one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.