setCurrentIndex( index( num ) );
}
+/*!
+ \brief Set the identifier to specified item.
+ \param index - index of the item
+ \param id - identifier of the item
+*/
+void QtxComboBox::setId( const int index, const int id )
+{
+ setItemData( index, QVariant( id ), (Qt::ItemDataRole)IdRole );
+}
+
/*!
\brief Customize paint event.
\param e paint event
int QtxComboBox::id( const int idx ) const
{
int id = -1;
- if ( myIndexId.contains( idx ) )
- id = myIndexId[idx];
+ QVariant v = itemData( idx, (Qt::ItemDataRole)IdRole );
+ if ( v.canConvert( QVariant::Int ) )
+ id = v.toInt();
return id;
}
\param id item ID
\return item index or -1 if ID is invalid.
*/
-int QtxComboBox::index( const int id ) const
+int QtxComboBox::index( const int ident ) const
{
int idx = -1;
- for ( IndexIdMap::ConstIterator it = myIndexId.begin(); it != myIndexId.end() && idx == -1; ++it )
+ for ( int i = 0; i < (int)count() && idx == -1; i++ )
{
- if ( it.value() == id )
- idx = it.key();
+ if ( id( i ) == ident )
+ idx = i;
}
return idx;
}
#include "Qtx.h"
-#include <QComboBox>
#include <QMap>
+#include <QComboBox>
#ifdef WIN32
#pragma warning( disable:4251 )
{
Q_OBJECT
- typedef QMap<int, int> IndexIdMap; //!< ID to item index map
-
public:
QtxComboBox( QWidget* = 0 );
virtual ~QtxComboBox();
int currentId() const;
void setCurrentId( int );
+ int id( const int ) const;
+ int index( const int ) const;
+
+ void setId( const int, const int );
+
signals:
void activatedId( int );
void highlightedId( int );
virtual void paintEvent( QPaintEvent* );
private:
- int id( const int ) const;
- int index( const int ) const;
-
void resetClear();
void paintClear( QPaintEvent* );
+private:
+ enum { IdRole = Qt::UserRole + 10 };
+
private:
bool myCleared; //!< "cleared" state
- IndexIdMap myIndexId; //!< ID to item index map
};
#ifdef WIN32