Results 1 to 19 of 19

Thread: Question about classes in Qt

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Question about classes in Qt

    I am admittedly a newbie to both Qt and C++, therefore this question.
    (Almost all of my posts are asking questions and many thanks to those who have taught me)

    I am stumped in trying to use a 3rd party library written in C++.
    If I build a dll from their source, using Mingw, it won't load into my Qt application.
    Although a test Windows dll will.

    So I tried using the .a library I built from their source as a static library with my application (my application compiles ok with this).

    Then comes the question. It seems to me, looking at the Qt examples, that Qt expects the header file to define a class for the source file (and that is the way I have written my application). But this 3rd party application is written in a manor where the header doesn't always define the classes (that I need) in the source file (but they are defined there). So I don't know how to create an object for the class I need to call functions from the 3rd party library.
    How can one handle this in Qt?

    Can someone please explain this to me or point me to a reference so I can figure it out. My Qt and C++ books don't seem to help much here.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Question about classes in Qt

    Quote Originally Posted by waynew View Post
    I am stumped in trying to use a 3rd party library written in C++. If I build a dll from their source, using Mingw, it won't load into my Qt application. Although a test Windows dll will.
    This sounds like a purely run-time linking issue. The DLL has to be in the same directory as the compiled executable or in the system path/Windows directories if its not a Qt Plugin. If it is a Qt plugin then it may need to be in a subdirectory relative to the executable (by default anyway).

    So I tried using the .a library I built from their source as a static library with my application (my application compiles ok with this).
    Good, so this is not a mismatched compiler issue.

    Then comes the question. It seems to me, looking at the Qt examples, that Qt expects the header file to define a class for the source file (and that is the way I have written my application). But this 3rd party application is written in a manor where the header doesn't always define the classes (that I need) in the source file (but they are defined there). So I don't know how to create an object for the class I need to call functions from the 3rd party library.
    How can one handle this in Qt?
    The header file is needed by other code so that is knows what data structures exist in the library in order to compile code that uses them. The library source file can also define classes and other data structure for use internally. These are not generally accessible from outside the library. You said that your program compiles, so the library headers you have included in your sources are adequate.

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

    waynew (4th November 2010)

  4. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Thanks Chris - that clears up some things.
    I can see that the functions in the library that I need to use are declared DLLEXPORT
    So it seems the only question now is how to access those functions from my Qt application - that is, what would the syntax look like?

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

    Default Re: Question about classes in Qt

    Quote Originally Posted by waynew View Post
    So it seems the only question now is how to access those functions from my Qt application - that is, what would the syntax look like?
    Just call them as any other functions/methods.
    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.


  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    I'm a little worried now. Trying to access one of the functions and I get an error that it hasn't been defined.
    So to test if the library is really linking, I checked the file size of the release exe, then commented out the library lines in the .pro file, ran qmake, then rebuilt. The exe file is still the same size. Seems to me if the static library was being included, the size of the exe should increase.
    Is there any way to check using the application code if the library is actually included other than trying to call a function in it?

    And if qmake runs with no error and the build also, with the library lines in the .pro, why would the library not be included?
    Here are the library lines:
    Qt Code:
    1. INCLUDEPATH += C:\cpp\qt_projects\hrlogger_development\debug\tqsllib
    2. LIBS += -L"C:\cpp\qt_projects\hrlogger_development\debug\tqsllib"
    3. LIBS += -ltqsllib
    To copy to clipboard, switch view to plain text mode 
    I am using the static library qserialdevice in the same manner with no problems at all.
    Only difference with qserialdevice is that I create an object for the class with the functions in it that I need.

  7. #6
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Question about classes in Qt

    Definition occurs in the header file. You're using the same path for both the include statement - which pulls in the header files - and for the library location. This would be an unusual configuration; headers are normally located in their own directory.

    Missing header files generate undefined symbol errors; missing libraries generate undefined references (to the functions that can't be found).

    Look around in the directory your external library is installed in, and see if you can find a .h file; put that directory in your include path, and include that file in your own source code.

  8. #7
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Thanks Six, yeah, I did all of that. I built the library under my mingw/msys/1.0/home/user directory then made the tqsllib directory under my application for simplicity and copied the .h files I needed and the library file there.

    Seems to me the library is not loading/linking when I do the build. I get no errors, but the exe is not getting any bigger than building without the library and I think it should. Then when I add code to call a function in the library I get the undefined reference error.

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

    Default Re: Question about classes in Qt

    What is the exact error you are getting?
    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.


  10. #9
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Here's the error message Wysota:

    C:/CPP/QT_Projects/HRLogger_Development/lotwupload.cpp:12: error: 'tqsl_getStationLocation' was not declared in this scope

    That function is defined in the library as DLLEXPORT int tqsl_getStationLocation(tQSL_Location *locp, const char *name)

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

    Default Re: Question about classes in Qt

    This is not a linker error but a compiler error. You are missing a prototype of the function. Are you including the proper header file?
    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.


  12. #11
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Ooops, I forgot to uncomment the header includes when I added the library back in.

    Now the compiler is complaining because I am not calling the function correctly, which is
    DLLEXPORT int
    tqsl_getStationLocation(tQSL_Location *locp, const char *name)

    So, this becomes a C++ problem as my app definitely seems to see the library.
    Thank you for your assistance gentlemen and I'm back to the C++ books to see how to make this work.
    I truly appreciate your help.

  13. #12
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Question about classes in Qt

    What error?

  14. #13
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Well tbscope, I'm not getting an error on the function call now, but on the things that precede it.

    Yeah, I know, this is basic C++ stuff, but I'm a 66 year old dog trying to learn new tricks and my native language is pl/sql.

    Here is the code from the library:
    Qt Code:
    1. //from the header
    2. typedef void * tQSL_Location;
    3. //from the source
    4. class TQSL_LOCATION {
    5. public:
    6. TQSL_LOCATION() : sentinel(0x5445), page(0), cansave(false), sign_clean(false) {}
    7. ~TQSL_LOCATION() { sentinel = 0; }
    8. int sentinel;
    9. int page;
    10. bool cansave;
    11. std::string name;
    12. TQSL_LOCATION_PAGELIST pagelist;
    13. std::vector<TQSL_NAME> names;
    14. std::string signdata;
    15. bool sign_clean;
    16. std::string tSTATION;
    17. std::string tCONTACT;
    18. std::string sigspec;
    19. };
    20.  
    21. DLLEXPORT int
    22. tqsl_getStationLocation(tQSL_Location *locp, const char *name)
    To copy to clipboard, switch view to plain text mode 
    So stumbling around in my usual way, in my application, I put in my LotwUpload class:
    Qt Code:
    1. class TQSL_LOCATION;
    2.  
    3. LotwUpload::LotwUpload(QWidget *parent) :
    4. QDialog(parent),
    5. ui(new Ui::LotwUpload)
    6. {
    7. ui->setupUi(this);
    8.  
    9. TQSL_LOCATION tqslLocation = new TQSL_LOCATION();
    10. tqslLocation = "K4ELO - K4ELO Farm";
    11. int loc = tqsl_getStationLocation(tqslLocation, "my_name");
    12. }
    To copy to clipboard, switch view to plain text mode 
    And the compiler errors are:
    C:/CPP/QT_Projects/HRLogger_Development/lotwupload.cpp:14: error: variable 'TQSL_LOCATION tqslLocation' has initializer but incomplete type

    C:/CPP/QT_Projects/HRLogger_Development/lotwupload.cpp:14: error: invalid use of incomplete type 'struct TQSL_LOCATION'

    C:/CPP/QT_Projects/HRLogger_Development/lotwupload.cpp:6: error: forward declaration of 'struct TQSL_LOCATION'

    So, unless someone is kind enough to explain to me what I am doing wrong, I'll just keep researching and stumbling until I get it to work

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

    Default Re: Question about classes in Qt

    You are missing an include of the header file containing the definition of the TQSL_LOCATION class. Furthermore line #8 of the last snippet of code should probably read:
    Qt Code:
    1. TQSL_LOCATION *tqslLocation = new TQSL_LOCATION;
    To copy to clipboard, switch view to plain text mode 
    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.


  16. #15
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Well Wysota, that's part of the problem, the class is only defined in the source, not the header. Why do people write code like this?
    There must be a reason I don't understand, but that's just my lack of knowledge.
    But your code correction is right, of course. I'm getting sloppy in my old age. Maybe too old to learn C++. But I'm still trying
    So, I changed the code as you rightly suggested, and I am still getting the same errors.

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

    Default Re: Question about classes in Qt

    Quote Originally Posted by waynew View Post
    Well Wysota, that's part of the problem, the class is only defined in the source, not the header. Why do people write code like this?
    There must be a reason I don't understand, but that's just my lack of knowledge.
    Maybe the class was never meant to be used by external developers? You can always move the definition to the header file if that doesn't break someone else's code.
    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.


  18. #17
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    I think I'm almost there (hopefully).
    Again, here is the library function I need to call:
    Qt Code:
    1. DLLEXPORT int
    2. tqsl_getStationLocation(tQSL_Location *locp, const char *name)
    3. // and in the header for the library:
    4. typedef void * tQSL_Location;
    To copy to clipboard, switch view to plain text mode 
    So now I have in my code:
    Qt Code:
    1. QString location = "my_location";
    2. QString name = "my_name";
    3. int loc = tqsl_getStationLocation(&location, &name);
    4. qDebug() << "loc returns " << loc;
    To copy to clipboard, switch view to plain text mode 
    And the compiler error is:
    C:/CPP/QT_Projects/HRLogger_Development/lotwupload.cpp:18: error: cannot convert 'QString*' to 'void**' for argument '1' to 'int tqsl_getStationLocation(void**, const char*)'
    So what am I doing wrong?

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

    Default Re: Question about classes in Qt

    You are passing a string where the function requires a pointer to the TQSL_Location structure (probably). With an ugly C cast, if I might add.
    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.


  20. #19
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Question about classes in Qt

    Thanks Wysota - I can see that now.
    Like someone said, inexperience can be cured, but stupid is forever. I'm hoping to be in the first category.

    Got sidetracked by other issues. Back to the problem I haven't solved yet.
    Now the code looks like this:
    Qt Code:
    1. tQSL_Location location;
    2. QString locName = "K4ELO Farm";
    3. char *myLoc = locName.toUtf8().data();
    4. int loc = tqsl_getStationLocation(&location, myLoc);
    To copy to clipboard, switch view to plain text mode 

    And the compiler error is:
    C:\CPP\QT_Projects\HRLogger_Development/lotwupload.cpp:18: undefined reference to `_imp__tqsl_getStationLocation@8'

    So, is it really not seeing the library then?

Similar Threads

  1. QT WMI Classes
    By justatiq in forum Qt Programming
    Replies: 31
    Last Post: 10th April 2011, 15:44
  2. Question about functions in classes
    By cwnelatury in forum Newbie
    Replies: 1
    Last Post: 13th May 2009, 06:05
  3. fax classes in qt
    By dyams in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2007, 09:14
  4. Quick question regarding abstract classes and DLLs
    By durbrak in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2007, 21:32

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
  •  
Qt is a trademark of The Qt Company.