Guest User

Untitled

a guest
Feb 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #ifndef COMBOBOXDELEGATE_H
  2. #define COMBOBOXDELEGATE_H
  3.  
  4. #include <QItemDelegate>
  5.  
  6. class ComboBoxDelegate : public QItemDelegate
  7. {
  8. Q_OBJECT
  9. QMap<int, QString> m_values;
  10.  
  11. public:
  12. ComboBoxDelegate(QObject *parent = 0);
  13. QWidget * createEditor( QWidget * parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
  14. void setEditorData( QWidget * editor, const QModelIndex &index ) const;
  15. void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
  16. void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  17. void paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
  18.  
  19. inline QMap<int, QString>& values() { return m_values; }
  20. };
  21.  
  22. #endif // COMBOBOXDELEGATE_H
  23.  
  24. #include "ComboBoxDelegate.h"
  25.  
  26. #include <QPainter>
  27. #include <QComboBox>
  28.  
  29. ComboBoxDelegate::ComboBoxDelegate(QObject *parent):QItemDelegate(parent)
  30. {
  31.  
  32. }
  33.  
  34. QWidget * ComboBoxDelegate::createEditor( QWidget * parent,
  35. const QStyleOptionViewItem& /* option */,
  36. const QModelIndex& /* index */) const
  37. {
  38. QComboBox * pEditor = new QComboBox(parent);
  39.  
  40. QMap<int, QString>::const_iterator i = m_values.constBegin();
  41. while ( i != m_values.constEnd() )
  42. {
  43. pEditor->addItem( i.value(), i.key() );
  44. ++i;
  45. }
  46.  
  47. pEditor->installEventFilter( const_cast<ComboBoxDelegate*>( this ) );
  48.  
  49. return pEditor;
  50. }
  51.  
  52. void ComboBoxDelegate::setEditorData( QWidget * editor, const QModelIndex &index) const
  53. {
  54. int value = index.model()->data( index, Qt::EditRole ).toInt();
  55. QComboBox * cb = static_cast<QComboBox*>( editor );
  56.  
  57. int idx = cb->findData( value );
  58. if ( idx < 0 ) return;
  59. cb->setCurrentIndex( idx );
  60. }
  61.  
  62. void ComboBoxDelegate::setModelData( QWidget * editor,
  63. QAbstractItemModel *model,
  64. const QModelIndex& index) const
  65. {
  66. QComboBox * cb = static_cast<QComboBox*>( editor );
  67. int idx = cb->currentIndex();
  68. if ( idx < 0 ) return;
  69. int value = cb->itemData( idx ).toInt();
  70. model->setData( index, value );
  71. }
  72.  
  73. void ComboBoxDelegate::updateEditorGeometry( QWidget *editor,
  74. const QStyleOptionViewItem &option,
  75. const QModelIndex& /* index */) const
  76. {
  77. editor->setGeometry( option.rect );
  78. }
  79. /**/
  80. void ComboBoxDelegate::paint( QPainter * painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
  81. {
  82. QStyleOptionViewItem opt = option;
  83. int value = index.data().toInt();
  84.  
  85. QString s = m_values[ value ];
  86. QVariant color = index.data( Qt::TextColorRole );
  87.  
  88. if ( color.isValid() && qvariant_cast<QColor>(color).isValid() )
  89. opt.palette.setColor( QPalette::Text, qvariant_cast<QColor>(color) );
  90.  
  91. opt.displayAlignment = Qt::AlignVCenter | Qt::AlignLeft;
  92.  
  93. drawDisplay(painter, opt, opt.rect, s);
  94. drawFocus(painter, opt, opt.rect);
  95. }
  96. /**/
  97.  
  98. ComboBoxDelegate *pModulatorDelegate = new ComboBoxDelegate(p_table);
  99. pModulatorDelegate->values().insert( 1, "J3E(USB)" );
  100. pModulatorDelegate->values().insert( 2, "J3E(LSB)" );
  101. pModulatorDelegate->values().insert( 3, "A1A" );
  102. pModulatorDelegate->values().insert( 4, "B8E" );
  103. pModulatorDelegate->values().insert( 5, "A3E" );
  104. pModulatorDelegate->values().insert( 6, "F1B" );
  105. pModulatorDelegate->values().insert( 7, "H3E(USB)" );
  106. pModulatorDelegate->values().insert( 8, "H3E(LSB)" );
  107. p_table->setItemDelegateForColumn( MODULATION_TYPE, pModulatorDelegate );
  108.  
  109. item = new QTableWidgetItem();
  110. item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled);
  111. /**/
  112. item = new QTableWidgetItem();
  113. item->setText( "J3E(USB)");
  114. p_table->setItem(i,MODULATION_TYPE,item);
  115. /**/
  116.  
  117. int value = index.data().toInt();
Add Comment
Please, Sign In to add comment