Private implementation Slots
Disclaimer: This article descrbes techiques that are not part of the public qt api, using them may result in non-portable or version specific code. the example below was tested with version 4.3.3 on 64bit Linux.
OK so, now that we have the disclaimer out of the way... if you are a QT developer you are probably familiar with the concept of private implementation, or "pimpl" classes. if not, well, I won't get into that here, but if you look it up on Google or Wikipedia you shoshould get the idea fairly easily.
Now, say you have a private class and you want it to have slots. one way is to add a slot to your public class then have it call the method you want in the private class, but this means you have to add a method to public API which means you break binary compatibility. another is to have your private class extend qobject, but this adds overhead. another way is to useQ_private_slot
Macro, which I will explain here.
First lets look at how to set up the private class.
File: Slottest_p.h
# Ifndef slottest_p_h _
# Define slottest_p_h _
# Include "slottest. H"
Class slottestprivate
{
Q_declare_public (slottest)
Public:
Slottestprivate (slottest * qq): q_ptr (qq ){}
PRIVATE:
Void Bob () {qwarning ("Bob! ");}
Slottest * q_ptr;
};
# Endif
TheQ_declare_public (slottest)
Macro definition is:
# Define q_declare_public (class )/
Inline class * q_func () {return static_cast (q_ptr );}/
Inline const class * q_func () const {return static_cast (q_ptr );}/
Friend class;
The main purpose of this is to defineQ_func ()
Method To make sure we can accesses it with the correct const-ness and ensure its cast to the correct type. In practice, when implementing methods of the private class you should always Ally never useQ_func ()
OrQ_ptr
Directly; instead you shoshould placeQ_q (class)
Macro, which is defined:# Define q_q (class) class * const q = q_func ()
, At the beginning of the method implementation. From then on in the method you can use the pointerQ
To refer to the public class. For example:
Void myclassprivate: somemethod (){
Q_q (myclass );
Q-> settext ("Hello world! ");
}
OK, so now we have our private class, Lets create the public class.
File:Slottest. h
# Ifndef slottest_h _
# Define slottest_h _
# Include <qtcore/qobject>
Class slottestprivate;
Class qtimer;
Class slottest: Public qobject
{
Q_object
Q_declare_private (slottest)
Public:
Slottest ();
PRIVATE:
Q_private_slot (d_func (), void Bob ())
Qtimer * timer;
Slottestprivate * d_ptr;
};
# Endif
It's important that you don't include the actual private file here, otherwise you will get errors like:
Slottest_p.h: 8: Error: iso c ++ forbids declaration of 'slottest' with no type
Slottest_p.h: 8: Error: 'slottest' declared as an 'line' Field
Slottest_p.h: 8: Error: Expected '; 'before' *' token
Slottest_p.h: 8: Error: Expected '; 'before' inline'
Slottest_p.h: 8: Error: iso c ++ forbids declaration of 'slottest' with no type
Slottest_p.h: 8: Error: 'slottest' declared as an 'line' Field
Slottest_p.h: 8: Error: Expected '; 'before' *' token
Slottest_p.h: 8: Error: Expected '; 'before' friend'
Make: *** [main. O] Error 1
We have to includeQ_object
Macro here even though this example class doesn't actually have any signals or slots of it's own because otherwise MOC won't even look at it in the first place.
TheQ_declare_private (class)
Macro is just the counterpart toQ_declare_public (class)
Macro cannot it creates a method named d_func () to access the private class. Likewise, there isQ_d
Macro for use inside method implementations so you can use D as a pointer to the private class.
TheQ_private_slot
Macro takes a pointer to your private class, and we will take advantage ofD_func ()
Method to get it. next is the signature of the private class 'method to call. one interesting thing about this macro is that it directly expands to no actual code-it simply a trigger for MOC.
We are also creating a qtimer to call this slot repetadly for the example.
Now our implementation:
File:Slottest. cpp
# Include <qtcore/qobject>
# Include <qtimer>
# Include "slottest. H"
# Include "slottest_p.h"
# Include "moc_slottest.cpp"
Slottest: slottest (): qobject ()
{
D_ptr = new slottestprivate (this );
Timer = new qtimer (this );
Timer-> setinterval (1000 );
Connect (timer, signal (timeout (), this, slot (Bob ()));
Timer-> Start ();
}
The most important thing to notice here is that we have listed the MOC-generatedMoc_slottest.cpp
File as an include, doing this ensures thatSlottestprivate
Has been defined before the MOC file is processed. If you were to not include of this file here, you may get an error like:
/Home/kdab/workspace/PRTST/slottest. h: 10: Undefined reference to 'vtable for slottest'
Here, we just set up our private class, a timer, and connect the timer to call our slot. Notice that even though the method we are calling is inside our private class, we still useThis
In the connection because the slot isTechnicallyA slot of our public class, even though the implementation is in the private class.
Now lets make it runnable:
Filename:Main. cpp
# Include <qtcore/qcoreapplication>
# Include "slottest. H"
Int main (INT argc, char * argv [])
{
Qcoreapplication app (argc, argv );
Slottest Bob;
App.exe C ();
}
And one final very important thing, the project file...
Template = app
Target =
Dependpath + =.
Includepath + =.
# Input
Headers + = slottest. h slottest_p.h
Sources + = Main. cpp slottest. cpp
It's very important thatSlottest. h
Is listed beforeSlottest_p.h
Otherwise you will end up with the following error:
Moc_slottest.cpp: In member function 'virtual int slottest: qt_metacall (qmetaobject: Call, Int, void **)':
Moc_slottest.cpp: 64: Error: Invalid use of undefined type 'struct slottestprivate'
Slottest. h: 5: Error: Forward Declaration of 'struct slottestprivate'
Once you have it built, you shoshould see "Bob! "Printed to the screen every second fromBob ()
Method in the private class.