QMouseEvent on button click
-
-
@uruloke
ok
1: place button on mainform
2: right click button
3: select Goto Slot
4: Select "released" from the list
Now you got the click handlervoid MainWindow::on_pushButton_released() { QPoint p = mapFromGlobal(QCursor::pos()); ui->pushButton->setText( QString::number(p.x()) + "," + QString::number(p.x())); }
-
@uruloke
well also I forgot to mention
to use QMouseEvent
you would override
void mousePressEvent(QMouseEvent *eventPress);
for the widget.
like here
http://doc.qt.io/qt-5/qtwidgets-widgets-scribble-example.html -
@mrjj
Okay, so Qcursor works for me, but I cannot getQMouseEvent
to work correctly.void MainWindow::on_screen2Button_clicked(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }
It doesn't give me any errors, but it doesn't print the text out either, it worked with Qcursor thoughEDIT 1:
cool, will look into the example. -
@uruloke
you have to addprotected:
void mousePressEvent(QMouseEvent *eventPress);to mainwindow .H
and
void mousePressEvent(QMouseEvent *eventPress) {
}
in .cppits a virtual function and must be named like this.
It will then be called by system.
What you seems to try is to put MouseEvent *eventPress to a button click.
and it will be zero as it not filled out by system since a buttons click is not the same as mouse click.
so you sort of give it a empty QMouseEvent.Note its protected: that is important.
-
-
@mrjj
Ah, yeah I forgot that it was an event :)void mousePressEvent(QMouseEvent *eventPress) { QPoint p = mapFromGlobal(eventPress->globalPos()); ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.x())); }
I get errors on
mapFromGlobal
now andui
, didn't get that before. -
@uruloke
ahh. such beast. :)
make sure to borrow from
http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html