0

I came across an error which says I have an undefined reference to my slot. The relevant section from my header file is:

  class Window : public QWidget   {
     Q_OBJECT
     .....    public slots:
     void quit():
     ........... }

and from my implementation file is:

Window::Window() { ...... //Setting up Pushbutton

  button1 = new QPushButton("Quit");
  button1->show();

 //Connecting
 connect(button1, SIGNAL(clicked()), this,  SLOT(quit())); 
 ....... }

The error I get on compilation is:

undefined reference to `Window::quit()'

However, I believed that my use of 'this' in the connection code would make a defined reference to this. It has for my previous signals and slots when making connections. Also, I have used the 'quit' slot in a main window application - so I know that's a relevant slot to use.

From searching the forums, the problem is usually resolved by the use of 'this' in the connection part of the code - so I haven't been able to find a solution.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
moker
  • 1

3 Answers3

1

You have declared method called quit (public slot), but you never defined it. You need to add definition

void Window::quit()
{
    // do your quit stuff here
}
Kamil Klimek
  • 12,884
  • 2
  • 43
  • 58
  • Thanks for that, it was my thought that 'quit()' was built in - as I had used it as a 'main.cpp' SLOT in the past. – moker Apr 13 '16 at 15:41
0

Did you implement Window::quit() slot in your class?

There is a QApplication::quit, QWindow::close QWindow::hide, they are built-in. Anything different should be implemented manually.

UPD:

If you read Qt Assistant, you'll see, that there's no QWindow::close as well as QWindow::quit.

That's because you've generally got an application with multiple windows, which you can show or hide, destroy them at most. To stop the application, you use it's quit method, all windows will be closed, destroyed and your program terminates. Probably, that's what you want, if anything's needed to be done with the window itself, there's quite different story.

So, as talamaki suggested:

connect(button1, SIGNAL(clicked()), qApp,  SLOT(quit())); 

Again: don't declare qt built-in slots, if compiler fails, it means there's no method in the class you are trying to use.

MasterAler
  • 1,614
  • 3
  • 23
  • 35
  • I have actually tried that, I will keep experimenting until I find something that works. Thanks for the reply. – moker Apr 13 '16 at 15:28
  • So, do you want to use a built-in (no code in your class required) or your own (you define it in header, implement in cpp) slot? – MasterAler Apr 13 '16 at 15:31
  • I was just wanting to use the built in code, as I simply just want to close the widget. This is because I have also used it before, successfully in a main application file when I was getting to grips with the basics of Qt. I initially tried to implement quit without any definition in the header file, but was given an error, so then I tried to implemented it by declaring quit as a public slot 'void slot quit();', but that gives the error detailed in the question. Very simple, so a funny problem to have. – moker Apr 13 '16 at 15:41
0

Your button's clicked signal can be connected directly to the application's quit slot. The QApplication object is accessible through the instance() function that returns a pointer equivalent to the global qApp pointer.

#include <QApplication>

connect(button1, SIGNAL(clicked()), QApplication::instance(), SLOT(quit()));
talamaki
  • 5,324
  • 1
  • 27
  • 40