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. Slots for main.cpp

Slots for main.cpp

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 6.5k 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.
  • S Offline
    S Offline
    sunil.nair
    wrote on last edited by
    #1

    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
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • S Offline
        S Offline
        sunil.nair
        wrote on last edited by
        #3

        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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0

          • Login

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