0

I have been working on learning C++ and Qt4 recently, but I have hit a stumbling block.

I have the following class and implementation:

class Window : public QWidget
{
public:
    Window();

public slots:
    void run();

private:
    //...
};

and

Window::Window()
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

However, when I attempt to build and run it, although it builds just fine, it immediately exits out with the message

Object::connect: No such slot QWidget::run()

Unless I did something wrong, Qt does not seem to be recognizing the slot run()

Could anyone please help?


Update:

The code is now:

class Window : public QWidget
{
    Q_OBJECT
public:
    Window(QWidget *parent = 0);

public slots:
    void run();

private:
    //...
};

and

Window::Window(QWidget *parent) : QWidget(parent)
{
    //...

    connect(runBtn,SIGNAL(clicked()),this,SLOT(run()));

    //...
}
Window::run()
{
    //...
}

The program still "unexpectedly finished", but no longer tell me that there is no such thing as QWidget::run()

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
  • Is this code *straight* copypaste from your actual code ? Cuz the error you describe usually happens if your run() slot has parameter but you are not defining in in SLOT macro? That is, that same error at runtime, not that crash. Also, you should call the parents constructor too. – rasjani Sep 08 '09 at 20:33
  • We can't tell you why it crashes from just the code you've pasted. There is probably a bug in some other code you've written but not shown. – rohanpm Sep 09 '09 at 01:01
  • "public slots:", how does that work? never seen that syntax? – Viktor Sehr Sep 09 '09 at 07:30
  • don't forget to add the header to HEADERS in your *.pro file, and re-run qmake – elcuco Sep 11 '09 at 23:57

4 Answers4

8

Possibly you have forgotten a Q_OBJECT macro in your Window class?

class Window : public QWidget
{
Q_OBJECT
public:
    Window()
...
Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • Apparently, yes I have. After I added it, though, it still crashed immediately, and exited with code 0. – Austin Hyde Sep 08 '09 at 16:47
  • @Austin, don't forget to re-run qmake after doing this, as it will need to add your file to the list of items to run the MOC on, which will also affect the behavior at runtime. – Caleb Huitt - cjhuitt Sep 08 '09 at 17:18
  • Do you have a stacktrace? Anyway, I recommend reading the Qt tutorials, which are part of the Qt Documentation. They are very well written and go step by step. Additionally there are a lot of examples for the use of the most common classes which are worth a look too. – Gunther Piez Sep 08 '09 at 17:19
  • I am using the qt creator IDE, so (I believe) it does this automatically on build. I ran qmake manually, but it is still crashing, minus the error about "No such slot". – Austin Hyde Sep 08 '09 at 17:21
  • @drhirsch I have indeed read through the tutorials, and have even gone as far to skim some of the documentation and look at all the pertinent examples. – Austin Hyde Sep 08 '09 at 17:23
  • @Austin, if you are using QtCreator, you should be able to debug the application and see what is going on when it crashes. More information would be very helpful on this on. – Caleb Huitt - cjhuitt Sep 08 '09 at 22:46
2

Well I was having this problem too and could find no help on-line. I found out that I was forgetting to delete the moc_* files before recompiling and it was using the old moc files to create the executable file. This caused it to not know about any new slots I had coded. I would check that if all the rest of these suggestions failed.

Sometimes the simplest solution is the best solution...

Brett
  • 21
  • 1
1

What is runBtn, and how is it created? If it's created as part of a ui file, are you calling setupUi()? How is your window class being created? You seem to have omitted some code (// ...) which may be where the error is.

The best advice I can give to to try and reduce your problem to a very small compilable example. This helps for two reasons:

  1. It helps you diagnose the problem, since there'll be less code to look at.
  2. If you still need our help, it'll help us since we will have a complete codebase we can download, compile and debug. Often the problem is not where you think it is.

Hope this helps.

Thomi
  • 11,647
  • 13
  • 72
  • 110
0

Looks like runBtn is not instantiated at the time connect is called -- as implied by one of the other answers.

Use breakpoints to check where the crash is happening.

Sam Dutton
  • 14,775
  • 6
  • 54
  • 64