QObject::connect: Cannot connect (null)
-
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 ?
-
Hi,
parent is not a BooleanWidget. The purpose of createEditor is to create a new editor, so your commented line was fine.
-
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);@
-
From your original post in the BooleanWidget.h snippet, it's not
-
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;
}
@ -
and another thing , why do i have to click like 3 times for the checkbox to get checked ?
-
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).
-