Reputation: 13
I am a novice to c++, but I am doing my best to learn. I am getting two errors and I don't know why, these are:
My code is as follows:
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 is blank
Upvotes: 0
Views: 475
Reputation: 21230
In order to implement your custom label you have to derive your class from Qt's standard QLabel class as:
class MyLabel : public QLabel
{
Q_OBJECT
public:
explicit MyLabel(QWidget *parent = 0);
~MyLabel();
protected:
void mouseMoveEvent(QMouseEvent * event);
};
Unfortunately in C++ you can not define a function inside another function as you did in MyLabel::MyLabel()
constructor. Just write it in the following way:
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);
}
UPDATE
I would implement the handling of the mouse move event in this way:
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);
}
Upvotes: 0
Reputation: 180720
You are getting error are you are trying to define functions inside your constructor. MyLabel::MyLabel(QWidget *parent)
so be
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);
}
And then the definition for mouseMoveEvent
should follow after the constructor
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()));
}
EDIT:
As pointed out in the comments setAlignment
and setText
are not members of QWidget
so if they are not members of MyLable
then you will need to remove those otherwise it will not compile.
Upvotes: 1