Errors in subclass of QSqlRelationalDelegate
-
In a subclass of QSqlRelationalDelegate I don't know how the correct code is.
At this line: 'if (isinstance(editor, QComboBox)){'
I get this error: ' error: 'QComboBox' does not refer to a value
qcombobox.h:59:24: note: declared here'At line 'editor->model().select();'...
I get this: ' error: no member named 'model' in 'QWidget''I have iincluded QComboBox and QtWidgets
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); if (isinstance(editor, QComboBox)){ editor->model().select(); return editor; } }
-
In a subclass of QSqlRelationalDelegate I don't know how the correct code is.
At this line: 'if (isinstance(editor, QComboBox)){'
I get this error: ' error: 'QComboBox' does not refer to a value
qcombobox.h:59:24: note: declared here'At line 'editor->model().select();'...
I get this: ' error: no member named 'model' in 'QWidget''I have iincluded QComboBox and QtWidgets
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); if (isinstance(editor, QComboBox)){ editor->model().select(); return editor; } }
@Panoss said in Errors in subclass of QSqlRelationalDelegate:
isinstance
What does this function do / where is it defined? You for sure want to use qobject_cast<>() instead.
-
@Panoss said in Errors in subclass of QSqlRelationalDelegate:
isinstance
What does this function do / where is it defined? You for sure want to use qobject_cast<>() instead.
@Christian-Ehrlicher said in Errors in subclass of QSqlRelationalDelegate:
use qobject_cast<>() instead.
I did it this way:
qobject_cast <QComboBox*>(editor )){ editor->model().select();
The error from the first line is gone.
On the second I get:
'bookdelegate.cpp:133:21: error: no member named 'model' in 'QWidget'' -
@Christian-Ehrlicher said in Errors in subclass of QSqlRelationalDelegate:
use qobject_cast<>() instead.
I did it this way:
qobject_cast <QComboBox*>(editor )){ editor->model().select();
The error from the first line is gone.
On the second I get:
'bookdelegate.cpp:133:21: error: no member named 'model' in 'QWidget''@Panoss said in Errors in subclass of QSqlRelationalDelegate:
qobject_cast <QComboBox*>(editor )){
And what should this help? You should assign the return value to a variable. c basics.
-
@Panoss said in Errors in subclass of QSqlRelationalDelegate:
qobject_cast <QComboBox*>(editor )){
And what should this help? You should assign the return value to a variable. c basics.
@Christian-Ehrlicher
What I 'm actually trying to do is convert this python code 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)
-
Learning the very basics of c++ is not part of this forum. Doing a simple assignment of a function call to a variable is really not that hard.
-
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; }
-
@Christian-Ehrlicher
What I 'm actually trying to do is convert this python code 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)
@Panoss said in Errors in subclass of QSqlRelationalDelegate:
editor = QSqlRelationalDelegate.createEditor(self, parent, option, index) if isinstance(editor, QComboBox): editor.model().select() return editor
QWidget *editor = QSqlRelationalDelegate::createEditor(parent, option, index); QComboBox *combo = qobject_cast<QComboBox *>(editor); if (combo) combo->model()->select(); return editor;
-
@Panoss said in Errors in subclass of QSqlRelationalDelegate:
editor = QSqlRelationalDelegate.createEditor(self, parent, option, index) if isinstance(editor, QComboBox): editor.model().select() return editor
QWidget *editor = QSqlRelationalDelegate::createEditor(parent, option, index); QComboBox *combo = qobject_cast<QComboBox *>(editor); if (combo) combo->model()->select(); return editor;
-
@JonB said in Errors in subclass of QSqlRelationalDelegate:
return editor;
Should I return the editor or the combo?
@Panoss
Since I wrotereturn editor;
and notreturn combo;
you have the answer :)But you should understand why this is. No matter what
editor = QSqlRelationalDelegate::createEditor()
, which is what you want to return under all circumstances.combo
only has a!= nullptr
value ifeditor
is aQComboBox *
--- i.e. thecreateEditor()
returned aQWidget
which is aQComboBox
--- and of course in that caseeditor == combo
anyway. But if, for whatever reason,editor
is notcombobox
/QComboBox *
--- presumably for a column which is not the foreign key column but some other column --- you still want to return whatever thateditor
is.My C++ is a direct translation of the original Python, which I presume to be correct/desired, unless you want to change that behaviour for some reason.