Override Qt class with built-in private class

In many cases, I would like to override the Qt class to extend or change its behavior. But almost all Qt classes use an internal private class such as QNetworkDiskCachePrivate inside QNetworkDiskCache. I know that there are advantages to this approach. But there is a serious problem with a private class: it makes redefining a class much more difficult. With another C ++ class library with source code, I usually override the class method, copy the code from the implementation in the parent class, and make small changes here and there to achieve the desired behavior. However, in Qt, the private class is not exported and is not visible to the derived class. Since Qt classes support critical internal data in a private class through the "d" member, the invisibility of the private internal class makes the expansion behavior very limited. You can only play with a few open public methods.

I tried to extract all the class source files and rename the class names and file names. But the Qt class library is so intertwined that extracting one class from it is also messy in most cases.

Did I miss something? Or are Qt classes just really bad in terms of extensibility?

+7
source share
2 answers

Qt classes are better than most in terms of extensibility; they often have hooks to change their behavior without resorting to copying and pasting the whole method. Having said that, if generally accepted extension methods do not work, yes, Qt classes are harder to crack. This is probably good, because copy-paste and modification of the base class implementation means that your derived class will not receive any improvements or corrections that are made to the base class implementation.

If you want to do this, you will need to convince your build system so that you can include private headers and then refer to private classes from your new implementation. Note the disclaimer in the Qt docs; when you do this, you open yourself up for a break with each new version of Qt released (Qt guarantees only a public API, and you mess with your internal components). The open Qt API is well read and documented; the internal code can be rather cryptic, so you really want to be sure that you cannot accomplish what you want with the public API. If you still decide to use private class material, this may help .

+1
source

Your approach is wrong and bad! To extend Qt and, moreover, C ++ code, you do not need to copy the source code and modify it where you need to. You should (should) use the OOP (Object Oriented Programming) paradigm extension. In C ++ you should write something like:

#include <iostream> using namespace std; class A { public: A(){x=1;} void writeName(){cout << "my name is A";} void writeNumber(){cout << "my number is " << x << endl;} private: int x; }; class B : public A { public: B(){} void writeName(){cout << "my name is B and i'm better then A" << endl;} }; int main() { B *b= new B(); b->writeName(); b->writeNumber(); return 0; } //output: my name is B and i'm better then A my number is 1 

Thus, B does everything that the base class A does, and you add your methods (extend the base class) according to your needs. If you look at the sample qt code, this is usually the approach used to execute something that is not included in the default qt widget behavior. In the example settings for ItemDelegate: you write your own class MyItemDelegate, which extends QItemDelegate: link

Or are the Qt classes just awful in terms of extensibility?

Qt is a "only" structure that creates the C ++ language. This means that everything you can do in C ++ you can do with Qt.

-one
source

All Articles