Results 1 to 6 of 6

Thread: Runtime dynamic linking + Qt4 problem

  1. #1
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Runtime dynamic linking + Qt4 problem

    I have abstract class Interface with one pure virtual method

    void Method(QMenuBar *menuBar, QWidget *parent = 0) const = 0;

    I inherit this class with my plugin class. This method should add one QMenu widget to QMenuBar of app that loads plugin. App that is loadin my plugin inherits QMainWindow. I can load the plugin and call this method and it adds one menu item, but the problem is when I try to connect it with slot.

    1. I have no idea where to put Q_OBJECT, in Interface or in Plugin or boath?
    2. Does my class have to inherit QObject to be able to use signals and slots and wich one?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Runtime dynamic linking + Qt4 problem

    Quote Originally Posted by _Ramirez_
    1. I have no idea where to put Q_OBJECT, in Interface or in Plugin or boath?
    You have put Q_OBJECT in every class in which you define new signals, slots or properties. If you want to use meta data (for example through QMetaObject) or you have a widget plugin, you will need Q_OBJECT too.

    Also sprach Assistant:
    Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObject regardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior
    Quote Originally Posted by _Ramirez_
    2. Does my class have to inherit QObject to be able to use signals and slots and wich one?
    You will probably want to be able to cast your interface to QObject *, so you could use it for example in the connect statement. In such case it must inherit QObject.

  3. #3
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Runtime dynamic linking + Qt4 problem

    Qt Code:
    1. class Interface
    2. {
    3. public:
    4. Interface() {}
    5. virtual ~Interface() {}
    6.  
    7. virtual void Method(QMenuBar* menuBar, QWidget* parent = 0) const = 0;
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Plugin : public Interface
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Plugin();
    7. ~Plugin();
    8.  
    9. void Method(QMenuBar* menuBar, QWidget* parent = 0) const;
    10. private slots:
    11. void TestSlot();
    12. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Plugin::Method(QMenuBar* menuBar, QWidget* parent) const
    2. {
    3. QAction *pluginAction = new QAction(tr("SomeText"), parent);
    4. connect(pluginAction, SIGNAL(triggered()), parent, SLOT(TestSlot()));
    5. QMenu *pluginMenu = new QMenu(tr("Plugin"));
    6. pluginMenu->addAction(pluginAction);
    7.  
    8. menuBar->addMenu(pluginMenu);
    9. }
    To copy to clipboard, switch view to plain text mode 

    I want to use slots in my Plugin so I've added Q_OBJECT but I get these errors:

    error C2039: 'staticMetaObject' : is not a member of 'Interface'
    error C2039: 'qt_metacast' : is not a member of 'Interface'
    error C2039: 'qt_metacall' : is not a member of 'Interface'
    error C3861: 'connect': identifier not found

    My .pro file looks like this:

    TEMPLATE = lib
    CONFIG += qt dll
    HEADERS = Interface.h \
    Plugin.h
    SOURCES = Plugin.cpp

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Runtime dynamic linking + Qt4 problem

    Quote Originally Posted by _Ramirez_
    error C2039: 'staticMetaObject' : is not a member of 'Interface'
    error C2039: 'qt_metacast' : is not a member of 'Interface'
    error C2039: 'qt_metacall' : is not a member of 'Interface'
    error C3861: 'connect': identifier not found
    You can place Q_OBJECT only in classes that inherit QObject, because only QObjects can use signals and slots.

    Try this:
    Qt Code:
    1. class Plugin : public QObject, public Interface /* note the order of super classes */
    2. {
    3. Q_OBJECT
    4. public:
    5. // ...
    6. };
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Runtime dynamic linking + Qt4 problem

    Thanks, it's working now. You were right, it was the order of QObject and Interface. I've tried it the other way and it didn't work. I'm not realy sure that I understand the diference? And one more thing. Receiver for connect has to be this and not my main app, but I don't know who should be parent for my QAction, this (Plugin) or parent that I pass from main app?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Runtime dynamic linking + Qt4 problem

    Quote Originally Posted by _Ramirez_
    I'm not realy sure that I understand the diference?
    It has nothing to do with C++, it's just a limitation of Qt tools.

    Quote Originally Posted by _Ramirez_
    I don't know who should be parent for my QAction, this (Plugin) or parent that I pass from main app?
    If you use the plugin as a parent, this action will be deleted automatically when you delete the plugin, otherwise you will have to delete it by hand.

Similar Threads

  1. linking problem (ffmpeg)
    By kralyk in forum Qt Programming
    Replies: 2
    Last Post: 13th April 2009, 08:35
  2. runtime error, the procedure entry point problem
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2008, 10:44
  3. problem with order of libs during linking
    By minimax in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2008, 11:32
  4. why linking problem with QGLWidget???
    By Shuchi Agrawal in forum Newbie
    Replies: 17
    Last Post: 16th March 2007, 11:45
  5. Dynamic lookup problem
    By jwintz in forum Qt Programming
    Replies: 3
    Last Post: 30th May 2006, 15:19

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.