How to Get CurrentWidget ObjectName



  • 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.

    alt text


  • Moderators

    @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?



  • @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();
        }
    }
    
    

  • Moderators

    @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 :-)



  • @sierdzio
    In this case I leave it unchanged, but I know your advice. thank you very much.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.