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.
  • T Offline
    T Offline
    Taz742
    wrote on 8 Aug 2017, 08:55 last edited by Taz742 8 Aug 2017, 08:57
    #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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 8 Aug 2017, 10:52 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(:^

      T 1 Reply Last reply 8 Aug 2017, 10:57
      0
      • S sierdzio
        8 Aug 2017, 10:52

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

        T Offline
        T Offline
        Taz742
        wrote on 8 Aug 2017, 10:57 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
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 8 Aug 2017, 11:06 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(:^

          T 1 Reply Last reply 8 Aug 2017, 11:11
          3
          • S sierdzio
            8 Aug 2017, 11:06

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

            T Offline
            T Offline
            Taz742
            wrote on 8 Aug 2017, 11:11 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

            1/5

            8 Aug 2017, 08:55

            • Login

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