Hi. The function mousePressEvent doesnt "run" when I click. Here is the code:
customscene.h
#ifndef CUSTOMSCENE_H
#define CUSTOMSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneEvent>
{
Q_OBJECT
public:
explicit CustomScene
(QObject *parent
= 0);
signals:
public slots:
protected:
};
#endif // CUSTOMSCENE_H
#ifndef CUSTOMSCENE_H
#define CUSTOMSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneEvent>
class CustomScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit CustomScene(QObject *parent = 0);
signals:
public slots:
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
};
#endif // CUSTOMSCENE_H
To copy to clipboard, switch view to plain text mode
customscene.cpp
#include "customscene.h"
#include <QMessageBox>
CustomScene
::CustomScene(QObject *parent
) :{
}
{
msg.setText("clicked"); //a test to see if the function is called
msg.exec();
}
#include "customscene.h"
#include <QMessageBox>
CustomScene::CustomScene(QObject *parent) :
QGraphicsScene(parent)
{
}
void CustomScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
QMessageBox msg;
msg.setText("clicked"); //a test to see if the function is called
msg.exec();
}
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
CustomScene *scene;
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new CustomScene;
ui->GraphicsView->setScene(scene);
}
void MainWindow::on_button_clicked()
{
scene->addRect(0,0,630,280);//draw a big rect
borda.setStyle(Qt::DashDotLine);
borda.setWidth(2);
borda.setBrush(Qt::green);
borda.setCapStyle(Qt::RoundCap);
borda.setJoinStyle(Qt::RoundJoin);
fundo.setStyle(Qt::SolidPattern);
fundo.
setColor(QColor(0,
0,
255,
85));
QRect rect
(30,
30,
60,
60);
//draw a small rect inside
scene->addRect(rect, borda, fundo);
ui->GraphicsView->show();
}
CustomScene *scene;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new CustomScene;
ui->GraphicsView->setScene(scene);
}
void MainWindow::on_button_clicked()
{
scene->addRect(0,0,630,280);//draw a big rect
QPen borda;
borda.setStyle(Qt::DashDotLine);
borda.setWidth(2);
borda.setBrush(Qt::green);
borda.setCapStyle(Qt::RoundCap);
borda.setJoinStyle(Qt::RoundJoin);
QBrush fundo;
fundo.setStyle(Qt::SolidPattern);
fundo.setColor(QColor(0,0,255,85));
QRect rect(30,30,60,60); //draw a small rect inside
scene->addRect(rect, borda, fundo);
ui->GraphicsView->show();
}
To copy to clipboard, switch view to plain text mode
It was supposed to pop the MessageBox when I click the GraphicsView, but nothing happens
Bookmarks