cpp/Qt : per class debugging - c++

I'm developing a Qt application. For each class, I'm trying to mimic the framework, such as error() and errorString() method, use of Private implementation.
But I would like to add a per class debugging:
Set a macro to the desired level of debug,
have a macro or a function that knows the level of debug, and use qDebug() or qWarning()
which is class independant, and will know the current class's name (for some pretty prints)
Anyone have a good idea to implement this ?

Maybe the QxtLogger class, part of the Qxt library (an extension library for Qt) provides what you need.
I would definitely consider using something already existing and tested rather than implementing my own logging solution.

You may write a class, for example CDebug with all needed debug methods, and use it in other classes, like:
class CMyDialog : public QDialog, public CDebug {...};

Related

Whats the alternative way to serialize inherited types in WCF

I want to use inherited types in WCF ,but i dont want to add KnownType attribute of all types in the base class, because the base is in another assembly ,also to avoid dependencies.
So, what are the other way to achieve that?
I think there are at least two ways to do this.
You can create your own classes (DTO) and map data from other assembly to your classes. For mapping you can use AutoMapper. It has following advantages:
Your communication layer is separeted from your logic (I'm assuming that this other assembly contains logic)
You will decide how your API looks like and your protocol will not change without your knowledge (I'm assuming here that this other assembly is a library or someone else is responsible for it).
You can add ServiceKnownType to your ServiceContract interface more or less like below (I did not test the code):
[DataContract]
public class BaseClass {}
[DataContract]
public class DerivedClass : BaseClass {}
[ServiceKnownType(typeof(BaseClass))]
[ServiceKnownType(typeof(DerivedClass))]
[ServiceContract()]
public interface IYourContract
{
[OperationContract]
BaseClass[] GetClasses();
}
For more information check out documentation here. There is really good example.
Which is beter?
It depends on what you need. If your are developing prototype or you need to do something fast go for option 2. If you need more reliable solution, that will stay with you longer choose option 1.
There are more ways of specifying the known types. One of them is to put them in your configuration file. That way your service doesn't know about them at compile time. But in order to serialize them, it has to know about them at runtime.

QT: Private member instead of inheritance? What is the reason? Is this a specific concept?

Some time ago I programmed a GUI with QT Designer / QT Creator.
For this question I will first state a schematic of how the general process of creating a GUI with the mentioned IDE works:
Creating the design with the QT Designer -> Get .ui files
The .ui files are translated into header files and you especially get something like "UIbasisclass.h" (with class UIbasisclass) .
You create something like an "UIsubclass.h" (with class UIsubclass) yourself making one private member UIbasisclass ui.
Code within class UIsubclass:
...
private:
Ui::UIbasisclass ui;
...
Finally you will create an object of UIsubclass in the main method -> Code:
...
UIsubclass *MyGUI = new UIsubclass();
...
where the constructor of UIsubclass consists among other code of:
...
ui.setupUi(this);
...
In short: We have a UIsubclass that is mostly responsible for applicational methods, but also has a private member of UIbasisclass named ui that consists mostly of design code.
When we create an object of UIsubclass its private member UIbasisclass ui is initialized within the constructor of UIsubclass with the object of UIsubclass itself (?). [see: this pointer]
My questions are now:
Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass? Instead one object of UIbasisclass becomes member of UIsubclass.
Is this some specific advantageous concept (if yes which advantages has it or how is it named?) or is it "just" a necessity of the QT code structure?
Let me know if I have to specify my questions or if there are any questions.
You can do with private inheritance, it is even documented in Qt documentation.
The use of a private member for ui is the default because of the templates used by Qt Creator, Qt itself does not care.
Why isn't there used inheritance in the way that UIsubclass inherits from UIbasisclass?
You're asking us about why you didn't do it in your own code? Just do it. It's up to you. It truly is your code. You are responsible for its design. If you're using a template, it's there to help you get started, but not to design your software for you. The responsibility is yours, and yours only.
it "just" a necessity of the QT code structure?
There is no such necessity. The Ui class is a POD class with a bunch of pointers and one method. Nothing much to it. If you want to privately inherit from it: go right ahead.
Because with a private member you can forward declare the generated class:
namespace Ui {
class MyForm;
}
class Foo {
private:
Ui::MyForm *ui;
};
and on the .cpp file you insert the include.
this way all of the possible includes of this file will not have to preprocess that file again.

How to automatically create virtual methods from inherited class in Qt Creator?

I am using QT4.8.4 + Qt Creator 2.8.1. Now I need to create several classes Child_X that inherit from another class Parent. In Parent I have several virtual methods.
Now I have to implement them in all of my Child_X classes. To save editing time, I'd like Qt do that for me automatically. When I remember right there is the possibility to have Qt create all the virtual methods. Does anybody know how?
Thank you
Sorry, I did not formulate correctly: I did not mean that Qt will automatically write the body of the methods. ( To invent that would probably make you very rich :-) )
I was talking about Qt writing all the headers of the virtual methods in the newly created (inherited) class. This saves a lot of writing/copying classnames etc.. The body would be empty in all the virtual methods.
Thank you
itelly
I hope you already found it, but maybe for others:
Right click on the class name in the editor.
In the menu, click on 'Refactor' and then on 'Insert virtual functions of base classes'.
You can choose to directly make the functions in the implementation file (as well as in the header file).
there is the possibility to have QT create all the virtual methods
There is no such possibility, because Qt can't read your mind and divine what those implementations are supposed to do.
Speaking of that - what are your virtual methods supposed to do? Please edit the question to fix that.

Override a Qt class with an embedded private class

In many cases, I would like to override a Qt class to extend or modify its behavior. But almost all Qt classes uses an internal private class such as QNetworkDiskCachePrivate inside QNetworkDiskCache. I know there are advantages of this approach. But there is a HUGE problem of the private class: it makes overriding the class a lot more difficult. With other C++ class library with source code, I usually override a class method, copy the code from the implementation in the parent class and make small modifications here and there to achieve the behavior I want. However, in Qt, the private class is not exported and not visible to the derived class. Since Qt classes maintains the critical internal data in the private class through the "d" member, the invisibility of the private internal class makes the possibility of behavior extension very limited. You can only play with the few exposed public method.
I tried extracting the entire source files of the class and renaming the class name and file names. But the Qt class library is so much intertwined that extracting a single class out of it is messy as well in most cases.
Do I miss something here? Or Qt classes are just really bad in terms of extendability?
Qt classes are better than most in terms of extendability; they often have hooks to change their behavior without resorting to copying and pasting an entire method. Having said that, if the generally accepted methods of extending don't work, yes the Qt classes are harder to hack. That's probably a good thing because copying-pasting-and-modifying the base class implementation means that your derived class won't get any improvements or bugfixes that are made in the base class implementation.
If you want to do it, you're going to need to convince your build system to let you include the private headers and then refer to the private classes from your new implementation. Pay attention to the disclaimer in the Qt docs; when you do this you are opening yourself up to breakage with every new version of Qt that is released (Qt only guarantees the public API, and you're messing with its internals). Qt's public API is wonderfully readable and documented; the internal code can be pretty cryptic, so you really, really want to be sure that you can't accomplish what you want with the public API. If you're still resolved to use the private class stuff, this might help.
Your approach is wrong and bad! To extend a Qt, and more in general C++, code you don't have to copy the source code and modify it where you need. You should (have to) use extending OOP(Object Oriented Programming) paradigm. 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
In this way B does all that base class A does and you add your methods(extend base class) to fit your needs. If you take a look at qt example code this is usually the approach used to do something not included into default qt widget behaviour. In example ItemDelegate customizations: you write your own class MyItemDelegate that extend QItemDelegate: link
Or Qt classes are just really bad in terms of extendability?
Qt is "only" a framework build up C++ language. This means that everything you can do in C++ you can do with Qt.

