PDA

View Full Version : Trying to convert python code to Qt c++



panoss
5th February 2022, 16:31
I 'm subclassing the QSqlRelationalDelegate and I have this (working) python code which I 'm trying to convert to Qt c++:


def createEditor(self, parent, option, index):
# column of combo box 'position'
positionColumn = 2
print("myDelegate.createEditor index.column()=" + str(index.column()) + " option=" + str(option) )
if index.column() == positionColumn:
editor = QSqlRelationalDelegate.createEditor(self, parent, option, index)
if isinstance(editor, QComboBox):
editor.model().select()
return editor
else:
return super(myDelegate, self).createEditor(parent, option, index)



What I 've done so far (don't take it as correct, it's just an effort, it's not working):


QWidget *BookDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
int positionColumn = 2;
qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
if (index.column() == positionColumn){
QWidget *editor = QSqlRelationalDelegate::createEditor(parent, option, index);
//QComboBox *editor = new QComboBox(parent);
//QSqlRelationalTableModel *model = qobject_cast <QSqlRelationalTableModel*>(index.model());
///QComboBox* myCombo = qobject_cast <QComboBox*>(editor);
editor->model()->select();
return editor;

}


Any help is welcome.

panoss
5th February 2022, 18:48
This is the code I ended up with (from the QSqlRelationalDelegate 's code), and works!
Any comments are welcome.


int positionColumn = 2;
qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
if (index.column() == positionColumn){
const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
if (!childModel)
return QStyledItemDelegate::createEditor(parent, option, index);

QComboBox *combo = new QComboBox(parent);
combo->setModel(childModel);
combo->setModelColumn(1);
combo->installEventFilter(const_cast<BookDelegate *>(this));
childModel->select();
return combo;
}

Ginsengelf
7th February 2022, 07:19
Hi, the "else" part from your original python code is gone in your last C++ version. You check for "index.column() == positionColumn", but do not return the default editor if that condition is false.

Ginsengelf