Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to Get CurrentWidget ObjectName
Forum Updated to NodeBB v4.3 + New Features

How to Get CurrentWidget ObjectName

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1
    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

    Do what you want.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

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

      (Z(:^

      Taz742T 1 Reply Last reply
      0
      • sierdzioS sierdzio

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

        Taz742T Offline
        Taz742T Offline
        Taz742
        wrote on last edited by
        #3

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

        Do what you want.

        1 Reply Last reply
        2
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

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

          (Z(:^

          Taz742T 1 Reply Last reply
          3
          • sierdzioS sierdzio

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

            Taz742T Offline
            Taz742T Offline
            Taz742
            wrote on last edited by
            #5

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

            Do what you want.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved