Important: Please read the Qt Code of Conduct -

Private slots: quit(); not working

  • Hi,

    I'm pretty new to QT4, so I'm trying to do this tutorial:

    It all goes well, untill the GUI is put in a class. Even though I could copy almost all code, I just can't get it to work. My code so far:

    #include <QtGui>
    #include "notepad.h"

    int main(int argc, char *argv[])
    QApplication app(argc, argv);

        Notepad window;;
    return app.exec&#40;&#41;;


    #ifndef NOTEPAD_H
    #define NOTEPAD_H

    #include <QtGui>

    class Notepad : public QWidget


    private slots:
    void quit();

    QTextEdit *textEdit;
    QPushButton *quitButton;

    #endif // NOTEPAD_H

    notepad.cpp (just with constructor):

    #include "notepad.h"

    // Constructor

    textEdit = new QTextEdit;
    quitButton = new QPushButton(tr("Quit"));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(quit()));
    QVBoxLayout *layout = new QVBoxLayout;


    The errors i get is:
    moc_notepad.o:: In function Notepad::qt_metacall(QMetaObject::Call, int, void**)': moc_notepad.cpp:72: error: undefined reference toNotepad::quit()'
    :: error: collect2: ld returned 1 exit status

    Can someone please explain what I'm doing wrong? Thank you very much!!

    fixed wrong link to tutorial

  • Class QWidget does not have a slot quit. If you did not define one yourself (which is very likely) your subclass dosn't either. So the connect in line 9 of the cpp cannot succeed.

    You probably want to connect to slot quit() of the global QApplication object (available via qApp) or do in the main function, before calling app.exec():

    connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));

  • I'm sorry, I don't really understand what you mean. I tried adding that line of code to my main file but then it says 'connect' is not declared in this scope.

    I made an error with copy/pasting the link to the tutorial, it must be this one:
    About halfway the page it's being explained. I'm supposed to get a confirmation windows when pressing the quit button, but I don't see how that's implemented...

  • Ah, I see. Unfortunately, the tutorial has some serious oddities.

    Have a look at my forum post "Oddities in Getting Started Programming with Qt": to see the missing methods and some other comments.

  • Ah, that's just great :( I guess it's better to just try another tutorial then... Can you recommend me a tutorial or website which teaches me the basics? It doesn't necessarily has to be an official one from Nokia, but it would be nice if it is complete and a working source code is available...

  • That tutorial page isn't very in-depth at all. You're supposed to implement a slot. Therefore, do it. You could (for instance) close() the window, print something on the console, etc.; since it's a tutorial, don't be afraid of experimenting!

  • The tutorial is quit ok if you adhere to my comments in the mentioned forum thread. As a programmer, you should be able to put the things together.

  • Alright, thanks for the help! If I run into any other oddities I'll post them in Volker's topic :)

  • A tutorial is suppose to help people unfamiliar with something get up to some sort of speed with it.
    Leaving out critical items from the tutorial is not helpful.

    I followed the link to find a solution to the same issue posted above and there was none.

    So - there are at least two (2) people stuck with this tutorial. If I cannot get this to work, what hope is there using the QtCreator GUI?

    Not everyone was born with an expert level of coding GUI interfaces.

  • lapin, the tutorial was fixed in the meanwhile. This thread is over a year old.
    Please create a new thread and post your problem there.

  • I downloaded Qt a few days ago.

    This is the tutorial I am following:

    Please provide me with a pointer to the fixed version. I'd like to get out of the hole that I'm in.


  • Moderators

    bq. If I cannot get this to work, what hope is there using the QtCreator GUI?

    A little melodramatic, don't you think?

    Allow me a quick rant, please. There are plenty of tutorials and examples around. While not everyone was born with an expert level of coding GUI interfaces (none were, as far as I can tell) everyone who has ever become an expert was able to use the resources available to build their knowledge. If you're willing to give up all hope because one particular tutorial doesn't meet your standards, then I think that says more about your ambition than it does about the tutorial.

    With that said, though, please do look around at the numerous other examples and tutorials that exist (a search on this site should point you to a number of them) and give it a try. If you have specific problems or questions, or if you get stuck somewhere, by all means post a message on the forums. There are tons of us on here who are more than happy to do our best to help you learn. That's what our community is all about. But you have to be constructive in your questions before we can be constructive in our answers.

    We are here to help!

  • [quote author="lapin" date="1336599157"]This is the tutorial I am following:
    Please provide me with a pointer to the fixed version.[/quote]

    I'll give you a hint: The current Qt version is 4.8. Maybe you can figure out the link to the latest version yourself ;)

Log in to reply