Results 1 to 3 of 3

Thread: Trying to convert python code to Qt c++

  1. #1
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Trying to convert python code to Qt c++

    I 'm subclassing the QSqlRelationalDelegate and I have this (working) python code which I 'm trying to convert to Qt c++:
    Qt Code:
    1. def createEditor(self, parent, option, index):
    2. # column of combo box 'position'
    3. positionColumn = 2
    4. print("myDelegate.createEditor index.column()=" + str(index.column()) + " option=" + str(option) )
    5. if index.column() == positionColumn:
    6. editor = QSqlRelationalDelegate.createEditor(self, parent, option, index)
    7. if isinstance(editor, QComboBox):
    8. editor.model().select()
    9. return editor
    10. else:
    11. return super(myDelegate, self).createEditor(parent, option, index)
    To copy to clipboard, switch view to plain text mode 


    What I 've done so far (don't take it as correct, it's just an effort, it's not working):
    Qt Code:
    1. QWidget *BookDelegate::createEditor(QWidget *parent,
    2. const QStyleOptionViewItem &option,
    3. const QModelIndex &index) const
    4. {
    5. int positionColumn = 2;
    6. qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
    7. if (index.column() == positionColumn){
    8. QWidget *editor = QSqlRelationalDelegate::createEditor(parent, option, index);
    9. //QComboBox *editor = new QComboBox(parent);
    10. //QSqlRelationalTableModel *model = qobject_cast <QSqlRelationalTableModel*>(index.model());
    11. ///QComboBox* myCombo = qobject_cast <QComboBox*>(editor);
    12. editor->model()->select();
    13. return editor;
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    Any help is welcome.

  2. #2
    Join Date
    Feb 2017
    Posts
    19
    Thanks
    5
    Qt products
    Qt5

    Default Re: Trying to convert python code to Qt c++

    This is the code I ended up with (from the QSqlRelationalDelegate 's code), and works!
    Any comments are welcome.
    Qt Code:
    1. int positionColumn = 2;
    2. qDebug()<< "BookDelegate.createEditor index.column()=" << (index.column()) << " option=" << option ;
    3. if (index.column() == positionColumn){
    4. const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
    5. QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : nullptr;
    6. if (!childModel)
    7. return QStyledItemDelegate::createEditor(parent, option, index);
    8.  
    9. QComboBox *combo = new QComboBox(parent);
    10. combo->setModel(childModel);
    11. combo->setModelColumn(1);
    12. combo->installEventFilter(const_cast<BookDelegate *>(this));
    13. childModel->select();
    14. return combo;
    15. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    487
    Thanks
    11
    Thanked 71 Times in 69 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Trying to convert python code to Qt c++

    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

Similar Threads

  1. Rewrite code from C++ to Python
    By Pedro Monteiro in forum Qt Programming
    Replies: 0
    Last Post: 6th January 2016, 20:52
  2. PyQt4 GUI to python code Help
    By dan_cuspi in forum Newbie
    Replies: 0
    Last Post: 26th November 2015, 18:47
  3. convert c++ class to python code
    By alrawab in forum Qt Programming
    Replies: 8
    Last Post: 25th June 2012, 08:43
  4. Replies: 0
    Last Post: 21st October 2010, 13:18
  5. Replies: 0
    Last Post: 9th November 2009, 16:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.