Important: Please read the Qt Code of Conduct -

Doubt in Screenshot Example Code

  • Hi All,
    I'm newbie and I've just started with Qt creator. Also I had just some basic knowledge of C++ programming in general. In the screenshot example bundled with Qt creator, I've seen a class definition with the following code.

    class QCheckBox;
    class QGridLayout;

    class Screenshot : public QWidget
    void resizeEvent(QResizeEvent *event);
    private slots:
    void newScreenshot();
    void createOptionsGroupBox();

    My doubts are,

    1. What does the QT_BEGIN_NAMESPACE block in the above code means?
    2. In the class definition there is a statement Q_OBJECT without a semicolon. What does that line mean?
    3. There is an access specifier named private slots: in the class definition - Is this valid? How & What does it mean?

    Thanks in advance

  • Something like this:

    • QT_BEGIN_NAMESPACE is a macro, that can be used for wrapping Qt things inside some specific namespace.

    • Q_OBJECT tells Qt qmake to create all the things needed for signals and slots.

    • "signals" and "slots" keywords are Qt extensions to C++ language, and they are preprocessed by qmake.

  • Napster, welcome to our forum!

    Qt can be compliled in such a way, that all Qt classes are inside Qt namespaces. The macro QT_BEGIN_NAMESPACE and QT_END_NAMESPACE will be defined to the correct value if so, and will be defined to empty otherwise. Using these macros, the example will run on both a Qt that has been compiled to use namespaces and those that have not been.

    Q_OBJECT is a core part of the Qt (meta) object system. It both acts as a trigger for the moc tool (that generates code) and actually expands to some code. The macro is needed to make signals and slots work, but also to enable other introspection features and properties. Basically, if you create a class that subclasses QObject (or any class derived from it), you should put the Q_OBJECT macro in the private section of your class declaration. By convention is it at the top. Any class that uses signals and/or slots, needs to derive from QObject.

    Slots in Qt are really just normal methods. The actual slots keyword is really a macro as well (you can also use Q_SLOTS if you want) that expands to... nothing. However, it is used by the moc tool to generate code to allow calling the method using the signals & slots mechanism. Just like normal methods, these slots can be public, protected or private. Note that signals are (currently!) all protected, but will be public in Qt 5.

  • Thanks Andre, that was a great answer, thanks to you for sparing the time and effort for a newbie. One more doubt from this section is that, as you talk about slots, are they anyway related to callback functions? Why those functions are different, to be placed in private slots:? Is it just because they respond to widget events?

  • Slots are a bit like callback functions, yes, only a bit more nicer to work with IMHO. The slots in question are private, because they have no meaning outside the context of the class they are defined in. It is just a matter of API design. If the slot is public, then anyone can call it and connect to it, while in this case it is only mend as an internal thing. That is not different from why you would make some normal class methods private, some protected and some public.

  • @Andre, @Broadpeak, @Mkosim
    Thank You...

Log in to reply