QItemDelegate avec des widgets personnalisés
-
26-12-2019 - |
Question
Je vais avoir des problèmes avec mon QTableView
et QItemDelegate
des classes.Pour une colonne de mon délégué crée une simple boîte combo et tout fonctionne bien.Pour mon 2ème colonne j'ai besoin d'un widget qui dispose de deux zones de liste déroulante dans un seul widget.
J'ai écrit le code suivant dans mon QItemDelegate
, juste pour être clair, cela ne montre le code pour mon 2ème colonne, celle qui ne fonctionne pas.L'autre simple Combo-box n'est pas montré comme il fonctionne très bien:
QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
//set up a simple widget with a layout
QWidget* pWidget = new QWidget(parent);
QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
pWidget->setLayout(hLayout);
//add two combo boxes to the layout
QComboBox* comboEditor = new QComboBox(pWidget);
QComboBox* comboEditor2 = new QComboBox(pWidget);
//now add both editors to this
hLayout->addWidget(comboEditor);
hLayout->addWidget(comboEditor2);
return pWidget;
}
Maintenant cette affiche très bien mais quand je l'ai modifier, puis cliquez sur ailleurs il n'arrête pas de montage.Quelqu'un peut-il offrir les pointeurs?
Edit:J'ai donc besoin d'appeler CommitData() et closeEditor() à un certain point.Quelqu'un peut-il offrir des conseils sur où appeler ces?
Merci.
La solution
Vous pouvez garder l'éditeur de widget en tant que membre de la classe et émettent commitData lorsque l'indice actuel de l'un des comboboxes a changé.Vous pouvez donc vous connecter currentIndexChanged(int) à un emplacement et émettent commitData à partir de là:
QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
//set up a simple widget with a layout
pWidget = new QWidget(parent);
QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
pWidget->setLayout(hLayout);
//add two combo boxes to the layout
QComboBox* comboEditor = new QComboBox(pWidget);
QComboBox* comboEditor2 = new QComboBox(pWidget);
connect(comboEditor,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
connect(comboEditor2,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
//now add both editors to this
hLayout->addWidget(comboEditor);
hLayout->addWidget(comboEditor2);
return pWidget;
}
void UserDefinedUnitsDelegate::setData(int val)
{
emit commitData(pWidget);
}