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 connect signal and slot.
QtWS25 Last Chance

How to connect signal and slot.

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 660 Views
  • 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.
  • A Offline
    A Offline
    Ashutosh_Sachdeva
    wrote on last edited by aha_1980
    #1

    Hello Folks,

    I'm using the connect function. There is no error in the compilation of code but after clicking on the action(i.e Search) it's not executing the search function body. I'm not able to find where i'm wrong.

    Any suggestion will be helpful.

    Thanks in Advance.

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
       // QObject::connect(this, SIGNAL(triggered(QAction*)),this, SLOT(setDefaultAction(QAction*)));
    
       QMenu *Searchmenu = new QMenu(this);
        Search =  new QAction("Search", this);
        Smartplay =   new QAction("SmartPlay", this);
        Searchmenu->addAction("Search");
        QObject::connect(Search, SIGNAL(triggered()),
                                   this, SLOT(search()));
        Searchmenu->addAction("SmartPlay");
    
        QMenu *Backupmenu = new QMenu(this);
        Backup =   new QAction("Backup", this);
        Backupmenu->addAction("Backup");
        //pushButton = new QPushButton(this);
        //pushButton->setMenu(menu);
        ui->pushButton->setMenu(Searchmenu);
        ui->pushButton_2->setMenu(Backupmenu);
    
    
    
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::search()
    {
        qDebug()<< "MainWindow::Welcome to ssearch";
    }
    
    /*
    void Ui_MainWindow::myfunction(){
        menu = new QMenu(this);
    
    
        Action1 =  new QAction("Testaaaaaaaaaaaaaaaaaaaaa", this);
        Action2 =   new QAction("Test1", this);
        Action3 =   new QAction("Test2", this);
        menu->addAction("Testaaaaaaaaaaaaaaaaaaaaa");
        menu->addAction("Test1");
        menu->addAction("Test2");
    
        pushButton = new QPushButton(this);
        pushButton->setMenu(menu);
    }*/
    
    JonBJ Pablo J. RoginaP small_birdS 3 Replies Last reply
    0
    • A Ashutosh_Sachdeva

      Hello Folks,

      I'm using the connect function. There is no error in the compilation of code but after clicking on the action(i.e Search) it's not executing the search function body. I'm not able to find where i'm wrong.

      Any suggestion will be helpful.

      Thanks in Advance.

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
         // QObject::connect(this, SIGNAL(triggered(QAction*)),this, SLOT(setDefaultAction(QAction*)));
      
         QMenu *Searchmenu = new QMenu(this);
          Search =  new QAction("Search", this);
          Smartplay =   new QAction("SmartPlay", this);
          Searchmenu->addAction("Search");
          QObject::connect(Search, SIGNAL(triggered()),
                                     this, SLOT(search()));
          Searchmenu->addAction("SmartPlay");
      
          QMenu *Backupmenu = new QMenu(this);
          Backup =   new QAction("Backup", this);
          Backupmenu->addAction("Backup");
          //pushButton = new QPushButton(this);
          //pushButton->setMenu(menu);
          ui->pushButton->setMenu(Searchmenu);
          ui->pushButton_2->setMenu(Backupmenu);
      
      
      
      }
      
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::search()
      {
          qDebug()<< "MainWindow::Welcome to ssearch";
      }
      
      /*
      void Ui_MainWindow::myfunction(){
          menu = new QMenu(this);
      
      
          Action1 =  new QAction("Testaaaaaaaaaaaaaaaaaaaaa", this);
          Action2 =   new QAction("Test1", this);
          Action3 =   new QAction("Test2", this);
          menu->addAction("Testaaaaaaaaaaaaaaaaaaaaa");
          menu->addAction("Test1");
          menu->addAction("Test2");
      
          pushButton = new QPushButton(this);
          pushButton->setMenu(menu);
      }*/
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Ashutosh_Sachdeva
      Guesses:

      Have you declared search() under slots in the .h file? I don't know if failing to do that would stop things working.

      Search = new QAction("Search", this);
      
      Searchmenu->addAction("Search");
      
      QObject::connect(Search, SIGNAL(triggered()),
      this, SLOT(search()));
      

      Aren't you supposed to add the Search QAction you have created here to the menu? I go:

      action = menu->addAction(text);
      

      and then connect that action.triggered. I'm struggling to see how your added menu action has any relation to the QAction whose signal you are connecting?

      A 2 Replies Last reply
      4
      • A Ashutosh_Sachdeva

        Hello Folks,

        I'm using the connect function. There is no error in the compilation of code but after clicking on the action(i.e Search) it's not executing the search function body. I'm not able to find where i'm wrong.

        Any suggestion will be helpful.

        Thanks in Advance.

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
           // QObject::connect(this, SIGNAL(triggered(QAction*)),this, SLOT(setDefaultAction(QAction*)));
        
           QMenu *Searchmenu = new QMenu(this);
            Search =  new QAction("Search", this);
            Smartplay =   new QAction("SmartPlay", this);
            Searchmenu->addAction("Search");
            QObject::connect(Search, SIGNAL(triggered()),
                                       this, SLOT(search()));
            Searchmenu->addAction("SmartPlay");
        
            QMenu *Backupmenu = new QMenu(this);
            Backup =   new QAction("Backup", this);
            Backupmenu->addAction("Backup");
            //pushButton = new QPushButton(this);
            //pushButton->setMenu(menu);
            ui->pushButton->setMenu(Searchmenu);
            ui->pushButton_2->setMenu(Backupmenu);
        
        
        
        }
        
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        void MainWindow::search()
        {
            qDebug()<< "MainWindow::Welcome to ssearch";
        }
        
        /*
        void Ui_MainWindow::myfunction(){
            menu = new QMenu(this);
        
        
            Action1 =  new QAction("Testaaaaaaaaaaaaaaaaaaaaa", this);
            Action2 =   new QAction("Test1", this);
            Action3 =   new QAction("Test2", this);
            menu->addAction("Testaaaaaaaaaaaaaaaaaaaaa");
            menu->addAction("Test1");
            menu->addAction("Test2");
        
            pushButton = new QPushButton(this);
            pushButton->setMenu(menu);
        }*/
        
        Pablo J. RoginaP Offline
        Pablo J. RoginaP Offline
        Pablo J. Rogina
        wrote on last edited by
        #3

        @Ashutosh_Sachdeva said in How to connect signal and Slot.:

        I'm using the connect function. There is no error in the compilation of code

        Please use the new syntax for signals & slots, which provides for compile time checks.

        Upvote the answer(s) that helped you solve the issue
        Use "Topic Tools" button to mark your post as Solved
        Add screenshots via postimage.org
        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

        A 1 Reply Last reply
        5
        • A Ashutosh_Sachdeva

          Hello Folks,

          I'm using the connect function. There is no error in the compilation of code but after clicking on the action(i.e Search) it's not executing the search function body. I'm not able to find where i'm wrong.

          Any suggestion will be helpful.

          Thanks in Advance.

          #include "mainwindow.h"
          #include "ui_mainwindow.h"
          
          
          
          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
             // QObject::connect(this, SIGNAL(triggered(QAction*)),this, SLOT(setDefaultAction(QAction*)));
          
             QMenu *Searchmenu = new QMenu(this);
              Search =  new QAction("Search", this);
              Smartplay =   new QAction("SmartPlay", this);
              Searchmenu->addAction("Search");
              QObject::connect(Search, SIGNAL(triggered()),
                                         this, SLOT(search()));
              Searchmenu->addAction("SmartPlay");
          
              QMenu *Backupmenu = new QMenu(this);
              Backup =   new QAction("Backup", this);
              Backupmenu->addAction("Backup");
              //pushButton = new QPushButton(this);
              //pushButton->setMenu(menu);
              ui->pushButton->setMenu(Searchmenu);
              ui->pushButton_2->setMenu(Backupmenu);
          
          
          
          }
          
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          
          void MainWindow::search()
          {
              qDebug()<< "MainWindow::Welcome to ssearch";
          }
          
          /*
          void Ui_MainWindow::myfunction(){
              menu = new QMenu(this);
          
          
              Action1 =  new QAction("Testaaaaaaaaaaaaaaaaaaaaa", this);
              Action2 =   new QAction("Test1", this);
              Action3 =   new QAction("Test2", this);
              menu->addAction("Testaaaaaaaaaaaaaaaaaaaaa");
              menu->addAction("Test1");
              menu->addAction("Test2");
          
              pushButton = new QPushButton(this);
              pushButton->setMenu(menu);
          }*/
          
          small_birdS Offline
          small_birdS Offline
          small_bird
          wrote on last edited by
          #4

          @Ashutosh_Sachdeva Try to finger out weather the search function is declared using slot symbol.

          A 1 Reply Last reply
          0
          • JonBJ JonB

            @Ashutosh_Sachdeva
            Guesses:

            Have you declared search() under slots in the .h file? I don't know if failing to do that would stop things working.

            Search = new QAction("Search", this);
            
            Searchmenu->addAction("Search");
            
            QObject::connect(Search, SIGNAL(triggered()),
            this, SLOT(search()));
            

            Aren't you supposed to add the Search QAction you have created here to the menu? I go:

            action = menu->addAction(text);
            

            and then connect that action.triggered. I'm struggling to see how your added menu action has any relation to the QAction whose signal you are connecting?

            A Offline
            A Offline
            Ashutosh_Sachdeva
            wrote on last edited by
            #5

            @JonB Hi ,

            i'e declared search as a slot in main file.

            Can u please give a brief about " action = menu->addAction(text);".

            1 Reply Last reply
            0
            • Pablo J. RoginaP Pablo J. Rogina

              @Ashutosh_Sachdeva said in How to connect signal and Slot.:

              I'm using the connect function. There is no error in the compilation of code

              Please use the new syntax for signals & slots, which provides for compile time checks.

              A Offline
              A Offline
              Ashutosh_Sachdeva
              wrote on last edited by
              #6

              @Pablo-J-Rogina Hi,

              Tried but it's showing error. If possible can u please tell me right syntax or my code

              Pablo J. RoginaP 1 Reply Last reply
              0
              • small_birdS small_bird

                @Ashutosh_Sachdeva Try to finger out weather the search function is declared using slot symbol.

                A Offline
                A Offline
                Ashutosh_Sachdeva
                wrote on last edited by
                #7

                @small_bird yes it is.

                1 Reply Last reply
                0
                • JonBJ JonB

                  @Ashutosh_Sachdeva
                  Guesses:

                  Have you declared search() under slots in the .h file? I don't know if failing to do that would stop things working.

                  Search = new QAction("Search", this);
                  
                  Searchmenu->addAction("Search");
                  
                  QObject::connect(Search, SIGNAL(triggered()),
                  this, SLOT(search()));
                  

                  Aren't you supposed to add the Search QAction you have created here to the menu? I go:

                  action = menu->addAction(text);
                  

                  and then connect that action.triggered. I'm struggling to see how your added menu action has any relation to the QAction whose signal you are connecting?

                  A Offline
                  A Offline
                  Ashutosh_Sachdeva
                  wrote on last edited by
                  #8

                  @JonB said in How to connect signal and Slot.:

                  action = menu->addAction(text);

                  Hi its triggering after trying this "Search = Searchmenu->addAction("Search");"

                  Thank you for the solution. But still i didn't understood thi syntax properly so please give a brief explanation of this that will be helpful.

                  Thanks

                  JonBJ 1 Reply Last reply
                  0
                  • A Ashutosh_Sachdeva

                    @JonB said in How to connect signal and Slot.:

                    action = menu->addAction(text);

                    Hi its triggering after trying this "Search = Searchmenu->addAction("Search");"

                    Thank you for the solution. But still i didn't understood thi syntax properly so please give a brief explanation of this that will be helpful.

                    Thanks

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @Ashutosh_Sachdeva
                    Sorry, I don't understand, there is no "syntax" to explain.

                    When you call https://doc.qt.io/qt-5/qmenu.html#addAction it puts the QAction onto the QMenu and returns a pointer to it for you to access it and set its slot.

                    What you did:

                        Search =  new QAction("Search", this);
                        Searchmenu->addAction("Search");
                    

                    was create one QAction your self via new, and a completely separate one via addAction(). You are somehow thinking because they both have the text "Search" that they are the same action, but they're not.

                    You must do one of the following:

                        searchAction = searchMenu->addAction("Search");
                        QObject::connect(searchAction, ...);
                    

                    or

                        searchAction =  new QAction("Search", this);
                        searchMenu->addAction(searchAction);
                        QObject::connect(searchAction , ...);
                    
                    1 Reply Last reply
                    2
                    • A Ashutosh_Sachdeva

                      @Pablo-J-Rogina Hi,

                      Tried but it's showing error. If possible can u please tell me right syntax or my code

                      Pablo J. RoginaP Offline
                      Pablo J. RoginaP Offline
                      Pablo J. Rogina
                      wrote on last edited by
                      #10

                      @Ashutosh_Sachdeva said in How to connect signal and slot.:

                      Tried but it's showing error.

                      which you didn't show... so how do you intend us to try helping?

                      Upvote the answer(s) that helped you solve the issue
                      Use "Topic Tools" button to mark your post as Solved
                      Add screenshots via postimage.org
                      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                      1 Reply Last reply
                      1

                      • Login

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