Python Forum
Convert python to Qt c++ - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Convert python to Qt c++ (/thread-36292.html)



Convert python to Qt c++ - panoss - Feb-05-2022

This is the code I want 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)
This is what I 've done so far:
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* myCombo = qobject_cast <QComboBox*>(editor);               ///?????????????
            myCombo->model()->select();                                                    ///??????????????/
            return editor;

    }
The 3 lines with the question marks are the problematic ones.
On the second one I get this: error: no member named 'select' in 'QAbstractItemModel'

Any help is welcome.


RE: Convert python to Qt c++ - panoss - Feb-05-2022

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;
    }