Connect Signal and Slots with QPointer

1.1k Views Asked by At

I am creating a GUI application in which I have a dynamic QComboBox. I have used QPointer to store the QComboBox * such that I can avoid a dangling pointer when it is deleted (elsewhere). Even though QPointer is supposed to be a smart pointer with conversion operators to the raw pointer type, when I pass it to QObject::connect() (new style), I will receive a compiler error.

Example:

QPointer<QComboBox> CMB_ItemType = new QComboBox;
connect(CMB_ItemType, &QComboBox::currentTextChanged, [&](const QString val){ui->TW_Contents->setCellWidget(nRow, 1, CMB_ItemContent);});

Compiler says:

     error: no matching function for call to 'EditRegionClass::connect(QPointer<QComboBox>&, void (QComboBox::*)(const QString&), EditRegionClass::addContent(QString, QString)::__lambda43)'
 connect(CMB_ItemType, &QComboBox::currentTextChanged, [&](const QString val){ui->TW_Contents->setCellWidget(nRow, 1, CMB_ItemContent);});
                                                                                                                                                    ^

I can make it work by replacing CMB_ItemType -> CMB_ItemType.data() but why is the conversion operator not used automatically?

1

There are 1 best solutions below

0
On

My test variant works good:

QPointer<QLineEdit> le = new QLineEdit;
connect(le, &QLineEdit::textChanged, this, &QWidget::close);

I think there some problems with your lambda.