PDA

View Full Version : Item Delegates QStringList and QComboBox



hec_mex
12th March 2013, 23:05
Hello!

I’m implementing an item delegate to extend the QStyledItemDelegate. The idea is whe an value is a QStringList i use a QComboBox as editor in the item delegate. But i have some problems

1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn’t show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell

2. When i want to know what is the selection i can’t figure ir out how to get the item selected in the QComboBox.

Any ideas?



class ItemDelegate : public QStyledItemDelegate {
Q_OBJECT

public:
ItemDelegate(QObject *parent = 0): QStyledItemDelegate(parent) {;}
virtual ~ItemDelegate() { ; }

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;

inline void updateEditorGeometry(QWidget *editor,const QStyleOptionViewItem &option, const QModelIndex& ) const {
editor->setGeometry(option.rect);
}

};




QWidget* ItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem & option ,
const QModelIndex & index ) const {

QVariant data = index.model()->data(index, Qt::EditRole);

QWidget* editor;
switch( data.type() ) {
case QVariant::StringList:
editor = new QComboBox(parent);
dynamic_cast<QComboBox*>(editor)->setFrame(false);
break;
default:
editor = QStyledItemDelegate::createEditor(parent, option, index);
}

return editor;
}

void ItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {

QVariant data = index.model()->data(index, Qt::EditRole);

switch( data.type() ) {
case QVariant::StringList:
static_cast<QComboBox*>(editor)->addItems(data.toStringList());
break;
default:
QStyledItemDelegate::setEditorData(editor, index);
}
}

void ItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QVariant data = index.model()->data(index, Qt::EditRole);

switch( data.type() ) {
case QVariant::StringList: {
QStringList list;
for( int i = 0; i < static_cast<QComboBox*>(editor)->count(); ++i )
list << static_cast<QComboBox*>(editor)->itemText(i);
model->setData(index, QVariant(list), Qt::EditRole);
break;
}
default:
QStyledItemDelegate::setModelData(editor, model, index);
}
}

wysota
13th March 2013, 00:27
1. When i set the QStringList in the data of a view (lets say a tree), the cell doesn’t show a text. When i double click the cell a combobox appears, i can make a selection, the combobox closes and nothin is diplayed in the cell
Return the current string as the Qt::DisplayRole value.


2. When i want to know what is the selection i can’t figure ir out how to get the item selected in the QComboBox.
It depends. Usually this is the current item but if you want to know the item the user chooses when the popup is open, it is the currently highlighted item (carried through a signal).

hec_mex
13th March 2013, 00:53
Return the current string as the Qt::DisplayRole value.
Maybe i'm too newbie.

In wich moment i get the current string?
Return the current string from where? to where?



It depends. Usually this is the current item but if you want to know the item the user chooses when the popup is open, it is the currently highlighted item (carried through a signal).
But outside of the item delegate i can connect any signal or even know the ComboBox

Lets try an example



QTreeView *tree = QTreeView;
tree->setModel(new QStandardItemModel);
tree->setItemDelegate(new ItemDelegate);

QStringList strList;

strList << "A string" << "Another string";

QStandardItem* item = new QStandardItem;
item->setData(strList, Qt::EditRole);

tree->model()->appendRow(tree->model()->invisibleRootItem(), item);

wysota
13th March 2013, 01:00
In wich moment i get the current string?
I have no idea. It's your project, not mine. You should be the one who knows what he wants displayed.


Return the current string from where?
From the model's data() method

to where?
To the caller.


But outside of the item delegate i can connect any signal or even know the ComboBox
I have no idea what that means.

hec_mex
13th March 2013, 19:22
ok pal, it's obvious that we don't understand each other. Thanks anyway.


I have no idea. It's your project, not mine. You should be the one who knows what he wants displayed.


From the model's data() method

To the caller.


I have no idea what that means.