PDA

View Full Version : hello, a beginner would like your help



tommy
2nd November 2007, 19:14
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!

pherthyl
2nd November 2007, 20:44
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:


void MyClass::myExit() {
cout << "in myexit" << endl;
qApp->quit();
};

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.

tommy
2nd November 2007, 21:24
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

marcel
2nd November 2007, 21:33
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.

tommy
2nd November 2007, 22:41
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.

marcel
2nd November 2007, 22:48
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.