Qt emit signal problem
-
Hi everyone here
i'm new in here and Qt.(having a lot problems)
what im doing is to draw a point (later a line a circle maybe) in a graphicsview mit mouse click
what ive done is like that:mainwindow.h
namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void paintEvent(QPaintEvent *ev); private: Ui::MainWindow *ui; myview view; };
myview.h
#include <QGraphicsView> #include <QMouseEvent> #include <QEvent> class myview : public QGraphicsView { //Q_OBJECT public: myview(QWidget *parent = 0); int x,y; protected: void mousePressEvent(QMouseEvent *ev); signals: void Mousepressed(); };
myview.cpp
#include <QMouseEvent> myview::myview(QWidget *parent) : QGraphicsView(parent) { } void myview::mousePressEvent(QMouseEvent *ev) { x = ev->pos().x(); y = ev->pos().y(); emit Mousepressed(); }
mainwindow.cpp
#include <QPainter> #include <myview.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(ui->graphicsView,SIGNAL(Mousepressed()),this,SLOT(paintEvent(QPaintEvent *ev))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *ev) { QPainter p(this); QPen pen(Qt::black); pen.setWidth(6); p.setPen(pen); p.drawPoint(view.x,view.y); }
when i bild the project i get this error:
undefined reference to myview::Mousepressed()Thanks in advance
[edit: corrected coding tags: use ```before and after SGaist]
-
Hi and welcome to devnet,
You commented out the Q_OBJECT macro from your myview class so moc doesn't run thus your signals are not implemented like they should be.
On a side note, your connection statement is invalid for two reasons:
- list item paintEvent is not a slot
- you can't connect a parameterless signal to a slot with parameters.
-
@SGaist Thank U so much. The Signal emit problem is solved perfect. I wanna ask how can I call the paintevent with a mouse click?
thanks in advance
-
You can't. paintEvent is called when needed, you can ask for a repaint but it doesn't mean it will be done immediately