[Interest] mouse events: how to handle left and right button pressed?

Joseph Crowell joseph.w.crowell at gmail.com
Mon Mar 5 03:50:22 CET 2012


On 3/4/2012 12:46 PM, Philipp Menke wrote:
> why dont you use:
>
> if (event->buttons() == (Qt::LeftButton | Qt::RightButton) )
> {
>       std::cerr<<  "both"<<  std::endl;
> }
>    else if ( event->buttons() == Qt::LeftButton)
> {
>       std::cerr<<  "left"<<  std::endl;
> }
> else if ( event->buttons() == Qt::RightButton)
> {
>       std::cerr<<  "right"<<  std::endl;
> }
> else
> {
>       std::cerr<<  "other"<<  std::endl;
> }
>
> I know some people who think switch is ugly or unsafe because you might
> forget a break, they use this style every time...
>
>
>
> Philipp
>
> Am 04.03.2012 16:59, schrieb Rui Maciel:
>> I'm trying to handle mouse events which include the case where multiple
>> mouse buttons are pressed. The reference page on QMouseButton[1] states
>> that "[t]he button state is a combination of Qt::LeftButton
>> <http://doc.qt.nokia.com/4.7/qt.html#MouseButton-enum>, Qt::RightButton
>> <http://doc.qt.nokia.com/4.7/qt.html#MouseButton-enum>, Qt::MidButton
>> <http://doc.qt.nokia.com/4.7/qt.html#MouseButton-enum>   using the OR
>> operator." From this, it would be expected that an event where multiple
>> buttons were pressed would be handled in a way similar to the following
>> code:
>>
>>
>> <code>
>> switch(event->buttons() )
>> {
>> case Qt::LeftButton:
>> std::cerr<<   "left"<<   std::endl;
>> break;
>>
>> case Qt::RightButton:
>> std::cerr<<   "right"<<   std::endl;
>> break;
>>
>> case (Qt::LeftButton | Qt::RightButton):
>> std::cerr<<   "both"<<   std::endl;
>> break;
>>
>> default:
>> std::cerr<<   "other"<<   std::endl;
>> break;
>> }
>> </code>
>>
>> Yet, when I try to compile this code, the following error message is
>> displayed:
>>
>> <message>
>> InputState.c++:22:41: error: temporary of non-literal type
>> ‘QFlags<Qt::MouseButton>’ in a constant expression
>> </message>
>>
>>
>> Does anyone know if it's possible to handle mouse events with multiple
>> buttons pressed through a switch() statement? If it isn't possible then
>> what alternatives are available?
>>
>>
>> Thanks in advance,
>> Rui Maciel
>>
>> [1] http://doc.qt.nokia.com/4.7/qmouseevent.html#buttons
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
Also, I think your error is related to a missing include. Are you doing 
#include <QMouseevent>?



More information about the Interest mailing list