PDA

View Full Version : slot issue



stef13013
17th September 2010, 17:21
Hi guys,

Could you tell me why the "about" messagebox doesn't trigger whe I'm doing this :




class Main: public QMainWindow
{
//Q_OBJECT

public:
Main();

public slots:
void about();

private:
QToolBar *m_tb;
QMenu *m_settings;
void createMenubar();
};


void Main::about()
{
QMessageBox::about( this, tr("About"), tr("test") );
}



void Main::createMenubar()
{
QMenu *menu;
QAction *atTop;

atTop = new QAction("Always on top", this);
connect(atTop, SIGNAL(triggered()), this, SLOT(about()));

menu = menuBar()->addMenu(tr("&Settings"));
menu->addAction(atTop);
}



Main::Main(): QMainWindow()
{
createMenubar();
}



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

Main *w = new Main();
w->show();
return a.exec();
}



I cannot find my fault :mad:

Thanks for your help

wysota
17th September 2010, 18:11
Q_OBJECT macro is commented out.

stef13013
17th September 2010, 21:21
Yes, thanks, I did that to avoid the message :
undefined reference to `vtable for Main'

I guess it's a probem with MOC...

Zlatomir
17th September 2010, 21:31
I guess you write all that code in one file?
If so write this at the end (after your code):


//...
#include "main.moc"

Then run Clean All from the Build menu in Qt Creator, and it should run and build (without undefined vtable)

But it's better if you learn the C++ way of writing classes: a .h for declaration and a .cpp file for the implementation (and in that way you don't need to include the file that moc generate for you)

stef13013
17th September 2010, 21:49
Indeed, it was just a "quick and dirty little test"

By the way, you're right Zlatomir, thanks for your answer and advice...