Results 1 to 16 of 16

Thread: Problem of including Widgets into DLL

  1. #1
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem of including Widgets into DLL

    Hi. I am writing a software using VS2008 and QT4.3.3 with VS integration and it's structure is following:

    First project is an application
    Second project is a Qt Library project which produces a DLL. Now the problem I am having is that when I include DLL header file into my application compiled can't find auto generated ui_***.h file.

    How should I go about including several widgets that I plan to use into a single DLL and be able to use QT Designer. If I were to write a widget from scratch without using .ui files - everything works. But the problem is that DLL compiles just fine, but the actual application, when includes DLL header (which also includes auto generated ui_***.h files) does not find those auto generated headers.

    Maybe there is some other way to write a single DLL projects which includes several widgets?

  2. #2
    Join Date
    Nov 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    HI.

    I have done something the same as you do...but I made an _impl class which inluded the ui file.

    the actual dll interface is defined as an interface class I have totally defined (just calls to the _impl class)...in this way I can control which elements in the widget(s) can be accessed externally.

    rgds
    Jan

  3. #3
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    I am not sure what you mean by _impl class. Could you explain a little more. I am not a big C++ pro =(

  4. #4
    Join Date
    Nov 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    I always make the dll's with one interface class.

    e.g.

    dll_mywidget.hpp
    dll_mywidget.cpp

    the class dll_mywidget are just call to another class dll_mywidget_impl

    The dll_mywidget_impl class is the one that include the ui file...basically you make dll_mywidget_impl as you would make a normal widget....

    and in dll_mywidget.cpp you decide which parts to make public in the dll...dont forget the class you want to make public in the dll, needs special DLL declaration (DLL_EXPORT) in order to work.

    jan

  5. #5
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    LOL I still don't get it. I think the way I understand it is that I have a dummy class which gets included into the main application and it exports other class with UI. But how do I include the base class into main application without ui header?

    So far I have this:

    Here the dummy class which includes the interface class (test.h) :
    Qt Code:
    1. #ifndef FBL_GUI_H
    2. #define FBL_GUI_H
    3.  
    4. #include "fbl_gui_global.h"
    5. #include "test.h"
    6. class FBL_GUI_EXPORT fbl_gui
    7. {
    8. public:
    9. fbl_gui();
    10. ~fbl_gui();
    11.  
    12. private:
    13.  
    14. };
    15.  
    16. #endif // FBL_GUI_H
    To copy to clipboard, switch view to plain text mode 

    Then I have an interface class in the same dll project:

    Qt Code:
    1. #ifndef TEST_H
    2. #define TEST_H
    3.  
    4. #include <QWidget>
    5. #include "ui_test.h"
    6.  
    7. class test : public QWidget
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. test(QWidget *parent = 0);
    13. ~test();
    14.  
    15. private:
    16. Ui::testClass ui;
    17. };
    18.  
    19. #endif // TEST_H
    To copy to clipboard, switch view to plain text mode 

    Now the problem is that if I include the base dll class it also includes the widget class. How can I skip this part but be able to access the widget and be able to create it in the main app?

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of including Widgets into DLL

    Quote Originally Posted by dsedov View Post
    Hi. I am writing a software using VS2008 and QT4.3.3 with VS integration and it's structure is following:

    First project is an application
    Second project is a Qt Library project which produces a DLL. Now the problem I am having is that when I include DLL header file into my application compiled can't find auto generated ui_***.h file.

    How should I go about including several widgets that I plan to use into a single DLL and be able to use QT Designer. If I were to write a widget from scratch without using .ui files - everything works. But the problem is that DLL compiles just fine, but the actual application, when includes DLL header (which also includes auto generated ui_***.h files) does not find those auto generated headers.

    Maybe there is some other way to write a single DLL projects which includes several widgets?
    at the first you need to create h-file with the following content (e.g. myglobal.h)
    Qt Code:
    1. #ifndef MYGLOBAL_H
    2. #define MYGLOBAL_H
    3.  
    4. #include <QtGlobal>
    5.  
    6. #ifdef MY_LIB_STATICLIB
    7. # undef MY_LIB_SHAREDLIB
    8. # define MY_LIB_EXPORT
    9. #else
    10. # ifdef MY_LIB_MAKEDLL
    11. # define MY_LIB_EXPORT Q_DECL_EXPORT
    12. # else
    13. # define MY_LIB_EXPORT Q_DECL_IMPORT
    14. # endif
    15. #endif
    16.  
    17. #endif//MYGLOBAL_H
    To copy to clipboard, switch view to plain text mode 

    then in pro-file of your lib you should add next thing
    Qt Code:
    1. ...
    2. contains(CONFIG, staticlib) {
    3. DEFINES += MY_LIB_STATICLIB
    4. } else {
    5. DEFINES += MY_LIB_SHAREDLIB
    6. }
    7.  
    8. win32 {
    9. DEFINES += MY_LIB_MAKEDLL
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 

    and then you must add MY_LIB_EXPORT before class name which you want to export
    Qt Code:
    1. #include "myglobal.h"
    2. class MY_LIB_EXPORT MyObj
    3. {
    4. ...
    5. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by spirit; 14th November 2008 at 14:20. Reason: updated contents
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    Yeah I know that. Thanks. The problem is that when I include DLL header in a main app it won't be able to find autogenerated files from QT Designer. Dll compiles just fine, but when main app compiles it sais : fatal error C1083: Cannot open include file: 'ui_test.h': No such file or directory

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of including Widgets into DLL

    could you show us pro-file of app not lib?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    Err.. there is no .pro file. I am using VS2008 with QT integration.

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of including Widgets into DLL

    ok. what do you try to do now: build a lib or build app with the lib which you are created?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    Build an app.
    The lib compiles just fine. I just have problem of my app using lib headers as it cannot find autogenerated ui_***.h files.

    My app has this in a header:
    Qt Code:
    1. #include "../fbl_gui/fbl_gui.h"
    To copy to clipboard, switch view to plain text mode 

    This includes library header, so I will be able to use it's objects.

  12. #12
    Join Date
    Nov 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    So any ideas???

  13. #13
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem of including Widgets into DLL

    look at this example
    Attached Files Attached Files
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #14
    Join Date
    Feb 2021
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    Did you ever figure this out? I'm just getting started with Qt and I'm having the exact same issue. The test file in the last post has the same error as well, so it doesn't include the fix.


    Added after 9 minutes:


    Quote Originally Posted by spirit View Post
    look at this example
    FYI: This project works in Creator, but does not work in VS with the Qt plugin. In VS with Qt plugin you get an error...

    fatal error C1083: Cannot open include file: 'ui_mywidget.h': No such file or directory


    Added after 28 minutes:


    After trying to create a similar class in Creator I noticed that it did things a bit differently. Instead of including the "ui_Dialog.h" file directly in the header it uses a pointer to a dummy namespace/class in the header, then imports the actual "ui_Dialog.h" in the cpp file.

    So instead of...

    Qt Code:
    1. #include <QDialog>
    2. #include "ui_DialogTest.h"
    3.  
    4. class DialogTest : public QDialog
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. DialogTest(QWidget *parent = Q_NULLPTR);
    10. ~DialogTest();
    11.  
    12. private:
    13. Ui::DialogTest ui;
    14. };
    To copy to clipboard, switch view to plain text mode 

    it uses...

    Qt Code:
    1. #include <QDialog>
    2.  
    3. namespace Ui {
    4. class DialogTest;
    5. }
    6.  
    7. class DialogTest : public QDialog
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. DialogTest(QWidget *parent = Q_NULLPTR);
    13. ~DialogTest();
    14.  
    15. private:
    16. Ui::DialogTest *ui;
    17. };
    To copy to clipboard, switch view to plain text mode 

    then in the cpp it uses...

    Qt Code:
    1. #include "dialogtest.h"
    2. #include "ui_dialogtest.h"
    3.  
    4. DialogTest::DialogTest(QWidget *parent) :
    5. QDialog(parent),
    6. ui(new Ui::DialogTest)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. DialogTest::~DialogTest()
    12. {
    13. delete ui;
    14. }
    To copy to clipboard, switch view to plain text mode 

    I converted the auto generated class in Visual Studio to use this format and now I can use the class outside the DLL and it still works. I'm going to report this as a bug to the developers of the VS plugin and see if they'll fix it in a future version.
    Last edited by Dan203; 14th July 2021 at 00:37.

  15. #15
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    I'm going to report this as a bug to the developers of the VS plugin and see if they'll fix it in a future version.
    This isn't a bug. It is simply a difference in the method of implementation.

    If your QWidget-derived class uses a UI file that declares a class containing the UIC-generated UI class (in your case DialogTest, which is scoped to the Ui namespace), there are three ways to add that to the QWidget class that uses it.

    First is the way your QDialog-derived DialogTest class uses it: as a pointer member of DialogTest. In this case, all you need is a forward declaration of the pointer class (Ui:: DialogTest). This is what QtCreator does.

    The second way is to include Ui:: DialogClass as a member variable (not a pointer) in the DialogTest Class.

    The third way is to use multiple inheritance and derive DialogTest from both QDialog and Ui:: DialogTest.

    To implement these last two cases, the compiler must have access to the full definition of the Ui:: DialogTest class. This requires a #include of the ui_dialogtest.h file. A forward reference is not sufficient.

    The Visual Studio plugin simply covers all cases by by #including the ui header file no matter how you use the Ui class. It isn't a bug, but it assumes that all of the code that uses the class has access to the Ui header file. This is the most common use case. It only breaks down in the rare case where the Ui class is part of a DLL -and- the QWidget-based class uses the pointer to Ui member idiom. In that case, all you have to do is move the #include statement from the .h file to the .cpp file as you have done, and replace it with a forward declaration in the header file. No big deal.

    By the way if you have other resources in a DLL that you want to use in an EXE (icons, bitmaps, etc.), then you add this declaration (in main.cpp) :

    Qt Code:
    1. int main( int argc, char * argv[] )
    2. {
    3. QApplication a( argc, argv );
    4. Q_INIT_RESOURCE( MyDLL );
    5.  
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    assuming your DLL is named MyDLL.dll.
    Last edited by d_stranz; 14th July 2021 at 17:16.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  16. #16
    Join Date
    Feb 2021
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Problem of including Widgets into DLL

    Quote Originally Posted by d_stranz View Post
    This isn't a bug. It is simply a difference in the method of implementation.

    If your QWidget-derived class uses a UI file that declares a class containing the UIC-generated UI class (in your case DialogTest, which is scoped to the Ui namespace), there are three ways to add that to the QWidget class that uses it.

    First is the way your QDialog-derived DialogTest class uses it: as a pointer member of DialogTest. In this case, all you need is a forward declaration of the pointer class (Ui:: DialogTest). This is what QtCreator does.

    The second way is to include Ui:: DialogClass as a member variable (not a pointer) in the DialogTest Class.

    The third way is to use multiple inheritance and derive DialogTest from both QDialog and Ui:: DialogTest.

    To implement these last two cases, the compiler must have access to the full definition of the Ui:: DialogTest class. This requires a #include of the ui_dialogtest.h file. A forward reference is not sufficient.

    The Visual Studio plugin simply covers all cases by by #including the ui header file no matter how you use the Ui class. It isn't a bug, but it assumes that all of the code that uses the class has access to the Ui header file. This is the most common use case. It only breaks down in the rare case where the Ui class is part of a DLL -and- the QWidget-based class uses the pointer to Ui member idiom. In that case, all you have to do is move the #include statement from the .h file to the .cpp file as you have done, and replace it with a forward declaration in the header file. No big deal.

    By the way if you have other resources in a DLL that you want to use in an EXE (icons, bitmaps, etc.), then you add this declaration (in main.cpp) :

    Qt Code:
    1. int main( int argc, char * argv[] )
    2. {
    3. QApplication a( argc, argv );
    4. Q_INIT_RESOURCE( MyDLL );
    5.  
    6. // ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    assuming your DLL is named MyDLL.dll.
    I started playing with it more after posting this and I noticed that there is an option in the Qt dialog wizard to do it different ways. For whatever reason I had never paid attention to that option before.

    Although it's still strange that Creator uses the pointer syntax by default and the VS plugin uses the header/member variable by default. For consistency they should probably make the VS plugin use the pointer syntax as the default.

Similar Threads

  1. Replies: 0
    Last Post: 16th July 2008, 14:15
  2. Replies: 3
    Last Post: 10th May 2008, 18:36
  3. Algorithms problem of brackets including.
    By luffy27 in forum General Programming
    Replies: 2
    Last Post: 12th April 2007, 02:10
  4. Docking widgets setFixedSize and setBaseSize Problem
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2006, 19:20
  5. Replies: 11
    Last Post: 7th July 2006, 14:09

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.