QObject *o = new QObject();QObject *o1 = new QObject();QObject *o = new QObject(o1);qDebug() << o;QSharedPointer<QObject> p(o);delete o1;qDebug() << p.data();
#include <QtWidgets>class MegaWidget : public QWidget{public: MegaWidget() { QVBoxLayout *layoutMain = new QVBoxLayout(); { button = new QPushButton("close if equals"); connect(button, &QPushButton::clicked, this, &MegaWidget::closeIfEquals); layoutMain->addWidget(button); line = new QLineEdit("text"); layoutMain->addWidget(line); } setLayout(layoutMain); show(); } void setOther(MegaWidget *other) { this->other = other; } QString getText() { return line->text(); }public slots: void closeIfEquals() { if (line->text() == other->getText()) delete this; else line->setText(other->getText()); }private: MegaWidget *other; QPushButton *button; QLineEdit *line;};int main(int argc, char** argv){ QApplication app(argc, argv); MegaWidget* w1 = new MegaWidget(); MegaWidget* w2 = new MegaWidget(); w1->setOther(w2); w2->setOther(w1); return app.exec();}
C++ (Qt)public slots: void closeIfEquals() { if (other.isNull()) return; if (line->text() == other->getText()) delete this; else line->setText(other->getText()); } private: QPointer<MegaWidget> other;
C++ (Qt)void closeIfEquals(){ if (other && (line->text() == other->getText())) { other->setOther(0); delete this;...}
C++ (Qt)void closeIfEquals(){ if (other && (line->text() == other->getText())) { delete other; other = 0;...