-1

我正在处理一段代码,我不能 100% 确定它的正确性。请问您能告诉我您对此有何看法吗?(我在 Qt 中编码)

只有一个样本:

CustomWidget *widget; //defined as private

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), this, SLOT(onWidgetClose()));

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), this, SLOT(onWidgetClose()));

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), this, SLOT(onWidgetClose()));


void onWidgetClose(){
   CustomWidget *w = findClosedWidget();
   delete w;
}
4

1 回答 1

1

没有必要编写自己的onWidgetClose-findClosedWidget特别是因为编写正确的代码findClosedWidget将非常重要。QObject::deleteLater而是使用专为此目的设计的插槽:

CustomWidget *widget; //defined as private

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), widget, SLOT(deleteLater()));

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), widget, SLOT(deleteLater()));

widget = new CustomWidget(this);
connect(widget,SIGNAL(onCLose(), widget, SLOT(deleteLater()));
于 2013-05-16T22:01:48.393 回答