How to make use of the private Qt C++ API? - c++

I'm trying to use the QBalloonTip class to generate a stilized tooltip for a button with QT 5.8.0.
What I've tried until now is modify the header file qsystemtrayicon_p.h the method QBalloonTip::showBalloon(...) by giving default arguments. However even when I included the header qsystemtrayicon_p.h in my code, I'm having problems when linking the compiled sourced code. Next error appears:
LNK2019: unresolved external symbol: "public static void QBalloonTip::showBalloon(...)"
Hope anyone has faced this before. Thank you in advance.

To use Qt's private API, you have to explicitly include it in your .pro file:
QT += widgets-private
However, it is not recommended to do that, as the private APIs are not documented and tie you to the specific Qt version you built with. If your Code is GPL compliant, you should propably rather copy the sources of the balloontip class into your application.
Furthermore, this will not solve your problem, as you can't simply modify a header to an already compiled library. This is simply not possible. Either try to inherit from that class, or copy the sources into your project and modify them there.

Related

how can I create an extension for Qt application

There is this guide: http://qt-project.org/doc/qt-4.8/plugins-howto.html
It is totally useless, because it is missing too many crucial information. However after few hours of some research I figured out how to get somewhere:
Source code of interface .h: https://github.com/huggle/huggle3-qt-lx/blob/master/huggle/iextension.h
And .h of plugin: https://github.com/huggle/extensions/blob/master/c%2B%2B/HelloWorld/HelloWorld/helloworld.h
I managed to create an interface for plugin and applied the macro to it, then I created a plugin but as in example the plugin should be inherited from interface, I clearly need to somehow include the .h file from my project. I did that, and then I used QPluginLoader in order to load the .so file.
However I am getting this:
Failed to load (reason: Cannot load library /home/petanb/Documents/huggle3-qt-lx/huggle/extensions/libHelloWorld.so: (/home/petanb/Documents/huggle3-qt-lx/huggle/extensions/libHelloWorld.so: undefined symbol: _ZTIN6Huggle10iExtensionE))
that makes sense, the library (where I #include the .h file of my project) is clearly missing the reference to binary of my application in order to resolve it but how can I give it to that? Should I copy the application's binary, rename it to something.so and put it to /lib or something? Or what should I do?

Unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent

I inherited a class from QObject :
class Parent: public QObject
{
Q_OBJECT
QObject* cl;
public:
Parent(QObject *parent=0):QObject(parent) {
cl = NULL;
}
QObject* getCl() const {
return cl;
}
void setCl(QObject *obj) {
cl = obj;
}
};
But when I write :
Parent ev;
I get the following error:
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Parent::metaObject(void)const " (?metaObject#Parent##UBEPBUQMetaObject##XZ)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __thiscall Parent::qt_metacast(char const *)" (?qt_metacast#Parent##UAEPAXPBD#Z)
main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __thiscall Parent::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#Parent##UAEHW4Call#QMetaObject##HPAPAX#Z)
You should delete the debug folder of your application and run it again to correct this problem.
If you're using Visual Studio, delete the line Q_OBJECT from the header file, save the file, put Q_OBJECT back into the header file, save the file again. This should generate the moc_* file and should build and link correctly.
I noticed some answers are based on Visual Studio.
This answer is based on Qt Creator.
Unlike the name suggest, Rebuild Project will not wipe out everything and build from scratch. If you recently added QObject (and/or Q_OBJECT) to your class, you'll have to run qmake again, e.g.
Clean Project
Run qmake
Build Project
This is because, by default, qmake only runs when you do significant changes to your solution like adding new source files or modify the .pro file. If you make edits to an existing file, it doesn't know it needs to run qmake.
As a fall back, to brute force Qt to build everything from scratch, delete the Debug or Release folder.
So the issue was I needed the Qt MOC compiler to compile my .h file. This is required for any classes that extend QObject or one of its children. The fix involed (for me) right-clicking on the header file, choosing Properties, and setting the Item Type to "Qt MOC Input", then hitting "Compile" on the header, and then adding the resulting moc_myfilename.cpp file to my project.
I added cpp/ui files to my project manually, but forgot to add the header file explicitly as header file. Now when compiling I got a similar error message as above and the moc_*.cpp file(s) were not generated in the debug (or release) directory of the build. That was not such an obvious mistake, qmake did not complain and other than the linker message I got no errors.
So if anyone encounters the same problem again (or makes the same copy & pase mistake): make sure the header files have also been added to your project file
If your moc files are generated in the visual studio project try to include them into project if they are not included into project then rebuild.
I had the same problem in Visual Studio, and solved it by taking the following steps:
Right-click header file in solution Explorer
Properties
Change "Item Type" to "Custom Build Tool"
Then in the Custom Build Tool configuration:
Go to General
set "Command Line" to:
"$(QTDIR)\bin\moc.exe" "%(FullPath)" -o ".\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp" "-fStdAfx.h" "-f../../../src/FileName.h" -DUNICODE -DWIN32 -DWIN64 -DQT_DLL -DQT_NO_DEBUG -DNDEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DWIN32_LEAN_AND_MEAN -DDIS_VERSION=7 -D_MATH_DEFINES_DEFINED "-I.\SFML_STATIC" "-I.\GeneratedFiles" "-I." "-I$(QTDIR)\include" "-I.\GeneratedFiles\$(ConfigurationName)." "-I$(QTDIR)\include\QtCore" "-I$(QTDIR)\include\QtGui" "-I$(QTDIR)\include\QtNetwork"
set "Outputs" to:
.\GeneratedFiles\$(ConfigurationName)\moc_%(Filename).cpp
set "Additional Dependencies" to:
$(QTDIR)\bin\moc.exe;%(FullPath)
Your exact values may be different. They are usually applied via a Qt plugin.
I had this problem with Visual Studio 2012 when I had a Q_OBJECT class definition in my cpp file. Moving the class definition to the header file resolved the issue.
It looks like it should be possible to support Q_OBJECT class in cpp file by adding the cpp file to moc but I did not try this.
I use CMake to manage Qt projects and the new Q_OBJECT needs to be added under the QT4_WRAP_CPP call. This will generate the moc_*.cxx for inclusion in the project and clean up the unresolved externals.
My problem was that one of my files that used a Qt macro didn't get moc'ed. I found out, that the Qt Plugin for Visual Studio doesn't recognize the Q_NAMESPACE macro and therefore doesn't add the file to the moc'ing list.
So I used the solution from this answer to add the file to the mic'ing list:
You should find a .h file which has successfully generated
"moc_*", and copy all the contents in "Custom Build Tool -> General"
to the new .h file setting page.
Be careful with the different options for Debug and Release-Mode.
After that, build your project.
Build it once each in Debug and Release-Mode
Finally, add the generated "moc_*" file to your project.
Now, "moc_filename.cpp" should be in Generated Files\Debug and Generated Files\Release.
Right click on each of them and change thair properties:
The file in Debug: Change configuration to Release and then change General->Excluded from build to yes.
The file in Release: Change configuration to Debug and then change General->Excluded from build to yes.
Faced this issue in case of chained CMake targets. Turned out I had to enable CMAKE_AUTOMOC even in targets that didn't use Qt directly (transitively). Also turned out that CMAKE_AUTOMOC can't be used w/o find_package(QtX) in same CMakeLists.txt or CMakeLists.txt of parent.
In my case (using QtAdd-in with VS2012 and Qt v4.8.4) none of the above suggestions worked.
For some reason VS could not generate proper moc files (build output: No relevant classes found. No output generated.) and when I compiled relevant headers by hand (setting qt moc as a compiler and clicking 'Compile') it produced empty moc file.
What did work was to compile all necessary mocs from command line (moc -o moc_SomeClass.cpp SomeClass.h) and then replace the wrong ones in GeneratedFiles folder.
This is only workaround (and not a convenient one for a big project) to have your project build succesfully, but does not really explain strange VS/QtAdd-in behaviour.
Using QtAdd-in with VS2010 i realized the moc_*.cpp files were updated in the GeneratedFiles/Debug folder although i was in release mode. Copying the files into the Release folder worked for me.
I know that this is a very old question, but it seems to be still interesting (I've been here at least 4 or 5 times in the last months) and seems like I found another reason for which is possible to get this error.
In my case in the header file I wrongly typed:
#include "MyClass.h""
Only after inspecting the whole output I found out that at that line the compiler was emitting a warning.
Now that I removed the additional quotation mark my QObject compiles perfectly!
I've encountered this problem with the use of a "private class" in Qt when employing the "PIMPL" (private implementation) programming pattern. Qt uses this model all through out their source code. I have come to really like it myself.
This technique involves the use of a "private" forward-declared class in a public header file, which will be be used by the "public" class (i.e. it's "parent"). The parent then has a pointer to an instance of the private class as a data member.
The "private" class is defined entirely within the cpp file for the public one. There is NO header file for the private class.
All of the "dirty work" is done with that private class. This hides all of the implementation of your public class including every other private member typically (both data and functions).
I highly recommend learning about the PIMPL pattern - especially if you are going ever read the internal Qt source.
Without explaining that coding style further, here's the point as it relates to this question... To get the Q_OBJECT macro to work inside the cpp for the "private" class to be QObject which can use signals/slot etc., you needed to explicitly include the .moc to the public class inside the cpp:
#include "MyPublicClass.moc"
You can ignore any IDE warnings about this line.
I'm not sure if it matters exactly off hand, but that inclusion I always see done AFTER the private class definition, rather than at the top of the cpp (like includes are typically placed). So, the cpp layout goes like this:
"Normal" includes are defined.
The private class is defined.
The moc for the public class is #included.
The public class implementation is defined.
Visual Studio 2017.
I've added file to already set up Qt project and got this error. How I fixed it:
Right click on the header in Solution Explorer
Properties... -> Configuration Properties -> General -> Item Type
Change from C/C++ Header to Qt Meta-Object Compiler (moc)
voila :)
This happened to me recently when switching from MingW to MSVC. I had a prototyped class/struct listed as a class, and MingW didn't mind.
MSVC definitely sees a difference between class and struct when prototyping is concerned.
Hope that helps someone else one day.
In my case, none of the above worked but it was totally my mistake.
I had overrided virtual functions in .h file (declared them) but had never defined them in .cpp :)
Either answer works for me in the VS 2013 environment. I eventually solve the problem by removing the .h/.cpp from project, and adding it back.
I am working in VS2015 with an integrated Perforce p4v client.
In my case Perforce tried to add moc file to a depo, when I reverted this operation, Perforce removed this moc file from project and deleted it.
The file was recreated after the next compilation, but it wasn't included in project, I have to add it manually to Generated files, when I finally understood what was the problem.
I have the same problem, my solution was the encoding( my file with "UTF16LE BOM" can't generate with moc.exe ) , y create another file with ASCII enconding and it work.
HxD HexEditor can help you to see the codification.
For me, this is the cause: some header or source file not included in QT's project file
In my case I had the .h and the .cpp file for the problematic QObject ancestor in subfolders of the project. When I moved them next to the CMakeLists.txt (project root folder) it linked successfully. I'm probably missing some CMake command to include mocs fot files in subdirectories.
when I removed Q_OBJECT it works fine.
I'm using Clion + CMake + MSVC/14.31.31103.
Is it because Q_OBJECT no longer needed after these?
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
For Visual Studio 2022 (with Qt VS Tools extention)
In Solution Explorer right click on the affected header file and select Properties.
Under "Configurations Properties -> General -> Item Type" select the option "Qt Meta-Object Compiler (moc)".
Then rebuild the project.
In Qt Creator
It turned out I had this error because I added Q_OBJECT afterwards in the .h file.
I managed to fix it after removing and adding the file back into the project.
Happy coding...
I solved my problem by adding this to my header file :
#ifndef MYCLASSNAME_H
#define MYCLASSNAME_H
... // all the header file content.
#endif

Non-class method in Qt ( with qtcreator ) unresolved external symbol

I have an application that was built without problems in Linux, now I'm trying to compile it against Windows, I created a .pro file specially for compiling it in windows.
I use a pseudo-class ( just a namespace with a buch of methods, but without a class statement so I can use it without creating an object ) that's working just fine in Linux, but
when I try to compile against windows I get an 'unresolved external symbol' throughout all the code where this pseudo-class's being used.
The pseudo-class goes like this:
namespace foo {
bool method_bar();
}
Then I use it like this:
foo:method_bar();
Pretty straight-forward, somewhat like static methods.
Before somebody asks me, why not use static methods in first place; I have some special situations in which I cannot use these methods as static. That's why I buit the methods directly under the namespace.
So, at my .PRO file, I added the .h and .cpp files from my pseudo-class like this:
HEADERS += \
....
include/foo.h
....
SOURCES += \
....
include/foo.cpp
----
Although it includes the files in my projects, it's throwing that LNK2019: unresolved external symbol error.
I'm no beginner with programming, but I'm a beginner with Qt.
Any help will be deeply appreciated.
ps: forgive my English mistakes.
first of all the scope operator is foo"::"method_bar();
See that the header is included before the usage of the function so that the places where ever you are calling this function knows about the declarations of the function
There is no problem in using namespace in QT.
One more suggestion would be
using namespace foo;
Looking at the complete code would really help me in resolving the problem

Qt vtable error

I'm going through the old Trolltech Qt tutorials as I find them more helpful than the newer ones and an undefined reference to vtable error hits me when I got to a class widget implementing its own signals.
http://doc.trolltech.com/4.0/tutorial-t7.html
Instead of keeping the code separate, I just copied them all in the same .cpp for convenience.
I snooped around and found that a vtable error is caused by an undefined virtual function. However, I didn't omit anything from the tutorial code except the code separation. Omitting the Q_OBJECT macro from lcdrange.h as well as the slots and signals seem to make the project compile. I figure it's void valueChanged(int newValue); at lcdrange.h that is causing the fuss but the tutorial did not provide a definition. I'm sure they won't post code that isn't working so I must be doing something wrong. I'm using C::B(MinGW) if that means anything. I'm also new to C++.
MOC must generate code for lcdrange.h and the generated code must be compiled and linked.
qmake ensures this for all header files listed in the HEADERS variable.
Make sure lcdrange.h is listed in the HEADERS section of your .pro file, and rerun qmake.

unable to call c++ functions in qt

I have a shared library of the C++ application and I am able to call it using the test app. I want to write a Qt UI for this. I am not able to call the C++ functions directly. Only if I give name mangled function name it works.
Also if I create an object of the C++ class and call a function of the class I am getting "undefined reference to " the function.
How can I call the C++ functions and create objects of C++ class and call functions on them?
You must include the path to the headers files. To do this in Qt Creator, modify your .pro file to include the following line:
INCLUDEPATH += path/to/header/files
you will notice that you MUST use the slash above... If you try using '\', it will not work.
It sounds like you're not correctly including the header files for the code you want to use. The "extra" features of QT don't stop all of the normal C++ features from working.