Results 1 to 6 of 6

Thread: hello, a beginner would like your help

  1. #1
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Lightbulb hello, a beginner would like your help

    Hi all,

    How are you? I just discovered this great site. I'm new to both Qt and C++ (migrated from simple C programming for DOS) and I don't really know what I'm doing. But I really want to learn - so, I started trying. Unfortunately things don't work for me because I'm missing some key points. To get some feel of what's going on, I'm trying to write a code where I use my own function to exit a program. Please be kind enough to look at my code and suggest what I'm doing wrong. When I click on "Exit", the program exits, but "My Exit" won't work.
    The main thing I'm trying to learn here is: How do I implement my own function that does something when a QPushButton is clicked? I guess I always need to define a new object through my own subclass that inherits from QObject? Is that correct?
    Here's my pathetic code that won't work:

    #include <QApplication>
    #include <QLabel>
    #include <QPushButton>
    #include <QHBoxLayout>
    using namespace std;

    class MyClass : public QObject
    {
    Q_OBJECT

    public:
    MyClass (QObject *parent = 0);
    public slots:
    void myExit();
    };

    MyClass::MyClass(QObject *parent)
    : QObject(parent)
    {
    }

    void MyClass::myExit() {exit(1)};

    int main(int argc, char *argv[])
    {

    MyClass a;

    QApplication app(argc, argv);
    QWidget window;

    QHBoxLayout* mainLayout = new QHBoxLayout(&window);
    QPushButton* cancelButton = new QPushButton("My Exit");
    QPushButton* exitButton = new QPushButton("Exit");

    mainLayout->addWidget(cancelButton);
    mainLayout->addWidget(exitButton);

    QObject::connect(cancelButton, SIGNAL(clicked()), &a, SLOT(myExit()));
    QObject::connect(exitButton, SIGNAL(clicked()), &app, SLOT(quit()));

    window.show();
    return app.exec();
    }

    Thanks and have a great day!

  2. #2
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: hello, a beginner would like your help

    Quote Originally Posted by tommy View Post
    Hi all,

    How are you? I just discovered this great site. I'm new to both Qt and C++ (migrated from simple C programming for DOS) and I don't really know what I'm doing. But I really want to learn - so, I started trying. Unfortunately things don't work for me because I'm missing some key points. To get some feel of what's going on, I'm trying to write a code where I use my own function to exit a program. Please be kind enough to look at my code and suggest what I'm doing wrong. When I click on "Exit", the program exits, but "My Exit" won't work.
    The main thing I'm trying to learn here is: How do I implement my own function that does something when a QPushButton is clicked? I guess I always need to define a new object through my own subclass that inherits from QObject? Is that correct?
    Here's my pathetic code that won't work:

    #include <QApplication>
    #include <QLabel>
    #include <QPushButton>
    #include <QHBoxLayout>
    using namespace std;

    class MyClass : public QObject
    {
    Q_OBJECT

    public:
    MyClass (QObject *parent = 0);
    public slots:
    void myExit();
    };

    MyClass::MyClass(QObject *parent)
    : QObject(parent)
    {
    }

    void MyClass::myExit() {exit(1)};
    Looks ok to me. Try this:

    Qt Code:
    1. void MyClass::myExit() {
    2. cout << "in myexit" << endl;
    3. qApp->quit();
    4. };
    To copy to clipboard, switch view to plain text mode 

    See if it prints out the message (and quits). Also check if qt prints out any warnings to the console about connecting signals/slots when you run your program.

  3. #3
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: hello, a beginner would like your help

    Hi pherthyl,
    Thanks a lot for responding to me. I made the change but now I'm getting lots of linker errors. Things like:
    In function `ZN7MyClassC2EP7QObject':
    [Linker error] undefined reference to `vtable for MyClass'
    Could it be that there's something wring in the signals/slots statement?
    Tommy

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hello, a beginner would like your help

    If you're using gcc that's most likely because you implemented two classes that have the Q_OBJECT macro in the same file.

    This is like the third time a user reports this error on this forum. I'll have to look into it and maybe find a solution.

    In the mean time you can create separate cpp files for the two classes and it should work.

  5. #5
    Join Date
    Nov 2007
    Posts
    103
    Thanks
    71
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: hello, a beginner would like your help

    Hi Marcel,

    Thanks a lot for your help. I'm using Dev-C++ compiler (essentially mingw). I don't know if that makes a difference?
    I'm only starting to learn Qt and some of my questions may be quite stupid but how do you use MOC? I don't know anyhting about that and that could be why I'm not getting things to work. I am not a command line person at all, don't know how to use it with Dev-C++.
    Thanks again.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: hello, a beginner would like your help

    You don't have to call moc manually. QMake will add all the necessary references to it in the generated makefile and mingw32-make will execute it.

Similar Threads

  1. Qfiledialog Problem - (Beginner)
    By kingslee in forum Qt Tools
    Replies: 3
    Last Post: 12th October 2006, 00:00
  2. Beginner C++ question
    By masoroso in forum General Programming
    Replies: 2
    Last Post: 19th April 2006, 15:15
  3. QT4 beginner Fatal Error
    By Remyfr in forum Installation and Deployment
    Replies: 3
    Last Post: 11th March 2006, 02:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.