Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Errors in subclass of QSqlRelationalDelegate
Forum Updated to NodeBB v4.3 + New Features

Errors in subclass of QSqlRelationalDelegate

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 916 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Panoss

    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;
            }
        }
    
    Christian EhrlicherC Offline
    Christian EhrlicherC Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @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.

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    P 1 Reply Last reply
    3
    • Christian EhrlicherC Christian Ehrlicher

      @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.

      P Offline
      P Offline
      Panoss
      wrote on last edited by
      #3

      @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 EhrlicherC 1 Reply Last reply
      0
      • P Panoss

        @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 EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        P 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @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.

          P Offline
          P Offline
          Panoss
          wrote on last edited by
          #5

          @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)
          
          JonBJ 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #6

            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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • P Offline
              P Offline
              Panoss
              wrote on last edited by Panoss
              #7

              You mean this?

              QComboBox* mycombo = qobject_cast <QComboBox*>(editor );
              
              1 Reply Last reply
              0
              • P Offline
                P Offline
                Panoss
                wrote on last edited by Panoss
                #8

                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;
                    }
                
                1 Reply Last reply
                0
                • P Panoss

                  @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)
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by JonB
                  #9

                  @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;
                  
                  P 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @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;
                    
                    P Offline
                    P Offline
                    Panoss
                    wrote on last edited by
                    #10

                    @JonB said in Errors in subclass of QSqlRelationalDelegate:

                    return editor;

                    Should I return the editor or the combo?

                    JonBJ 1 Reply Last reply
                    0
                    • P Panoss

                      @JonB said in Errors in subclass of QSqlRelationalDelegate:

                      return editor;

                      Should I return the editor or the combo?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #11

                      @Panoss
                      Since I wrote return editor; and not return 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 if editor is a QComboBox * --- i.e. the createEditor() returned a QWidget which is a QComboBox --- and of course in that case editor == combo anyway. But if, for whatever reason, editor is not combobox/QComboBox * --- presumably for a column which is not the foreign key column but some other column --- you still want to return whatever that editor 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.

                      1 Reply Last reply
                      3
                      • P Offline
                        P Offline
                        Panoss
                        wrote on last edited by
                        #12

                        @JonB (and to all of course) thank you for your time and effort!
                        In python it works fine, I will try your code but I am sure it's fine!

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved