How to Get CurrentWidget ObjectName
-
wrote on 8 Aug 2017, 08:55 last edited by Taz742 8 Aug 2017, 08:57
connect(qApp, SIGNAL(focusChanged(QWidget*, QWidget*)), this, SLOT(WidgetChanged(QWidget*, QWidget*))); void dlgAutomaticSend::WidgetChanged(QWidget *prev, QWidget *) { if(!prev) return; QString objName = prev->objectName(); if (objName == "treeWidget" || objName == "tableWidgetContact" || objName == "tableWidgetTemplate"){ lastWidget = prev; } } void dlgAutomaticSend::closeEvent(QCloseEvent *){ qApp->blockSignals(true); }
It works but when the dialogue starts for the first time its crashed.
Because when I pressed the button then there was no "prev" - widget. -
@Taz742 said in How to Get CurrentWidget ObjectName:
if(!prev)
return;So that guard does not protect you in this case?
Can you show how you declare WidgetChanged() in your header file? Does "prev" default to nullptr?
-
@Taz742 said in How to Get CurrentWidget ObjectName:
if(!prev)
return;So that guard does not protect you in this case?
Can you show how you declare WidgetChanged() in your header file? Does "prev" default to nullptr?
wrote on 8 Aug 2017, 10:57 last edited by@sierdzio said in How to Get CurrentWidget ObjectName:
Can you show how you declare WidgetChanged() in your header file?
it is private slot:
void WidgetChanged(QWidget *prev, QWidget *now);
i solved this.
void dlgAutomaticSend::WidgetChanged(QWidget *prev, QWidget *now) { if(prev == ui->tableWidgetContact){ objName = "tableWidgetContact"; } if(prev == ui->tableWidgetTemplate){ objName = "tableWidgetTemplate"; } if(prev == wdgTree){ objName = "wdgTree"; } } void dlgAutomaticSend::on_btnPlus_clicked() { if (objName == "treeWidget") { wdgTree->AddSLOT(); } else if (objName == "tableWidgetContact") { AddContactSLOT(); } else if (objName == "tableWidgetTemplate") { AddTemplateSLOT(); } } void dlgAutomaticSend::on_btnEdit_clicked() { if (objName == "treeWidget") { wdgTree->EditSLOT(); } else if (objName == "tableWidgetTemplate") { EditTemplateSLOT(); } } void dlgAutomaticSend::on_btnRemove_clicked() { if (objName == "treeWidget") { wdgTree->RemoveSLOT(); } else if (objName == "tableWidgetTemplate") { RemoveTemplateSLOT(); } }
-
@Taz742 said in How to Get CurrentWidget ObjectName:
void WidgetChanged(QWidget *prev, QWidget *now);
perhaps
void WidgetChanged(QWidget *prev = nullptr, QWidget *now = nullptr);
would have worked better.Anyway, your solution is good, too. Congrats and happy coding :-)
-
@Taz742 said in How to Get CurrentWidget ObjectName:
void WidgetChanged(QWidget *prev, QWidget *now);
perhaps
void WidgetChanged(QWidget *prev = nullptr, QWidget *now = nullptr);
would have worked better.Anyway, your solution is good, too. Congrats and happy coding :-)
1/5