0

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?

Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
Abhi
  • 81
  • 7
  • Not sure what you meant with "so that it smart pointer manages the memory leak", but the `QPointer` destructor does not delete the object it points to. – Kevin Krammer Oct 26 '16 at 11:39
  • Then how that object will get free? – Abhi Oct 26 '16 at 12:23
  • For widgets lke the combobox that is usally taken care of by their parent widget. Or is your combobox a window of its own? – Kevin Krammer Oct 26 '16 at 15:36
  • 1
    @Abhi The QPointer doesn't protect your code from memory leaks, it protects you from accessing invalid pointer to QObject. I think you are confusing it with QSharedPointer – senx Jan 29 '17 at 16:36

1 Answers1

0

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.