From 1e9601e927da655b04d2d37c0341488c36ae9cdd Mon Sep 17 00:00:00 2001 From: stv Date: Mon, 28 May 2007 08:52:01 +0000 Subject: [PATCH] no message --- src/Qtx/QtxComboBox.cxx | 23 +++++++++++++++++------ src/Qtx/QtxComboBox.h | 16 +++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Qtx/QtxComboBox.cxx b/src/Qtx/QtxComboBox.cxx index eecfbb405..0bbe2ce60 100755 --- a/src/Qtx/QtxComboBox.cxx +++ b/src/Qtx/QtxComboBox.cxx @@ -122,6 +122,16 @@ void QtxComboBox::setCurrentId( int num ) 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 @@ -196,8 +206,9 @@ void QtxComboBox::paintClear( QPaintEvent* e ) 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; } @@ -206,13 +217,13 @@ int QtxComboBox::id( const int idx ) const \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; } diff --git a/src/Qtx/QtxComboBox.h b/src/Qtx/QtxComboBox.h index 8038e5aa9..9aae4eb72 100755 --- a/src/Qtx/QtxComboBox.h +++ b/src/Qtx/QtxComboBox.h @@ -24,8 +24,8 @@ #include "Qtx.h" -#include #include +#include #ifdef WIN32 #pragma warning( disable:4251 ) @@ -35,8 +35,6 @@ class QTX_EXPORT QtxComboBox : public QComboBox { Q_OBJECT - typedef QMap IndexIdMap; //!< ID to item index map - public: QtxComboBox( QWidget* = 0 ); virtual ~QtxComboBox(); @@ -49,6 +47,11 @@ public: 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 ); @@ -61,15 +64,14 @@ protected: 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 -- 2.39.2