我收到两个错误,但我无法识别它们

I am getting two errors but I cannot identify them

我是c++的新手,但我正在努力学习。 我收到两个错误,我不知道为什么,它们是:

  1. 在构造函数中'MyLabel::MyLabel(QWidget*)':
  2. 第 7 行“(”标记之前的声明中的合格 ID
  3. '('标记前的声明中的合格 id -line20

我的代码如下:

mylabel.cpp:

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

MyLabel::MyLabel(QWidget *parent) :
    QWidget(parent)
{
    void MyLabel::MyLabel()
    {
        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);
    }



    void MyLabel::mouseMoveEvent(QMouseEvent * event)
    {
        //Show x and y coordinate values of mouse cursor here
        this->setText("X:" + QString::number(event->x()) + "-- Y:" + QString::number(event->y()));
    }


}        

mylabel.h:

#ifndef MYLABEL_H
#define MYLABEL_H

#include <QObject>
#include <QApplication>
#include <QMainWindow>
#include <QMouseEvent>

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

    void mouseMoveEvent(QMouseEvent * event);

signals:

};

#endif // MYLABEL_H 

main.cpp

#include "mainwindow.h"
#include "mylabel.h"
#include <QApplication>
#include <QHBoxLayout>

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

    QMainWindow *window = new QMainWindow();

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

    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();
}

mainwindow.cpp为空

您正在尝试在构造函数中定义函数时遇到错误。 MyLabel::MyLabel(QWidget *parent) 就这样吧

MyLabel::MyLabel(QWidget *parent) : QWidget(parent)
{
    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);
}

然后 mouseMoveEvent 的定义应该跟在构造函数

之后
void MyLabel::mouseMoveEvent(QMouseEvent * event)
{
    //Show x and y coordinate values of mouse cursor here
    this->setText("X:" + QString::number(event->x()) + "-- Y:" + QString::number(event->y()));
}

编辑:

正如评论中指出的那样,setAlignmentsetText 不是 QWidget 的成员,因此如果他们不是 MyLable 的成员,那么您需要删除它们否则无法编译。

为了实现您的自定义标签,您必须从 Qt 的标准 QLabel class 派生您的 class 作为:

class MyLabel : public QLabel
{
    Q_OBJECT
public:
    explicit MyLabel(QWidget *parent = 0);
    ~MyLabel();
protected:
    void mouseMoveEvent(QMouseEvent * event);
};

不幸的是,在 C++ 中,您不能像在 MyLabel::MyLabel() 构造函数中那样在另一个函数中定义一个函数。按照下面的方式写就可以了:

MyLabel::MyLabel(QWidget *parent) : QLabel(parent)
{
    setAlignment(Qt::AlignCenter);

    //Default Label Value
    setText("No Value");

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

更新

我会这样实现鼠标移动事件的处理:

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);
}