Simulate qobject_cast failure

I have a pure virtual class A, on which I do
Q_DECLARE_INTERFACE(A, "org.something.A")
and I have a plugin implemented as a class B which inherits A and QObject, has as an interface A and implements all pure virtual methods of A
class B : public QObject, public A
{
Q_OBJECT
Q_INTERFACES(A)
public:
void someMethod();
}
and in the cpp :
Q_EXPORT_PLUGIN2(A, A)
This works well. In reality there are many different interfaces, and the core application (which I haven't written and on which I can't do big modifications) calls
qobject_cast<A *>(bPointer);
and checks the result to know if a certain plugin implements an interface.
All this works very well.
How ever, I'd like in the B class to determine at runtime whether or not I want to implement a certain interface.
The methods would always be implemented, but I would like sometimes make qobject_cast fail but determined at runtime (A single instance would always fail or succeed for the same interface).
This might sound strange, but the reason for this is I would like to add Python (or other languages) plugins. They would have a C++ wrapper. Their python source code would be stored using an rcc file. The c++ code should be the same for all python plugins.
The c++ wrapper would call a python method the determine which interfaces are implemented by the python code, and make qobject_cast fail if the python code doesn't implement to interface.
The C++ wrapper class would implement all interface methods and forward the call to python, but only the ones which have casted succesfully would really be called.
It may be feasable by reimplementing QObject's meta related methods, but I don't know which.
I hope you understand what I try to do (although it's not very clear). Maybe there is a completly different way of doing this ?
Thanks
You cannot change qobject_cast behavior, using only public Qt APIs.
Also note, that you can have only one plugin object instance for every plugin library.
A solution will be a plugin, working as factory and returning interfaces by request.