Results 1 to 12 of 12

Thread: Item in a DLL not getting events

  1. #1
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Item in a DLL not getting events

    I am having a little trouble with dll´s and items .
    I had some custom items that worked fine.I had an abstract item that inherited QGraphicsItem , an item (it was a node in a diagram ,so I will cal it node) that inherited that abstract custom item ,a text item that was part of the abstract one and inherited QGraphicsTextItem (hopefully you understand this because I didnt explain it very well) and an arrow that inherited from QGraphicsItem.
    My first attempt ,to make it simpler ,was to make a dll that just exported a function that returned a pointer to a node .In the dll ,of course ,there was more code ,but not exported .I could create it ,put it on a scene and view it with a view,call some functions to inicialice it....but the events were not delivered to them .The funny (and annoying) thing is that the other items that I ve created (except for the abstract one,of course) are getting the events just as usual .For example , the node had a filter event for his text item : the text item is getting events ,but they dont go through the filter .
    I tried to also export the item clases I had created ,and link my aplication against the dll (with dynamic link ,because it doesnt run without the dll) :nothing changed .
    The only difference between the clases that dont work and the ones that works (the only one I ve noticed)....well I dont know how to explain it (I m new to dll s) ,so ...lets see:
    http://img228.imageshack.us/img228/2193/dllxc8.jpg
    This is some kind of view of the dll on eclipse .For example ,the arrow class (in arrow.h and arrow.cpp) is working , but the nodofuente class (nodofuente.cpp ,the node) is not working .As you can see (well ,believe me because you dont see the whole dll) the dll doesnt "include" (or whatever) the .h file of the class that is not working .I dont know what this means or even if it means something at all and has something to do with my problem .
    Any help will be really ,really,really apreciated .
    Thanks in advance .
    If God has friends ,then I cant be God.

  2. #2
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Item in a DLL not getting events

    Hi

    Sorry to say, but I cannot get your problem.

    But for using a dll, move all the dll source files to 1 project, create a pro file that creates a lib.

    include the .h file in the application that you want to use the dll in.
    use QLibrary to load the dll. (dynamic linking).

    make sure to put Q_DECL_EXPORT when you compile as library on your class and Q_DECL_IMPORT when you include the header file in your application.

    something like this:
    mylibrary.pro
    Qt Code:
    1. CONFIG += dll
    2. DEFINES += COMPILING_LIBRARY
    3.  
    4. #then comes your sources and headers... remember to add LIBS += mylibrary.lib in your application pro file
    To copy to clipboard, switch view to plain text mode 

    mylibrary.h
    Qt Code:
    1. #ifdef COMPILING_LIBRARY
    2. #define MYLIBRARYEXPORT Q_DECL_EXPORT
    3. #else
    4. #define MYLIBRARYEXPORT Q_DECL_IMPORT
    5. #endif
    6.  
    7. class QFD_USB_LIB_EXPORT MyLibraryInDLL: public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyLibraryInDLL(QWidget *parent);
    13. }
    To copy to clipboard, switch view to plain text mode 

    myapplication.cpp
    Qt Code:
    1. #include "mylibrary.h"
    2. QLibrary *loadmylibrary = new QLibrary("mylibrary", this);
    3. loadmylibrary->setLoadHints(QLibrary::ResolveAllSymbolsHint);
    4. loadmylibrary->load();
    5. if (loadmylibrary->isLoaded() == false)
    6. {
    7. // error didn't load... do something!
    8. }
    To copy to clipboard, switch view to plain text mode 


    cheers,
    Leif

  3. #3
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    Ok .... ok,very interesting .
    I was building the dll in a normal c++ shared library project on eclipse .I did not copy the sources , but told the project to look for them in the correct folder (I think this makes no difference) .So I did "the same" but with the _declspec(dllimport) and stuff .But ,as I said ,I didnt hace to explicit load the dll , because the application automatically loaded it .
    I have one doubt :
    (...)remember to add LIBS += mylibrary.lib in your application pro file (...)
    Did you mean mylibrary.dll?
    And MYLIBRARYEXPORT instead of QFD_USB_LIB_EXPORT?
    Thank you very much .
    If God has friends ,then I cant be God.

  4. #4
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Item in a DLL not getting events

    Sorry... my bad!
    should be like this:

    mylibrary.h
    Qt Code:
    1. #ifdef COMPILING_LIBRARY
    2. #define MYLIBRARYEXPORT Q_DECL_EXPORT
    3. #else
    4. #define MYLIBRARYEXPORT Q_DECL_IMPORT
    5. #endif
    6.  
    7. class MYLIBRARYEXPORT MyLibraryInDLL: public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MyLibraryInDLL(QWidget *parent);
    13. }
    To copy to clipboard, switch view to plain text mode 

    The only reason to load it is if you want to make sure it's loaded before calling a function. As that makes you able to handle a loading-library-error... If not, your app will just crash if there is a problem loading the library. (e.g. the user moved it).

    As you say, you don't have to move the files. It was just a suggestion to keep projects like:
    Application
    --- library1
    --- library2
    --- library3
    --- library3a
    --- library3b

    Just to make it easier for your self if the projects grows and gets big...

    As for the LIBS += mylibrary.lib in the pro file, it's to make the linker find any links to the library. Nothing else...

    cheers,
    Leif

  5. The following user says thank you to luf for this useful post:

    Benne Gesserit (7th August 2008)

  6. #5
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    Quote Originally Posted by luf View Post
    (...)
    As for the LIBS += mylibrary.lib in the pro file, it's to make the linker find any links to the library. Nothing else...
    (...)
    Ok ,lets say I have mylibrary.a and mylibrary.dll .Can the .pro file tell the difference between both files? I mean , after all ,wont it be -lmylibrary in the command? And then it looks for libmylibrary.a or mylibrary.dll? I dont know how to link the application with the library considering both files .
    Well ,I have succesfully compiled the library the way you told me ,but got the same results : some items are not getting events .I loaded it with mapandload and loadlibrary ,so I think I will now give QLibrary a chance .
    Thank you very much.
    P.D.: And by the way , as it was normal to think , no matter if the dll "includes" .h files of the items (that means the .h has some funtion implementation).
    Last edited by Benne Gesserit; 7th August 2008 at 10:46. Reason: add P.D.
    If God has friends ,then I cant be God.

  7. #6
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Item in a DLL not getting events

    Other question. The object that doesn't get the event? What kind of object is it, and what kind of event is it that the object doesn't get?

    cheers,
    leif

  8. #7
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    The object that doesnt get the event ....well ,I wont simplify it just in case it s important .
    I have an abstract class (lets call it A) that inherits QGraphicsItem , then 2 clases (non abstract) that virtually inherit A (B,C) ,and then a class that inherits B and C (D) . (None is Q_OBJECT) .
    In my application , I use only pointers to tha abstract base class.
    The other items : one inherits QGraphicsTextItem (inherits QObject) ,and the other is a QGraphicsLineItem (doesnt inherit QObject) .

    I realy dont know why it doesnt work
    Thanks again.
    If God has friends ,then I cant be God.

  9. #8
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Item in a DLL not getting events

    And the event it doesn't recieve?

    Have you tried adding the Q_OBJECT macro to the classes to see what happens? It might be that since you don't have the macro, some events sent through signals/slots are not reciveved? I'm unsure about this...

  10. #9
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    Quote Originally Posted by luf View Post
    And the event it doesn't recieve?

    Have you tried adding the Q_OBJECT macro to the classes to see what happens? It might be that since you don't have the macro, some events sent through signals/slots are not reciveved? I'm unsure about this...
    Sorry ,I forgot that .They dont seem to recieve any event. If I click them,nothing happens , I had rubber band drag (or however it s called) and it doesnt work now .It s just like if the item wasnt there .
    And about Q_OBJECT....maybe I try it if I have some time ,but I dont like that solution ,why should a graphics view item that was working perfectly have to inherit QObject and add that macro in order to be able to work in a DLL ? Maybe there is a reason ,I dont know ,but I wouldnt like that handicap.
    Thanks , hopefuly I solve this......soon ,but I m a little bit faithless.
    If God has friends ,then I cant be God.

  11. #10
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    -_- ,I think I have found the problem or at least a fix.I had a constructor without parameters like this:
    Node()
    {
    }

    Tha should implicitly call QGrapchicsItem() , what actualy is QGraphicsItem ( QGraphicsItem * parent = 0 ) .I had another constructor :
    Node (...... ,QGraphicsScene * scene =0 ,QGraphicsItem * parent=0)
    that called QGraphicsItem(parent) ,but parent was always null .So ,if I use the default contructor ,the events dont work ,if I use the other ,they do .It doesnt make any sense to me .I ll try to explicitly call the default base class constructors to see if it works that way ,otherwise,I ll just use the constructors with parameters .
    Anyone knows why this happens?
    If God has friends ,then I cant be God.

  12. #11
    Join Date
    Apr 2006
    Location
    Denmark / Norway
    Posts
    67
    Thanks
    3
    Thanked 12 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Item in a DLL not getting events

    Hi

    What happens is that when the item doesn't have a parent, it won't recieve events unless it has focus. (e.g. clicked on, selected or programatticly has focus)

    When programming with Qt it's a good practice to have a parent for all objects, so that objects are deleted if the parent is deleted...

    Leif

  13. #12
    Join Date
    Jul 2008
    Posts
    27
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Item in a DLL not getting events

    Uhm.....So the thing was the items werer not getting focus....But other parentless items worked fine .
    Thanks for the tips.
    If God has friends ,then I cant be God.

Similar Threads

  1. Replies: 2
    Last Post: 1st August 2008, 16:58
  2. QGraphicsView Mouse Events
    By tomf in forum Qt Programming
    Replies: 5
    Last Post: 29th July 2008, 15:03
  3. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  4. Replies: 1
    Last Post: 21st August 2007, 16:25
  5. Replies: 1
    Last Post: 19th April 2007, 22:23

Tags for this Thread

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.