QMouseEvent on button click
-
-
Hello,
I am fairly new to Qt and would like to know how I can use QMouseEvent? I have tried a lot of ways but I cannot get it to work.What I wanna do is when I click a button I want it to setText to the mouse X and Y coordinates.
Any help appreciated
@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
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 -
@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. -
@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.
-
@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 so if I want it to run when a mouse is pressed I just have to run themousePressEvent(QMouseEvent *eventPress)
inside myonButtonClicked
event? -
@uruloke
well Im not sure I understand.
Why dont you just run when button is clicked ?mousePressEvent is for click on some spot on mainwindow where no control.
Dont call the mousePressEvent your self. system call it.
@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. -
@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
try witheventPress->pos().x();
eventPress->pos().y();and no mapFromGlobal
oh and make sure its maiwindow::mousePressEvent(...) else it wont compile
if you are in the .cpp file. -
@mrjj
thanks a lot for the help.
It was just themapFromGlobal
that fucked it up.
It worked when I removed that.QPoint p = eventPress->globalPos();
ui->pixMap->setText( QString::number(p.x()) + "," + QString::number(p.y()));
-
@mrjj Sure, I am making a program to take screenshots with. So I need the mouse global position to mark the area to screenshot.
@uruloke
ahh. such beast. :)
make sure to borrow from
http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html -
@uruloke
ahh. such beast. :)
make sure to borrow from
http://doc.qt.io/qt-5/qtwidgets-desktop-screenshot-example.html