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 add slot for a button press ?
Forum Updated to NodeBB v4.3 + New Features

How to add slot for a button press ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 1.7k 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.
  • BhanuKiranChaluvadiB Offline
    BhanuKiranChaluvadiB Offline
    BhanuKiranChaluvadi
    wrote on last edited by BhanuKiranChaluvadi
    #1

    Hi ,

    I want to execute a function whenever the button is pressed.
    I am not using QtGui.

    I have an application and i am trying to manipulate the application with the help of Qt (In this case - whenever i click the button , a signal should generate and in turn execution of slot should take place )

    The code looks like this

    #includes <....>
    #includes <.....>
    ......
    
    void execute_this();
    
    int main()
    {
    
    ...
    ...
    QMainWindow mainWin;
    myApp.setMainWindow(&mainWin);
    QPushButton *button = new QPushButton(&mainWin);
    button->setText("Button");
    // QObject::connect(button, SIGNAL(clicked()), &mainWin, SLOT(execute_this()));
    mainWin.showNormal();
    ...
    ...
    ...
    
    return myApp.exec();
    }
    
    void execute_this()
    {
    std::cout<<"Sample text \n";
    }
    

    How to execute a function whenever i press a button in the Qmainwindow ?

    Please help

    jsulmJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      This should work fine. Hope execute_this function is slot in inside your class. What do you mean by not using QtGui ? Without using this you will not be able to create the button or main window objects

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      5
      • BhanuKiranChaluvadiB BhanuKiranChaluvadi

        Hi ,

        I want to execute a function whenever the button is pressed.
        I am not using QtGui.

        I have an application and i am trying to manipulate the application with the help of Qt (In this case - whenever i click the button , a signal should generate and in turn execution of slot should take place )

        The code looks like this

        #includes <....>
        #includes <.....>
        ......
        
        void execute_this();
        
        int main()
        {
        
        ...
        ...
        QMainWindow mainWin;
        myApp.setMainWindow(&mainWin);
        QPushButton *button = new QPushButton(&mainWin);
        button->setText("Button");
        // QObject::connect(button, SIGNAL(clicked()), &mainWin, SLOT(execute_this()));
        mainWin.showNormal();
        ...
        ...
        ...
        
        return myApp.exec();
        }
        
        void execute_this()
        {
        std::cout<<"Sample text \n";
        }
        

        How to execute a function whenever i press a button in the Qmainwindow ?

        Please help

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @BhanuKiranChaluvadi execute_this has to be a slot. A slot is a method in a class which is derived from QObject and has Q_OBJECT macro. Please take a look at http://doc.qt.io/qt-4.8/signalsandslots.html This is a basic concept in Qt which you should know if you want to use Qt.
        What do you mean by "I am not using QtGui"? If you use QMainWindow and QPushButton then you use GUI.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        3
        • BhanuKiranChaluvadiB Offline
          BhanuKiranChaluvadiB Offline
          BhanuKiranChaluvadi
          wrote on last edited by
          #4

          @jsulm
          @dheerendra

          Thank you very much for the reply.

          I have gone through the documentation and was able to solve the issue..
          The process involved creating a creating a moc file and including in my visual studio project.

          1 Reply Last reply
          1
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            @BhanuKiranChaluvadi

            if the topic is solved make the thread as solved , which will be usefull.
            Cheers,
            Thanks.

            Pradeep Kumar
            Qt,QML Developer

            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