You are not logged in.

1

Saturday, April 7th 2012, 4:16pm

Mouse buttons switch in mousePressEvent of QgraphicsPixmapItem

Is there any easy way to switch the role of mousebuttons at QGraphicsPixmapItem mousePressEvent(QGraphicsSceneMouseEvent*)? The QGraphicsSceneMouseEvent doesnt have constructor avaible so there is no way to construct one easily. I have my own class GraphPixmapItemCustom that is derived from QGraphicsPixmapItem.

The hard ways I have though off are:
1. Implement my own class derived from QGraphicsScebeMouseEvent that will have the ability of changing buttons, and do it in my custom mousePressEvent() of my own custom class
2. Search for code of QGraphicsPixmapItem and copy-paste most of code changing the keys

but those are, in my opinion, not elegant solutions, nevertheless can be the only ones. Though i hope they're not. Maybe someone with more XP could find more elegant, easier way?

2

Saturday, April 7th 2012, 7:31pm

why do you need to switch the buttons?

If you do need to switch buttons and pass event on to non-custom QWidgets, then you will need to do 1) and pass that custom event into the QWidgets. If you don't need to pass the event on to anywhere, then you dont need a custom event.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Sunday, April 8th 2012, 10:30pm

I just want to switch default action for mouse buttons for class derived from QGraphicsPixmapItem. I assume from your answer that my point #1 is the better way.

Why would I like to change event that I don't do anything with?

4

Sunday, April 8th 2012, 11:36pm

I just want to switch default action for mouse buttons for class derived from QGraphicsPixmapItem. I assume from your answer that my point #1 is the better way.

Yes, of course.

Why would I like to change event that I don't do anything with?

I don't know what you mean.

My previous post is explaining you have different options depending on which of these your event handler looks like:

Source code

1
2
3
4
5
6
7
8
9
10
11
void SomeClass::handler(QEvent* event)
{
  // do stuff
  event->accept();
}

void SomeClass::handler(QEvent* event)
{
  // do stuff
  Base::handler(event); // SomeClass is derived from Base.
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Tuesday, April 10th 2012, 4:52pm

I don't know what you mean.

Seems we've lost each other's thoughts. In topic I said I just wanna switch default actions for buttons - I don't want anything else (so it seemed to me, it was obvious that it meant the 2nd way u stated in ur last post, where "do stuff" would be switching mouse buttons). But You are right, that i should not give any space for free interpretation.

Anyway, OK I'll try to do it the way you've suggested.

6

Tuesday, April 10th 2012, 5:43pm

argh, ok. Lets get this absolutely clear! :D

here are options (pseudo code):

Source code

1
2
3
4
5
6
7
8
9
10
11
// use this very simple way if you dont need to pass the event around
// `event` here is qt event - no need to subclass
class::eventhandler(event)
{
  if event is LMB
  // do stuff, pretending RMB
  elif event is RMB 
  // do stuff, pretendending LMB

  // throw away event, do not pass to base class
}


alternative:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// if you DO need to pass the event around
// `eventSubclass` here is your own subclass, inheriting the Qt event class (don't even know if this is well supported) 
class::eventhandler(eventSubclass)
{
  if eventSubclass is LMB
  // do stuff for LMB - eventSubclass switches the buttons
  elif eventSubclass is RMB 
  // do stuff for RMB

  //if you need to pass event deeper, then it obviously has 
  //to be wrapped if you want to keep pretending the buttons are switched
    Base::eventhandler(eventSubclass); 

  // A bizarre alternative is where you want to pass the event with 'normal' buttons
  // into event handler, in which case use above example - no need for event wrapper here.
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Tuesday, April 10th 2012, 6:16pm

I wanna do something like this:

Source code

1
2
3
4
5
6
7
8
9
10
class::eventHandler(event)
if (event->button == LMB)
{
  event->button = RMB
}
else if (event->button == RMB)
{
  event->button = LMB
}
BaseClass::eventHandler(event)

8

Tuesday, April 10th 2012, 6:53pm

then your idea 1 from OP is the way to go, if it will work.
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.