Results 1 to 13 of 13

Thread: What do I do wrong

  1. #1
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default What do I do wrong

    Hi Everybody,
    Can someone tell me what is wrong with the code below.All I want to do is connecting an action to a slot which is defined in a derived class.

    I get the following error message saying QMainWindow doesnt have such a slot but code uses a derived class(SMainWindow) which has the rewuired slot.

    Object::connect: No such slot QMainWindow::slotNewGraph() in c:\qt_test\main.cpp:26
    Object::connect: (sender name: 'actionNew')
    Object::connect: (receiver name: 'MainWindow')





    #include "QApplication.h"
    #include "mainactions.h"
    class SMainWindow : public QMainWindow
    {
    private slots:
    void slotNewGraph()
    {
    printf("new Graph\n");
    }
    };

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

    QApplication app(argc,argv);

    Ui::MainWindow ui;
    SMainWindow* mainForm=new SMainWindow;
    ui.setupUi(mainForm);

    QAction* actionNew = mainForm->findChild<QAction*>("actionNew");
    QObject::connect(actionNew, SIGNAL(activated()), mainForm, SLOT(slotNewGraph()));

    mainForm->show();
    return app.exec();
    return 0;
    }

    Thanks in advance!

  2. #2
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    MOC needs to see a slot defined in a header in order to generate the necessary code for it.

    Edit: spelling error.

  3. #3
    Join Date
    Sep 2010
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: What do I do wrong

    At least 1 problem is that you need the Q_OBJECT macro in your class definition.

    Qt Code:
    1. class SMainWindow : public QMainWindow {
    2. Q_OBJECT
    3. private slots:
    4. void slotNewGraph() { printf("new Graph\n"); }
    5. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    u mean just add a
    #define SLOT 1 somewhere in the code?

  5. #5
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    No, the header for your class. it appears to me in the code that you posted that you are defining the class inline in your main source. If this isn't the case, then post more complete code.

  6. #6
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    i did the Q_OBJECT trick
    now I get unresolved symbols. Any Ideas?

    error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall SMainWindow::metaObject(void)const " (?metaObject@SMainWindow@@UBEPBUQMetaObject@@XZ)
    error LNK2001: unresolved external symbol "public: virtual void * __thiscall SMainWindow::qt_metacast(char const *)" (?qt_metacast@SMainWindow@@UAEPAXPBD@Z)
    error LNK2001: unresolved external symbol "public: virtual int __thiscall SMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@SMainWindow@@UAEHW4Call@QMetaObject@ @HPAPAX@Z)
    fatal error LNK1120: 3 unresolved externals

    it is just a test code ,yes an inline class.This is the whole thing + ui header which is automatically genereated

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What do I do wrong

    No, it means add the Q_OBJECT macro to your class and rerun qmake.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Sep 2010
    Posts
    145
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    Inline definitions are (not fine, but permissible) in headers, but simply won't work right with Qt's signal/slot mechanism when defining new slots and defining the class inline in a cpp. MOC needs to see the slot declaration (I originally said "definition" but I am reversing myself but that isn't quite right) in a header.

    Edit: lost train of though.

  9. #9
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    I moved the class definition to a seperate header file , and added Q_OBJECT macro. here is how the code looks like now.
    And I still get the unresolved symbols
    Qt Code:
    1. error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall SMainWindow::metaObject(void)const " (?metaObject@SMainWindow@@UBEPBUQMetaObject@@XZ)
    2. error LNK2001: unresolved external symbol "public: virtual void * __thiscall SMainWindow::qt_metacast(char const *)" (?qt_metacast@SMainWindow@@UAEPAXPBD@Z)
    3. error LNK2001: unresolved external symbol "public: virtual int __thiscall SMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@SMainWindow@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
    4. fatal error LNK1120: 3 unresolved externals
    To copy to clipboard, switch view to plain text mode 


    smainwindow.h
    Qt Code:
    1. #pragma once
    2.  
    3. #include "QApplication.h"
    4. #include "QMainWindow.h"
    5. class SMainWindow:public QMainWindow
    6. {
    7. Q_OBJECT
    8. private slots:
    9. void slotNewGraph();
    10. };
    To copy to clipboard, switch view to plain text mode 
    smainwindow.cpp
    Qt Code:
    1. #include "smainwindow.h"
    2.  
    3. void SMainWindow::slotNewGraph()
    4. {
    5. printf ("new Graph\n");
    6. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "QApplication.h"
    2. #include "frmmain.h"
    3. #include "smainwindow.h"
    4. int _tmain(int argc, char** argv)
    5. {
    6.  
    7. QApplication app(argc,argv);
    8.  
    9. Ui::MainWindow ui;
    10. SMainWindow* mainForm=new SMainWindow;
    11. ui.setupUi(mainForm);
    12.  
    13. QAction* actionNew = mainForm->findChild<QAction*>("actionNew");
    14. QObject::connect(actionNew, SIGNAL(activated()), mainForm, SLOT(slotNewGraph()));
    15.  
    16. mainForm->show();
    17. return app.exec();
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    P.S frmmain.h is a qt designer generated header.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What do I do wrong

    Let me repeat myself - add the Q_OBJECT macro and rerun qmake.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default

    wysote,

    I use vs2008 as compiler and Q_OBJECT is in the SMainWindow class definition.

    Thanks,

    I think it is about MOC ing , whatever it is. I need to do more readings.
    Thanks guys.

    Problem is solved. If you work with visual studio any class that is derived from a QT class has to be moced. then output of the moc operation should be added to cpp file of the derived class (not in header)
    how moc a header
    1) Add qt bin directory to your path
    2)Open cmd and cd to the location of the header
    3)moc header.h > header.cpp
    4)work on your new header.cpp
    Last edited by wysota; 20th October 2010 at 19:23.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: What do I do wrong

    Quote Originally Posted by Arif Bilgin View Post
    If you work with visual studio any class that is derived from a QT class has to be moced.
    No, that's not true.

    how moc a header
    1) Add qt bin directory to your path
    2)Open cmd and cd to the location of the header
    3)moc header.h > header.cpp
    4)work on your new header.cpp
    Or run "qmake -project" and then "qmake -tp vc" when you start developing a project. Then Visual Studio will be mocing files for you by itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Oct 2010
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What do I do wrong

    wysote,
    you are absolutely right !
    These are the steps to automate all mocing and .ui to .h conversion
    1-)Make sure QT Add in for Visual Studio is installed
    2-)Create a a qt project with qmake
    qmake -project
    qmake myproject.pro


    3-)Open Visual Studio | Click on QT menu | Open Qt Project File

    and you are all set , all mocings and uic ings are taken care automatically by QT add in.

    Beautiful!

Similar Threads

  1. i don't know what i do wrong..
    By Hardstyle in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 17:33
  2. what is wrong with my qt ?
    By lwb422 in forum Qt Programming
    Replies: 5
    Last Post: 12th April 2010, 14:07
  3. What's wrong??
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 08:07
  4. What is wrong with this simple example ?
    By igor in forum Qt Programming
    Replies: 10
    Last Post: 16th January 2007, 11:26
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06

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.