PDA

View Full Version : QCombobox in QTreeview



jigglyslime
30th October 2013, 02:24
ok so i'm trying to get a combobox to show up in my tree view, but for some reason it won't allow me to edit it, my view select thing is set for row select.

Also is there a way i can edit an item while hovering over it, so e.g i hover over an item with a combobox and just click the box and it comes up with options.


inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionComboBox comboBoxOption;
comboBoxOption.rect = option.rect;
comboBoxOption.state = QStyle::State_Enabled;
comboBoxOption.frame = true;
comboBoxOption.currentText = index.data().toString();
QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
//QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);

}

inline QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug() << "Editor created";
// ComboBox ony in column 2

// Create the combobox and populate it
QComboBox *cb = new QComboBox(parent);
int row = index.row();
cb->addItem(QString("one in row %1").arg(row));
cb->addItem(QString("two in row %1").arg(row));
cb->addItem(QString("three in row %1").arg(row));
return cb;
}

inline void setEditorData ( QWidget *editor, const QModelIndex &index ) const
{
if(QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
// get the index of the text in the combobox that matches the current value of the itenm
QString currentText = index.data(Qt::EditRole).toString();
int cbIndex = cb->findText(currentText);
// if it is valid, adjust the combobox
if(cbIndex >= 0)
cb->setCurrentIndex(cbIndex);
} else {
QStyledItemDelegate::setEditorData(editor, index);
}
}
inline void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
// save the current text of the combo box as the current value of the item
model->setData(index, cb->currentText(), Qt::EditRole);
else
QStyledItemDelegate::setModelData(editor, model, index);
}
inline void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

Santosh Reddy
30th October 2013, 13:37
Do you want to show the QComboBox all the time or just while editing the cell data?

jigglyslime
31st October 2013, 00:12
i want to show the combobox when the mouse is hovering over the row of the tree view