QMouseEvent on button click
-
@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.Lifetime Qt Championwrote on 8 Nov 2015, 12:07 last edited by mrjj 11 Aug 2015, 12:08@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.
-
@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.
wrote on 8 Nov 2015, 12:23 last edited by@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.Lifetime Qt Championwrote on 8 Nov 2015, 12:26 last edited by mrjj 11 Aug 2015, 12:28@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. -
@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()));
@uruloke
ok.super.Can I ask what you are programming ?
-
@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
15/15