C++ 需要解释定义类时为什么使用一个冒号吗
我试图理解为什么下面的代码在子类之后使用一个冒号。我试着在网上搜索,但我没有得到有关其原因的信息 myLabel.h文件C++ 需要解释定义类时为什么使用一个冒号吗,c++,qt,C++,Qt,我试图理解为什么下面的代码在子类之后使用一个冒号。我试着在网上搜索,但我没有得到有关其原因的信息 myLabel.h文件 #ifndef MYLABEL_H #define MYLABEL_H #include <QApplication> #include <QMainWindow> #include <QHBoxLayout> #include <QLabel> #include <
#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
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);
}
/#包括“mainwindow.h”
#包括“mylabel.h”
#包括“矩形.h”
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QMainWindow*window=新的QMainWindow();
窗口->设置窗口标题(QString::fromUtf8(“QT-捕获鼠标移动”);
窗口->调整大小(500450);
QWidget*centralWidget=新的QWidget(窗口);
QHBoxLayout*布局=新的QHBoxLayout(centralWidget);
MyLabel*坐标标签=新的MyLabel();
布局->添加小部件(协调标签);
窗口->设置中心Widget(中心Widget);
窗口->显示();
返回app.exec();
}
void MyLabel::mouseMoveEvent(QMouseEvent*事件)
{
//在此处显示鼠标光标的x和y坐标值
QString txt=QString(“X:%1--Y:%2”).arg(事件->X()).arg(事件->Y());
setText(txt);
}
另外,有人能解释一下myLabel.cpp文件中的指针“this”指的是什么吗
谢谢大家这一个代表继承:
MyLabel
类是从QLabel
类派生的(使用公共继承,您可以阅读更多):
这个函数调用一个特定的基类构造函数,其思想是将MyLabel
的父小部件正确设置为parent
,并且由于QLabel
将指向父小部件的指针作为其构造函数参数,因此该构造函数被称为(更多信息):
在您的情况下使用
此
是不必要的成员函数中的此
指针指向正在运行函数的对象。哪个冒号让您感到困惑:类定义中的冒号(class a:public B
),或者构造函数中的冒号(a::a():B()
)“JLRRIN,我确信在阅读QT程序之前,你应该先读至少一本C++的书。):请某人解释一下“这个”的引用是什么:问Scott Meyers。我很清楚构造器,但我对它的定义感到困惑。没有解释为什么在这里使用冒号。这只是一条武断的规则。假设,C++已经被定义为允许代码>类A公共B<代码>。好,现在我的问题是:如果QLabel正在使用指向父QWIDGET的指针,QLabk参数不应该有**操作符吗?我想我现在就可以得到它了。这是初始化列表的一个例子。除了这些家伙发布的内容之外,我建议大家看看帖子:
//#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);
}
class MyLabel : public QLabel
{
MyLabel::MyLabel(QWidget *parent) : QLabel(parent)