PDA

View Full Version : QObject::connect



littlepig
9th February 2011, 12:34
Hi there

I'm having a problem to connect QObjects... I've searched on web a lot and none of the finds seen to help the problem.

I have a class



class MainWindow : public QMainWindow
{
Q_OBJECT
/* code here */
public slots:
void changeWindow(int);
}


another class



class MainMenu : public QWidget
{
Q_OBJECT
private:
QPushButton *optionsB;
/* code here */
public:
void setup(MainWindow*window);
};


where setup have:



void MainMenu::setup(MainWindow* mainWindow)
{
/* code here */


QSignalMapper* signalMapper = new QSignalMapper(this);
signalMapper->setMapping(optionsB, int(1));

QObject::connect(optionsB, SIGNAL(clicked()),signalMapper, SLOT (map()));
QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
}


And the following runtime error:


Object::connect: No such slot MainWindow::changeWindow(int) in /Users/jorgecarleitao/Trabalho_Outros/qt/teste/mainmenu.cpp:50
Object::connect: (receiver name: 'MainWindow')

Can someone explain to me what is wrong here? I'm 2 days searching for this problem and I can't find the solution...

FelixB
9th February 2011, 12:38
can you call any other method of "mainWindow"?

littlepig
9th February 2011, 12:47
yes, I can. In my MainMenu, I have a button quitB that is "QObject::connect(quitB, SIGNAL(clicked()), mainWindow, SLOT(close()));" and it works. (and MainMenu is a object created by MainWindow constructor, and initialized via a MainWindow::setupUI().

EDIT:
ah, moc_something.cpp was modified by me (using as a base, an example of one made from qmake), it was not generated from from qmake. I'm not sure it can be the problem, but here it is:



#include "mainwindow.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'mainwindow.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.7.0. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_MainWindow[] = {

// content:
5, // revision
0, // classname
0, 0, // classinfo
0, 0, // methods
0, 0, // properties
0, 0, // enums/sets
0, 0, // constructors
0, // flags
0, // signalCount

0 // eod
};

static const char qt_meta_stringdata_MainWindow[] = {
"MainWindow\0"
};

const QMetaObject MainWindow::staticMetaObject = {
{ &QMainWindow::staticMetaObject, qt_meta_stringdata_MainWindow,
qt_meta_data_MainWindow, 0 }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &MainWindow::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *MainWindow::metaObject() const
{
return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *MainWindow::qt_metacast(const char *_clname)
{
if (!_clname) return 0;
if (!strcmp(_clname, qt_meta_stringdata_MainWindow))
return static_cast<void*>(const_cast< MainWindow*>(this));
return QMainWindow::qt_metacast(_clname);
}

int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
_id = QMainWindow::qt_metacall(_c, _id, _a);
if (_id < 0)
return _id;
return _id;
}

QT_END_MOC_NAMESPACE

FelixB
9th February 2011, 12:52
no, I mean your setup()-Method, like this:


void MainMenu::setup(MainWindow* mainWindow)
{
/* code here */

QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
mainWindow->doSomething()// HERE!!
}

littlepig
9th February 2011, 13:08
Yes, it works:


QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
mainWindow->print();

returns output "Hello World" as expected from my print() definition.

Added after 5 minutes:

Ok, I've found the solution... It was the moc file... It must be generated every time I change a class with Q_OBJECT, because qt functionality is based on it... Thanks FelixB