Results 1 to 5 of 5

Thread: QObject::connect

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QObject::connect

    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

    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. /* code here */
    5. public slots:
    6. void changeWindow(int);
    7. }
    To copy to clipboard, switch view to plain text mode 

    another class

    Qt Code:
    1. class MainMenu : public QWidget
    2. {
    3. Q_OBJECT
    4. private:
    5. QPushButton *optionsB;
    6. /* code here */
    7. public:
    8. void setup(MainWindow*window);
    9. };
    To copy to clipboard, switch view to plain text mode 

    where setup have:

    Qt Code:
    1. void MainMenu::setup(MainWindow* mainWindow)
    2. {
    3. /* code here */
    4.  
    5.  
    6. QSignalMapper* signalMapper = new QSignalMapper(this);
    7. signalMapper->setMapping(optionsB, int(1));
    8.  
    9. QObject::connect(optionsB, SIGNAL(clicked()),signalMapper, SLOT (map()));
    10. QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
    11. }
    To copy to clipboard, switch view to plain text mode 

    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...

  2. #2
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::connect

    can you call any other method of "mainWindow"?

  3. The following user says thank you to FelixB for this useful post:

    littlepig (9th February 2011)

  4. #3
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QObject::connect

    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:

    Qt Code:
    1. #include "mainwindow.h"
    2. #if !defined(Q_MOC_OUTPUT_REVISION)
    3. #error "The header file 'mainwindow.h' doesn't include <QObject>."
    4. #elif Q_MOC_OUTPUT_REVISION != 62
    5. #error "This file was generated using the moc from 4.7.0. It"
    6. #error "cannot be used with the include files from this version of Qt."
    7. #error "(The moc has changed too much.)"
    8. #endif
    9.  
    10. QT_BEGIN_MOC_NAMESPACE
    11. static const uint qt_meta_data_MainWindow[] = {
    12.  
    13. // content:
    14. 5, // revision
    15. 0, // classname
    16. 0, 0, // classinfo
    17. 0, 0, // methods
    18. 0, 0, // properties
    19. 0, 0, // enums/sets
    20. 0, 0, // constructors
    21. 0, // flags
    22. 0, // signalCount
    23.  
    24. 0 // eod
    25. };
    26.  
    27. static const char qt_meta_stringdata_MainWindow[] = {
    28. "MainWindow\0"
    29. };
    30.  
    31. const QMetaObject MainWindow::staticMetaObject = {
    32. { &QMainWindow::staticMetaObject, qt_meta_stringdata_MainWindow,
    33. qt_meta_data_MainWindow, 0 }
    34. };
    35.  
    36. #ifdef Q_NO_DATA_RELOCATION
    37. const QMetaObject &MainWindow::getStaticMetaObject() { return staticMetaObject; }
    38. #endif //Q_NO_DATA_RELOCATION
    39.  
    40. const QMetaObject *MainWindow::metaObject() const
    41. {
    42. return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
    43. }
    44.  
    45. void *MainWindow::qt_metacast(const char *_clname)
    46. {
    47. if (!_clname) return 0;
    48. if (!strcmp(_clname, qt_meta_stringdata_MainWindow))
    49. return static_cast<void*>(const_cast< MainWindow*>(this));
    50. return QMainWindow::qt_metacast(_clname);
    51. }
    52.  
    53. int MainWindow::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    54. {
    55. _id = QMainWindow::qt_metacall(_c, _id, _a);
    56. if (_id < 0)
    57. return _id;
    58. return _id;
    59. }
    60.  
    61. QT_END_MOC_NAMESPACE
    To copy to clipboard, switch view to plain text mode 
    Last edited by littlepig; 9th February 2011 at 11:56.

  5. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject::connect

    no, I mean your setup()-Method, like this:

    Qt Code:
    1. void MainMenu::setup(MainWindow* mainWindow)
    2. {
    3. /* code here */
    4.  
    5. QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
    6. mainWindow->doSomething()// HERE!!
    7. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QObject::connect

    Yes, it works:

    Qt Code:
    1. QObject::connect(signalMapper, SIGNAL(mapped(int)), mainWindow, SLOT(changeWindow(int))); // line 50
    2. mainWindow->print();
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by littlepig; 9th February 2011 at 12:08.

Similar Threads

  1. [Help] QObject::connect
    By vinny gracindo in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 13:26
  2. QObject::connect: No such signal
    By caseyong in forum Qt Programming
    Replies: 5
    Last Post: 19th February 2008, 07:23
  3. Replies: 4
    Last Post: 10th November 2006, 15:38
  4. QStringList in QObject::connect
    By DPinLV in forum Qt Programming
    Replies: 6
    Last Post: 6th September 2006, 17:01

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.