QObject::connect: No such slot QApplication::checkText()
-
Hey all!
I am trying to build 2 buttons. With "Quit" you quit the Program , but if you click in " Clicke me" or "Info" it should open a Label with "Hello world"
But I get the error: QObject::connect: No such slot QApplication::checkText()this is my code:
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QtGui>
#include <QWidget>class QPushButton;
class checkText;
class Window : public QWidget
{
public:
explicit Window(QWidget *parent = 0);signals:
void QApplication();
private slots:void checkText();
private:
QPushButton *m_button;
};
#endif // WINDOW_H
###########
main.cpp#include <QApplication>
#include <QPushButton>
#include "window.h"
#include <QLabel>
#include <QtGui>int main(int argc, char **argv)
{
QApplication app (argc, argv);
int)), progressBar, SLOT (setValue(int)));Window window;
window.show();
window.showMaximized();
window.setFixedSize(100, 80);QPushButton *buttonInfo = new QPushButton("Info", &window);
buttonInfo->setGeometry(10, 10, 80, 30);
buttonInfo->show();
QObject::connect(buttonInfo,SIGNAL(clicked()), qApp, SLOT(checkText()));QPushButton *buttonQuit = new QPushButton("Quit", &window);
buttonQuit->setGeometry(10, 40, 80, 30);
buttonQuit->show();QObject::connect(buttonQuit, SIGNAL(clicked()), qApp, SLOT(quit()));
return app.exec();
}#############
window.cpp#include "window.h"
#include <QPushButton>
#include <QApplication>
#include <QToolTip>
#include <QPoint>
#include <QWidget>
#include <QDebug>
#include <QLabel>Window::Window(QWidget *parent) :
QWidget(parent)
{
// Set size of the windowsetFixedSize(100, 50);
//Create and position the button
m_button = new QPushButton("Click me", this);
m_button->setGeometry(10, 10, 80, 30);
m_button->setCheckable(true);m_button->show(); QObject::connect(this, SIGNAL (clicked()), this, SLOT(checkText()));
}
void Window::checkText(){QLabel *label1 = new QLabel();
label1->setText("Hello World");
label1->show();
} -
Hi and welcome to the forums.
You lie to it :)
QObject::connect(buttonInfo,SIGNAL(clicked()), qApp, SLOT(checkText()));
you say that qApp has the checkText function but it does not.it seems to be windows class that has it so
QObject::connect(buttonInfo,SIGNAL(clicked()), &window, SLOT(checkText()));if that windows instance is the one that has
Window::checkText()However, that aside.
why are you creating the buttons in main ?
should they not be in mainwindow or similar?
if they are not part of a form/window, you cannot really see them. -
Thank you for your reply, but it still does not work
I am not quite sure how can create a button in mainwindow and not in main
Well you just open the mainwindows ui file and drag the buttons from left side to it.
it seems you made your own Window class that is just a widget with no UI file ?
Both ways can work but using an UI file make it super easy as you can then simply design it versus hand code it.
You can make a new Widget/window with
and it will have an UI file and allow you just just drag the buttons to it
-
Hi
Ok super.
Well its a good idea to learn the basic by hand coding.
Just as a note. When you use an UI file its just also code.
if you look in the setupUI function you can see the c++ code generated from the UI files.
That can be used to visually build something and then study the code.
This is handy with say with a complex layout.
Also you can mix UI and hand code. no issues.
Just add stuff after the setupUI and it works as expected/normal.