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

时间:2015-10-26 14:13:54

标签: c++ qt codeblocks

我是c ++的新手,但我正在尽力学习。 我有两个错误,我不知道为什么,这些是:

  1. 在构造函数'MyLabel :: MyLabel(QWidget *)'中:
  2. '('token -line 7
  3. 之前的声明中的Qualified-id
  4. '('token -line20
  5. 之前的声明中的Qualified-id

    我的代码如下:

    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为空白

2 个答案:

答案 0 :(得分:1)

如果您正在尝试在构造函数中定义函数,那么您会收到错误。 public Form1() { InitializeComponent(); this.ActiveControl = textBox1; } 所以

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())); } setAlignment不是setText的成员,所以如果他们不是QWidget的成员,那么您将需要删除其他内容不会编译。

答案 1 :(得分:0)

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

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