-5

I am trying to understand why is the code below using one colon after the child class. I tried searching online but I got no info about its cause.

myLabel.h file

    #ifndef MYLABEL_H
    #define MYLABEL_H
    #include <QApplication>
    #include <QMainWindow>
    #include <QHBoxLayout>
    #include <QLabel>
    #include <QMouseEvent>


    class MyLabel : public QLabel
    {
        Q_OBJECT
    public:
        explicit MyLabel(QWidget *parent = 0);
        ~MyLabel(){}

    protected:
        void mouseMoveEvent ( QMouseEvent * event );

    };

    #endif // MYLABEL_H

myLabel.cpp file

    #include "mylabel.h"
    #include "ui_mainwindow.h"

    MyLabel::MyLabel(QWidget *parent) : QLabel(parent) //QWidget calls the widget, QLabel calls text
    {
          this->setAlignment(Qt::AlignCenter);

          //Default Label Value
          this->setText("No Value");

          //set MouseTracking true to capture mouse event even its key is not pressed
          this->setMouseTracking(true);

    }

main.cpp

//#include "mainwindow.h"
#include "mylabel.h"
#include "rectangle.h"
#include <QApplication>
#include <QHBoxLayout>
#include <QPainterPath>

           int main(int argc, char *argv[])
            {
                QApplication app(argc, argv);

                QMainWindow *window = new QMainWindow();

                    window->setWindowTitle(QString::fromUtf8("QT - Capture Mouse Move"));
                    window->resize(500, 450);

                    QWidget *centralWidget = new QWidget(window);
                QHBoxLayout* layout = new QHBoxLayout(centralWidget);

                    MyLabel* CoordinateLabel = new MyLabel();
                layout->addWidget(CoordinateLabel);

                window->setCentralWidget(centralWidget);

                window->show();
                return app.exec();
            }



    void MyLabel::mouseMoveEvent( QMouseEvent * event )
    {
        // Show x and y coordinate values of mouse cursor here
        QString txt = QString("X:%1 -- Y:%2").arg(event->x()).arg(event->y());
        setText(txt);
    }

Also, can someone please explain what is the pointer "this" referencing to in the myLabel.cpp file?

Thank you all

Paul R
  • 208,748
  • 37
  • 389
  • 560
JLREng
  • 13
  • 4
  • 1
    Which colon is confusing you: the one at the class definition (`class A : public B`), or the one in the constructor (`A::A() : B()`)? – SingerOfTheFall Oct 28 '15 at 11:40
  • 6
    @JLREng I am sure you should at first read at least one book on C++ before iinvestigating Qt programs.:) – Vlad from Moscow Oct 28 '15 at 11:41
  • Can someone please explain what is the pointer "this" referencing: Ask Scott Meyers. – jpo38 Oct 28 '15 at 11:42
  • @singerofthefall the class definition. I got very clear about the constructor one, but I am confused about the definition. – JLREng Oct 28 '15 at 11:47
  • There's no _explanation_ why a colon is used there. It's just an arbitrary rule. Hypothetically, C++ could have been defined to allow `class A public B`. – MSalters Oct 28 '15 at 11:57
  • @m.s. I am reading it as I write this. Thank you. – JLREng Oct 28 '15 at 11:58

1 Answers1

2

This one represents inheritance: MyLabel class is derived from QLabel class (public inheritance is used, you can read more here):

class MyLabel : public QLabel
    {

This one calls a specific base class constructor, the idea is to properly set MyLabel's parent widget to parent, and since QLabel takes a pointer to the parent widget as it's constructor parameter, this constructor is called (more info here):

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)

Using this in your case is unnecessary. this pointer inside a member function points to an object on which the function is being run.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
  • Okay, now my question is: If QLabel is taking a pointer to the parent Qwidget, shouldn't the QLabel parameter have the '*' operator? – JLREng Oct 28 '15 at 11:55
  • I think I get it now. This is a case of initializer list. In addition to what these guys posted I would suggest to take a look to the thread: http://stackoverflow.com/questions/120876/c-superclass-constructor-calling-rules – JLREng Oct 28 '15 at 12:04