Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Slots for main.cpp

    General and Desktop
    2
    4
    6258
    Loading More Posts
    • 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.
    • S
      sunil.nair last edited by

      Consider this code for main.cpp file. We have a slot quit already available. What are the other already available slots for main.cpp file. Can I create a new slot that can be accessed from the main.cpp file

      @#include "mainwindow.h"
      #include <QApplication>
      #include <QPushButton>
      #include<QHBoxLayout>
      #include<iostream>

      void buttonpress()
      {
      std::cout<<"hello";
      }
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      //MainWindow *w;
      QWidget *w=new QWidget;
      w->setWindowTitle("How many chicken wings");

      QPushButton *button= new QPushButton("Quit the window");
      QObject::connect(button, SIGNAL(clicked()), &a,  SLOT(quit()));
      button->show();
      return a.exec&#40;&#41;;
      

      }
      @

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        There's no such things as slots in main.cpp e.g. the quit slot comes from QApplication. Slots are available where you have objects that provides them e.g. QPushButton's show slot would also be available in the same fashion as QApplication::quit.

        For a complete list of signals and slots for a given class, the most simple thing to do is read the class's documentation.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • S
          sunil.nair last edited by

          Thank you for your reply SGaist. So you are saying that if I have the qApplication header file, i can use the slot in that. Say I include a new header file which contains a different slot.

          @#include "mainwindow.h"
          #include <QApplication>
          #include <QPushButton>
          #include<QHBoxLayout>
          #include<iostream>

          int main(int argc, char *argv[])
          {
          QApplication a(argc, argv);
          MainWindow w;

          QPushButton *button= new QPushButton("Quit the window");
          QObject::connect(button, SIGNAL(clicked()), &a,  SLOT(quit()));
          button->show();
          return a.exec&#40;&#41;;
          

          }
          @

          main window file @#ifndef MAINWINDOW_H
          #define MAINWINDOW_H

          #include <QMainWindow>

          namespace Ui {
          class MainWindow;
          }

          class MainWindow : public QMainWindow
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();

          private slots:
          bool on_pushButton_clicked();

          private:
          Ui::MainWindow *ui;
          };@

          Can I call the the slot on_pushbutton_clicked(); slot of mainwindow from Qobject button pressed connect signal?

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            The header is not enough you need to have an instance of the class you want to use.

            By the way, button being never deleted you have a memory leak.

            Then why the private slot if you want to access it from outside of the class. Note that slots should not have a return value.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • First post
              Last post