Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to connect signal and slot.



  • 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);
    }*/
    


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



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



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



  • @JonB Hi ,

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

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



  • @Pablo-J-Rogina Hi,

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



  • @small_bird yes it is.



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



  • @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 , ...);
    


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


Log in to reply