Slots for main.cpp
-
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();
}
@ -
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.
-
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();
}
@main window file @#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
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?
-
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.