我试图理解为什么下面的代码在子类之后使用一个冒号。我尝试在线搜索,但我没有得到有关其原因的信息。
myLabel.h文件
#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文件
#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);
}
此外,有人可以解释什么是指针&#34;这&#34;在myLabel.cpp文件中引用?
谢谢大家