Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COMBOBOXDELEGATE_H
- #define COMBOBOXDELEGATE_H
- #include <QItemDelegate>
- class ComboBoxDelegate : public QItemDelegate
- {
- Q_OBJECT
- QMap<int, QString> m_values;
- public:
- ComboBoxDelegate(QObject *parent = 0);
- QWidget * createEditor( QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
- void setEditorData( QWidget * editor, const QModelIndex &index ) const;
- void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
- void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
- void paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
- inline QMap<int, QString>& values() { return m_values; }
- };
- #endif // COMBOBOXDELEGATE_H
- #include "ComboBoxDelegate.h"
- #include <QPainter>
- #include <QComboBox>
- ComboBoxDelegate::ComboBoxDelegate(QObject *parent):QItemDelegate(parent)
- {
- }
- QWidget * ComboBoxDelegate::createEditor( QWidget * parent,
- const QStyleOptionViewItem& /* option */,
- const QModelIndex& /* index */) const
- {
- QComboBox * pEditor = new QComboBox(parent);
- QMap<int, QString>::const_iterator i = m_values.constBegin();
- while ( i != m_values.constEnd() )
- {
- pEditor->addItem( i.value(), i.key() );
- ++i;
- }
- pEditor->installEventFilter( const_cast<ComboBoxDelegate*>( this ) );
- return pEditor;
- }
- void ComboBoxDelegate::setEditorData( QWidget * editor, const QModelIndex &index) const
- {
- int value = index.model()->data( index, Qt::EditRole ).toInt();
- QComboBox * cb = static_cast<QComboBox*>( editor );
- int idx = cb->findData( value );
- if ( idx < 0 ) return;
- cb->setCurrentIndex( idx );
- }
- void ComboBoxDelegate::setModelData( QWidget * editor,
- QAbstractItemModel *model,
- const QModelIndex& index) const
- {
- QComboBox * cb = static_cast<QComboBox*>( editor );
- int idx = cb->currentIndex();
- if ( idx < 0 ) return;
- int value = cb->itemData( idx ).toInt();
- model->setData( index, value );
- }
- void ComboBoxDelegate::updateEditorGeometry( QWidget *editor,
- const QStyleOptionViewItem &option,
- const QModelIndex& /* index */) const
- {
- editor->setGeometry( option.rect );
- }
- /**/
- void ComboBoxDelegate::paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
- {
- QStyleOptionViewItem opt = option;
- int value = index.data().toInt();
- QString s = m_values[ value ];
- QVariant color = index.data( Qt::TextColorRole );
- if ( color.isValid() && qvariant_cast<QColor>(color).isValid() )
- opt.palette.setColor( QPalette::Text, qvariant_cast<QColor>(color) );
- opt.displayAlignment = Qt::AlignVCenter | Qt::AlignLeft;
- drawDisplay(painter, opt, opt.rect, s);
- drawFocus(painter, opt, opt.rect);
- }
- /**/
- ComboBoxDelegate *pModulatorDelegate = new ComboBoxDelegate(p_table);
- pModulatorDelegate->values().insert( 1, "J3E(USB)" );
- pModulatorDelegate->values().insert( 2, "J3E(LSB)" );
- pModulatorDelegate->values().insert( 3, "A1A" );
- pModulatorDelegate->values().insert( 4, "B8E" );
- pModulatorDelegate->values().insert( 5, "A3E" );
- pModulatorDelegate->values().insert( 6, "F1B" );
- pModulatorDelegate->values().insert( 7, "H3E(USB)" );
- pModulatorDelegate->values().insert( 8, "H3E(LSB)" );
- p_table->setItemDelegateForColumn( MODULATION_TYPE, pModulatorDelegate );
- item = new QTableWidgetItem();
- item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
- /**/
- item = new QTableWidgetItem();
- item->setText( "J3E(USB)");
- p_table->setItem(i,MODULATION_TYPE,item);
- /**/
- int value = index.data().toInt();
Add Comment
Please, Sign In to add comment