Reputation: 13
'I am currently having an issue tryinh to compile this program. The program is supposed to show the coordinates of the mouse on a GUI QWidget The error is in line 6 of the mainwindow.cpp file'
//header
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QApplication>
#include <QMainWindow>
#include <QMouseEvent>
#include <QMessageBox>
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
void mouseReleaseEvent(QMouseEvent * event);
~MainWindow();
private:
Ui::MainWindow *ui;
QMessageBox *msgBox;
};
#endif // MAINWINDOW_H
' mainwindow.cpp file'
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow()
{
MainWindow::mouseReleaseEvent (QMouseEvent * event);
}
void MainWindow::mouseReleaseEvent(QMouseEvent * event)
{
msgBox = new QMessageBox();
msgBox -> setWindowTitle("Coordinates");
msgBox -> setText("You released the button");
msgBox -> show();
}
MainWindow::~MainWindow()
{
delete ui;
}
'main.cpp'
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow *w = new MainWindow();
w->setWindowTitle(QString::fromUtf8("QT-capture mouse release"));
w->resize(300, 250);
w->show();
return a.exec();
}
Please help, I know it is something related to pointers and possibly mutators, but I can't see it yet. Thank you.
Upvotes: 1
Views: 558
Reputation: 21514
This is illegal:
MainWindow::MainWindow()
{
// illegal:
MainWindow::mouseReleaseEvent (QMouseEvent * event);
}
If you wish to call the handler manually, you need to create an event and pass it:
MainWindow::MainWindow()
{
QMouseEvent event;
MainWindow::mouseReleaseEvent(&event);
}
But you then need to set the QMouseEvent attributes correctly/ It's hard to tell how to do so without knowing why you want to do that.
Wht are you doing that? Those events are emitted automatically upon mouse activity, you don't need to manually call mouseReleaseEvent, it will be called when you release the mouse button.
If you want to show mouse position, I suggest that you:
mouseReleaseEvent
by mouseMoveEvent
MainWindow::MainWindow()
MainWindow::mouseMoveEvent(QMouseEvent * event)
write mouse coordinates in a label of the main window rather than using a mesage box (format a QString
with mouse coordinates using QMouseEvent::pos
and changing label text using QLabel::setText
)Like that:
void MainWindow::mouseMoveEvent(QMouseEvent * event)
{
std::stringstream str;
str << "Mouse position is " << event->pos.x() << ";" << event->pos().y();
ui->label->setText( str.str().c_str() );
}
Upvotes: 1