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. QObject::connect: Cannot connect (null)

QObject::connect: Cannot connect (null)

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 5.6k Views 1 Watching
  • 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.
  • saeedhardanS Offline
    saeedhardanS Offline
    saeedhardan
    wrote on last edited by
    #1

    Hi,
    i'm trying to implement a checkbox within itemdelegate .
    This is my code :
    BooleanWidget.h
    @
    class BooleanWidget : public QWidget
    {
    Q_OBJECT
    QCheckBox * checkBox;

    public:
    BooleanWidget(QWidget *parent = 0):
    QWidget(parent)
    {
    checkBox = new QCheckBox(this);
    QHBoxLayout * layout = new QHBoxLayout(this);
    layout->addWidget(checkBox,0, Qt::AlignCenter);
    }

    bool isChecked(){
        return checkBox->isChecked();
    }
    void setChecked(bool value){
        checkBox->setChecked(value);
    }
    

    signals:
    void toggled(bool value);

    private:
    };
    @

    CheckBoxDelegate.h
    @
    class CheckBoxDelegate : public QItemDelegate
    {
    Q_OBJECT
    private:
    BooleanWidget *checkbox;

    public:
    CheckBoxDelegate();
    CheckBoxDelegate(QObject *parent);
    ~CheckBoxDelegate();
    void setEditorData( QWidget *editor,const QModelIndex &index )const;
    void setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &index )const;
    QWidget *createEditor( QWidget parent,const QStyleOptionViewItem &/ option /,const QModelIndex &/ index */ )const;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

    public slots:
    void changed( bool );

    };
    @

    CheckBoxDelegate.cpp
    @
    QWidget *CheckBoxDelegate::createEditor( QWidget parent,const QStyleOptionViewItem &/ option /,const QModelIndex &/ index */ ) const
    {
    //BooleanWidget editor = new BooleanWidget( parent );
    BooleanWidget * editor = qobject_cast<BooleanWidget
    >(parent);
    connect( editor, SIGNAL(toggled(bool)), this, SLOT(changed(bool)));

    return editor;
    

    }
    @

    As you can see BooleanWidget does have the macro Q_OBJECT .
    But the @ qobject_cast<BooleanWidget*>(parent);@

    always returns null . why is that ?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      parent is not a BooleanWidget. The purpose of createEditor is to create a new editor, so your commented line was fine.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • saeedhardanS Offline
        saeedhardanS Offline
        saeedhardan
        wrote on last edited by
        #3

        thanks it works , but now i added

        @const QModelIndex &index@

        to the signal and slot , but i get the error :

        QObject::connect: No such signal BooleanWidget::toggled(bool,index)

        my signal is :

        @ void toggled(bool value,const QModelIndex &index);@

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          From your original post in the BooleanWidget.h snippet, it's not

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • saeedhardanS Offline
            saeedhardanS Offline
            saeedhardan
            wrote on last edited by
            #5

            This is the new one :

            @class BooleanWidget : public QWidget
            {
            Q_OBJECT
            QCheckBox * checkBox;

            public:
            BooleanWidget(QWidget *parent = 0):
            QWidget(parent)
            {
            checkBox = new QCheckBox(this);
            QHBoxLayout * layout = new QHBoxLayout(this);
            layout->addWidget(checkBox,0, Qt::AlignCenter);
            }

            bool isChecked(){
                return checkBox->isChecked();
            }
            void setChecked(bool value){
                checkBox->setChecked(value);
            }
            

            signals:
            void toggled(bool value,const QModelIndex &index);

            private:
            };@

            @class CheckBoxDelegate : public QItemDelegate
            {
            Q_OBJECT
            private:
            BooleanWidget *checkbox;

            public:
            CheckBoxDelegate();
            CheckBoxDelegate(QObject *parent);
            ~CheckBoxDelegate();
            void setEditorData( QWidget *editor,const QModelIndex &index )const;
            void setModelData( QWidget *editor,QAbstractItemModel *model,const QModelIndex &index )const;
            QWidget *createEditor( QWidget parent,const QStyleOptionViewItem &/ option /,const QModelIndex &/ index */ )const;
            void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;

            public slots:
            void changed( bool ,const QModelIndex &index );

            };
            @

            @
            void CheckBoxDelegate::changed( bool value ,const QModelIndex &index )
            {
            BooleanWidget checkbox = qobject_cast<BooleanWidget>( sender() );
            checkbox->setChecked(value);
            emit commitData( checkbox );
            emit closeEditor( checkbox );
            QWidget t;
            QMessageBox::information(&t,"yay","yay index: "+index.row());
            }
            QWidget *CheckBoxDelegate::createEditor( QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
            {
            BooleanWidget editor = new BooleanWidget( parent );
            //BooleanWidget * editor = qobject_cast<BooleanWidget
            >(parent);
            connect( editor, SIGNAL(toggled(bool,index)), this, SLOT(changed(bool,index)));

            return editor;
            

            }
            @

            1 Reply Last reply
            0
            • saeedhardanS Offline
              saeedhardanS Offline
              saeedhardan
              wrote on last edited by
              #6

              and another thing , why do i have to click like 3 times for the checkbox to get checked ?

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Your SIGNAL and SLOT macros are wrong, you gave the variable name not the type:

                @connect( editor, SIGNAL(toggled(bool,QModelIndex)), this, SLOT(changed(bool,QModelIndex)));@

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • saeedhardanS Offline
                  saeedhardanS Offline
                  saeedhardan
                  wrote on last edited by
                  #8

                  Please ignore this : "but even though i get no error the slot CheckBoxDelegate::changed() doesn't get called because i dont get any messagebox ("yay") " .

                  The situation now is :

                    • That signal method works , thanks a lot .
                    • I still the first time the window opens i have to click the checkbox like 3 time for it to get checked (but only first time meaning after i checked it for the first time then i can check/uncheck with only one click)
                    • where is the best place to emit a signal (toggeled()) with index . i mean which function is responsible for changing the state of the checkbox ,(sorry im not that good with itemdelegate).
                  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