PDA

View Full Version : Help with mousePressEvent implementation



QuTe
13th October 2007, 11:30
Hi all,

I've a QLabel wherein, I've to trigger an action according to the mouse presses inside that label rectangle. For this I've implemented a mouePressEvent function in which, I'm checking for the mouse Press event's pos(), to check out whether it falls in the first half or the second half of the label rectangle,(I've divided the total rect of label widget into 2 halfs) and on the results of which I'm triggering the right action accordingly.
Now the programs works fine in a way only thing is, it requires double click to actually trigger the particular action which I've set in the slot. I'm wondering why is it requiring double clicks and not working with a single click on it??
The parent widget is the QMainWindow and there are couple of other widgets in the QMainWindow widget along with this QLabel widget.
is there any problem of mouse focus to the QLabel widget? or am I missing something very basic over here?

plz help.

regards.

marcel
13th October 2007, 11:44
No, it must be your implementation. Can you post some code, or review it yourself?

QuTe
13th October 2007, 12:37
No, it must be your implementation. Can you post some code, or review it yourself?
ok the mousePressEvent() is here:

void MainWindow::mousePressEvent(QMouseEvent* event)
{
QRect r = centralWidget->rect().intersect(pageLabel->frameRect());

QRect firstHalf(r.x(), r.y(), (r.width() / 2), r.height());

QRect secondHalf((r.x() + (r.width() / 2) ), r.y() , (r.width() / 2), r.height());

if(firstHalf.contains(QPoint(event->x(), event->y()), true)) {
scribblePad->navigatePrev();
setPageNumber();
}
else if(secondHalf.contains(QPoint(event->x(), event->y()), true)) {
scribblePad->navigateNext();
setPageNumber();
}
}

and as i said the parent widget is the a QMainWindow widget, in which couple of other widgets are also along with this QLabel widget. The scribblePad over there is such another widget, which is used for scribbling purpose and this label represents its page numbers.
And the problem is, the QLabel requires two clicks to actually trigger the action put into the respective slots.

pls tell me your review of the code.

Regards

marcel
13th October 2007, 12:53
You must reimplement the QLabel's mouse press event, not the one for the window.
Most likely that is why you're getting thius behavior.

To access scribblePad from the labels's mouse event you have to make your main window a singleton. Then you can do something like MainWindow::instance()->getScribblePad()->navigateNext().

Regards

QuTe
15th October 2007, 12:32
Hi,

as per your suggestion I've reimplemented the mousePressEvent() for QLabel only, and not for the main window; but unfortunately the problem still persists and it requires double clicks to actually trigger the respective actions!

is there something else I need to do to solve this problem?

Regards

QuTe
17th October 2007, 13:19
is it too enigmatic to get a solution for?

I still have not been able to get it with a single click somehow :(
can someone help?

Thanks

jpn
26th October 2007, 13:24
Is this still unsolved? How does the mousePressEvent() implementation look like now?

QuTe
29th October 2007, 05:43
No, it has solved my problem.
sry I couldn't msg over here back then b'coz it worked out after couple of days of my last post.
the mousePressEvent() works fine now :)


Thanks marcel

stefanadelbert
16th April 2010, 10:41
Please post your code. I'm having a problem with activating a particular QWidgetAction given a mouse event from a QPushButton in that QWidgetAction. You code could be very useful to me.

Thanks