I started working with QT and want to program a little game named Pong ( 2 player and a ball). The left player should be moved with the keys 'W' and 'S', the right player with the arrow keys up and down. I have a class Game to handle my animation. There I have an eventfilter to handle the keyPress
bool Game::eventFilter(QObject *target, QEvent *e)
{
Q_UNUSED(target);
bool handled = false;
if(e->type() == QEvent::KeyPress)
{
QKeyEvent *keyEvent = (QKeyEvent *)e;
if(keyEvent->key() == Qt::Key_W)
{
leftPlayerDir = ((leftPlayerDir == 0) ? 5 : leftPlayerDir);
handled = true;
}
else if(keyEvent->key() == Qt::Key_S)
{
leftPlayerDir = ((leftPlayerDir == 0) ? -5 : leftPlayerDir);
handled = true;
}
else if(keyEvent->key() == Qt::Key_Up)
{
rightPlayerDir = ((rightPlayerDir == 0) ? 5 : rightPlayerDir);
handled = true;
}
else if(keyEvent->key() == Qt::Key_Down)
{
rightPlayerDir = ((rightPlayerDir == 0) ? -5 : rightPlayerDir);
handled = true;
}
}
return handled;
}
and in my class Pong, where I set up the Mainwindow and handle the buttons, I have a line to install the eventfilter
ui->animation->installEventFilter(game);
If I start my program the animation is working, but the players are not moving on keyPress and I don't know why. Some ideas?
Related
There is a bug in Qt in which multiple touch screens causes Qt to be in an inconsistent state. More about the issue here.
As a short-term patch, I'd to use event filters (which Qt provides for xcb events) to prevent multiple devices from being processed by Qt at a time.
The steps would be as followed.
A sequence of events for input is beginning (mouse button down, touch press, etc).
Block all other events for devices that don't belong to the current device being used.
When the sequence of events is completed, Resume events for all devices, starting back at step 1.
Effectively, I want to gate the events, so that only one device can be used at a time. I'm hoping this get's around Qt's bug.
First, I am trying to just filter events for a hard-coded device to see if this even gets around Qt's bug, but it doesn't.
class DuplicateHardwareEventFilter : public QAbstractNativeEventFilter
{
public:
DuplicateHardwareEventFilter() {}
bool nativeEventFilter(const QByteArray &eventType, void *message, long *) override
{
if (eventType == "xcb_generic_event_t") {
xcb_generic_event_t* ev = static_cast<xcb_generic_event_t *>(message);
uint responseType = ev->response_type & ~0x80;
if(responseType == XCB_GE_GENERIC) {
xcb_ge_event_t* gev = reinterpret_cast<xcb_ge_event_t*>(ev);
// assume input event
xcb_input_button_press_event_t* xiEvent = reinterpret_cast<xcb_input_button_press_event_t*>(ev);
if(xiEvent->event_type == XCB_INPUT_DEVICE_CHANGED) {
auto inputChangedEvent = reinterpret_cast<xcb_input_device_changed_event_t *>(gev);
if(inputChangedEvent->sourceid == 11) {
return true;
}
qDebug("xcb device changed: %d source: %d", xiEvent->deviceid, inputChangedEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_MOTION) {
auto inputMotionEvent = reinterpret_cast<xcb_input_motion_event_t*>(gev);
if(inputMotionEvent->sourceid == 11) {
return true;
}
qDebug("xcb motion: %d source: %d", inputMotionEvent->deviceid, inputMotionEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_ENTER) {
auto inputEnterEvent = reinterpret_cast<xcb_input_enter_event_t*>(gev);
if(inputEnterEvent->sourceid == 11) {
return true;
}
qDebug("xcb enter: %d source: %d", inputEnterEvent->deviceid, inputEnterEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_LEAVE) {
auto inputLeaveEvent = reinterpret_cast<xcb_input_leave_event_t*>(gev);
qDebug("xcb leave: %d source: %d", inputLeaveEvent->deviceid, inputLeaveEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_BUTTON_PRESS) {
auto buttonPressEvent = reinterpret_cast<xcb_input_button_press_event_t*>(gev);
qDebug("xcb buttonPress: %d source: %d", buttonPressEvent->deviceid, buttonPressEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_BUTTON_RELEASE) {
auto buttonReleaseEvent = reinterpret_cast<xcb_input_button_release_event_t*>(gev);
qDebug("xcb buttonRelease: %d source: %d", buttonReleaseEvent->deviceid, buttonReleaseEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_BEGIN) {
auto touchBeginEvent = reinterpret_cast<xcb_input_touch_begin_event_t*>(gev);
if(touchBeginEvent->sourceid == 11) {
return true;
}
qDebug("xcb touchBegin: %d source: %d", touchBeginEvent->deviceid, touchBeginEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_UPDATE) {
auto touchUpdateEvent = reinterpret_cast<xcb_input_touch_update_event_t*>(gev);
if(touchUpdateEvent->sourceid == 11) {
return true;
}
qDebug("xcb touchUpdate: %d source: %d", touchUpdateEvent->deviceid, touchUpdateEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_TOUCH_END) {
auto touchEndEvent = reinterpret_cast<xcb_input_touch_end_event_t*>(gev);
if(touchEndEvent->sourceid == 11) {
return true;
}
qDebug("touchEnd: %d source: %d", touchEndEvent->deviceid, touchEndEvent->sourceid);
return false;
}
if(xiEvent->event_type == XCB_INPUT_PROPERTY) {
auto propertyEvent = reinterpret_cast<xcb_input_property_event_t*>(gev);
qDebug("property: %d", propertyEvent->deviceid);
return false;
}
return false;
}
}
return false;
}
};
Qt still get's into it's quirky state.
How do I block a device completely, with filtering of the events of xcb_wait_for_event?
I do not grasp exactly what your issue is here, as I do not grasp what the inconsistent state does, but here is definitely a possible avenue if you get desperate.
For wayland, I wanted to create an API that would interface with my touchscreen, something like autohotkey or xdotools. Using the linux Kernel's uinput, I achieved remarkable success, interfacing with the device, and having it issue commands, and really vouch for how easy it was to get working. Just take a look here:
https://www.kernel.org/doc/html/v4.12/input/uinput.html
Definitely worth building an API, and perhaps even a virtual device driver.
TabletEvents comes as mouse events.
Actual for MAC OS Qt 4.8.0 - 4.8.5.
Works fine in Qt 4.7.3 on any OS and Qt 4.8.0 on Windows and Linux.
I have two instances of QGraphcisScene and two instances of QGraphicsView.
The same types, but one view have a parent, and the another - doesn't (also it's transparent, used for drawing something over desktop).
I'm using tablet (wacom pen and touch) for painting. I handle QTabletEvents and it works only for QGrahicsView instance that doesn't have parent (means parent==0).
On the view with parent (
QMainWindow->centralWidget->ControlContainerWidget->QStackedLayout->QGraphicsView
) tablet events doesn't comes. They comes to QApplication::eventFilter fine, but doesn't comes to view. They comes to QMainWindow as mouseEvents.
If i set parent to 0, tablet events delivers fine.
The 1st receiver of tablet event is QMainWindow.
I see that inside qt_mac_handleTabletEvent:
QWidget *qwidget = [theView qt_qwidget];
QWidget *widgetToGetMouse = qwidget;
And then:
`qt_sendSpontaneousEvent(widgetToGetMouse, &qtabletEvent);`
qtabletEvent - is not accepted event created just before calling sendSpontaneousEvent.
Then inside QApplication::notify():
QWidget *w = static_cast<QWidget *>(receiver);
QTabletEvent *tablet = static_cast<QTabletEvent*>(e);
QPoint relpos = tablet->pos();
bool eventAccepted = tablet->isAccepted();
while (w) {
QTabletEvent te(tablet->type(), relpos, tablet->globalPos(),
tablet->hiResGlobalPos(), tablet->device(), tablet->pointerType(),
tablet->pressure(), tablet->xTilt(), tablet->yTilt(),
tablet->tangentialPressure(), tablet->rotation(), tablet->z(),
tablet->modifiers(), tablet->uniqueId());
te.spont = e->spontaneous();
res = d->notify_helper(w, w == receiver ? tablet : &te);
eventAccepted = ((w == receiver) ? tablet : &te)->isAccepted();
e->spont = false;
if ((res && eventAccepted)
|| w->isWindow()
|| w->testAttribute(Qt::WA_NoMousePropagation))
break;
relpos += w->pos();
w = w->parentWidget();
}
tablet->setAccepted(eventAccepted);
As we can see:
res = d->notify_helper(w, w == receiver ? tablet : &te);
It calls event processing by filters, layouts and then - QMainWindow::tabletEvent. Default implementation is event->ignore().
Since QMainWindow have no Parent, it is all.
So tablet event doesn't comes to QMainWindow childs.
Then seems it is QWidget *qwidget = [theView qt_qwidget]; works wrong.
Unfortunately, i can't debug it...
Please give me some hints... i'm stucked...
I spent more time on comparison Qt 4.8.0 and 4.7.3 and now i see that it is the problem in internal qt event dispatcher. It sends event to NSWindow (QMainWindow) instead of NSView (QGraphicsView).
I didn't found where is the problem, but i found that QMainWindow returns false from ::event() method.
So i reimplemented that method and parsed tablet event there:
bool UBMainWindow::event(QEvent *event)
{
bool bRes = QMainWindow::event(event);
if (NULL != UBApplication::boardController)
{
UBBoardView *controlV = UBApplication::boardController->controlView();
if (controlV && controlV->isVisible())
{
switch (event->type())
{
case QEvent::TabletEnterProximity:
case QEvent::TabletLeaveProximity:
case QEvent::TabletMove:
case QEvent::TabletPress:
case QEvent::TabletRelease:
{
return controlV->directTabletEvent(event);
}
}
}
}
return bRes;
}
The problem is: i need to use tablet for any controls in application, so i need to determine when QGraphicsView is under mouse cursor:
bool UBBoardView::directTabletEvent(QEvent *event)
{
QTabletEvent *tEvent = static_cast<QTabletEvent *>(event);
tEvent = new QTabletEvent(tEvent->type()
, mapFromGlobal(tEvent->pos())
, tEvent->globalPos()
, tEvent->hiResGlobalPos()
, tEvent->device()
, tEvent->pointerType()
, tEvent->pressure()
, tEvent->xTilt()
, tEvent->yTilt()
, tEvent->tangentialPressure()
, tEvent->rotation()
, tEvent->z()
, tEvent->modifiers()
, tEvent->uniqueId());
if (geometry().contains(tEvent->pos()))
{
if (NULL == widgetForTabletEvent(this->parentWidget(), tEvent->pos()))
{
tabletEvent(tEvent);
return true;
}
}
return false;
}
Also i need to stop handle tablet events for QGraphicsView childs.
QWidget *UBBoardView::widgetForTabletEvent(QWidget *w, const QPoint &pos)
{
Q_ASSERT(w);
UBBoardView *board = qobject_cast<UBBoardView *>(w);
QWidget *childAtPos = NULL;
QList<QObject *> childs = w->children();
foreach(QObject *child, childs)
{
QWidget *childWidget = qobject_cast<QWidget *>(child);
if (childWidget)
{
if (childWidget->isVisible() && childWidget->geometry().contains(pos))
{
QWidget *lastChild = widgetForTabletEvent(childWidget, pos);
if (board && board->viewport() == lastChild)
continue;
if (NULL != lastChild)
childAtPos = lastChild;
else
childAtPos = childWidget;
break;
}
else
childAtPos = NULL;
}
}
return childAtPos;
}
Maybe when i will have more time - i will investigate qt more deeply and fix that problem at all.
I have a button in my game. and I want to make it to change to other button image when mouse hover on it and change back when the mouse not in the button.
the problem is when the mouse went out from the button rectangle area, it not change back to the first image
my code like this :
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
MouseState mouseState;
mouseDiBack = false;
mouseState = Mouse.GetState();
if (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(backButtonRectangle))
{
backButton = backButtonHilite;
}
if ((mouseState.LeftButton == ButtonState.Pressed) &&
(new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(backButtonRectangle)))
{
mouseDiBack = true;
}
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Draw(ScoreBG, ScoreBGRectangle, Color.White);
spriteBatch.Draw(backButton, backButtonRectangle, Color.White);
base.Draw(gameTime);
}
}
}
any idea how I to do it...?
Fairly simple solution, you didn't set the image back on the case where the mouse is not hovering.
if (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(backButtonRectangle))
{
backButton = backButtonHilite;
}
else
{
backButton = originalImage; //whatever your Texture2D object may be called
}
Don't expect the machine to know that you want to switch back! Machines are stupid! ..Ok, it's actually because you overwrote the value of the variable and didn't reset it.
You are not setting your backButton back to what it used to be when the mouse moves out of the scope area. Take a look at the code below, and notice the added ELSE statement in your Update function.
defaultBackButton = backButton; //Save the default back button somewhere outside your update function
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
MouseState mouseState;
mouseDiBack = false;
mouseState = Mouse.GetState();
if (new Rectangle(mouseState.X, mouseState.Y, 1,1).Intersects(backButtonRectangle))
{
backButton = backButtonHilite;
}
else
{
backButton = defaultBackButton;
}
if ((mouseState.LeftButton == ButtonState.Pressed) && (new Rectangle(mouseState.X, mouseState.Y, 1, 1).Intersects(backButtonRectangle)))
{
mouseDiBack = true;
}
}
As mentioned by Jon you need to set your original texture back when the mouse has left the rectangle.
bool mouseOverBackButton =
mouseX >= buttonRectangle.Left && mouseX <= buttonRectangle.Right &&
mouseY >= buttonRectangle.Top && mouseY <= buttonRectangle.Bottom;
backgroundTexture = mouseOverBackButton ? mouseOverTexture: mouseAwayTexture;
mouseDiBack = mouseState.LeftButton == ButtonState.Pressed && mouseOverBackButton;
I'm writing an application were i use my own shortcut. It looks like this:
myShortcut= new QShortcut(Qt::SHIFT + Qt::Key_B,this);
connect(myShortcut, SIGNAL(activated()), this, SLOT(setCameraBack()));
I defined it in the constructor of main widget and it works fine until i click one of the spinbox buttons which are also located on the main widget. After that my shortcut stop working and it doesn't work until i click push button or check box. When i do that everything is fine again. I'd like to add that after i click spinbox it seems to be "active" (because the cursor is still "blinking" on it) until i click one of the other buttons. Do you have any idea what is wrong? Is it some kind process or event problem? Thanks for all answers
~Marwroc
A shortcut is "listened for" by Qt's
event loop when the shortcut's parent
widget is receiving events.
When the QSpinBox has keyboard focus, then the QShortcut object's parent is no longer receiving events. Therefore, the shortcut does not work until keyboard focus is removed form the QSpinBox. You can change this behavior by passing Qt::WidgetWithChildrenShortcut or Qt::ApplicationShortcut to the QShortcut::setContext method of your QShortcut.
Before a shortcut is activated, the focus widget is given a ShortcutOverride event. If the event is accepted, the key event is passed along to the widget and the shortcut is not activated.
Source: https://wiki.qt.io/ShortcutOverride
Looking at Qt source
QAbstractSpinBox::event(QEvent *event)
{
Q_D(QAbstractSpinBox);
switch (event->type()) {
...
case QEvent::ShortcutOverride:
if (d->edit->event(event))
return true;
break;
...
}
return QWidget::event(event);
}
QAbstractSpinBox is allowing the internal edit to accept the event. QLineEdit defers to QLineControl. From qt/src/gui/widgets/qlinecontrol.cpp
case QEvent::ShortcutOverride:{
if (isReadOnly())
return false;
QKeyEvent* ke = static_cast<QKeyEvent*>(ev);
if (ke == QKeySequence::Copy
|| ke == QKeySequence::Paste
|| ke == QKeySequence::Cut
|| ke == QKeySequence::Redo
|| ke == QKeySequence::Undo
|| ke == QKeySequence::MoveToNextWord
|| ke == QKeySequence::MoveToPreviousWord
|| ke == QKeySequence::MoveToStartOfDocument
|| ke == QKeySequence::MoveToEndOfDocument
|| ke == QKeySequence::SelectNextWord
|| ke == QKeySequence::SelectPreviousWord
|| ke == QKeySequence::SelectStartOfLine
|| ke == QKeySequence::SelectEndOfLine
|| ke == QKeySequence::SelectStartOfBlock
|| ke == QKeySequence::SelectEndOfBlock
|| ke == QKeySequence::SelectStartOfDocument
|| ke == QKeySequence::SelectAll
|| ke == QKeySequence::SelectEndOfDocument) {
ke->accept();
} else if (ke->modifiers() == Qt::NoModifier || ke->modifiers() == Qt::ShiftModifier
|| ke->modifiers() == Qt::KeypadModifier) {
if (ke->key() < Qt::Key_Escape) {
ke->accept();
} else {
switch (ke->key()) {
case Qt::Key_Delete:
case Qt::Key_Home:
case Qt::Key_End:
case Qt::Key_Backspace:
case Qt::Key_Left:
case Qt::Key_Right:
ke->accept();
default:
break;
}
}
}
}
This code accepts most keys if the control key is not also pressed.
So the easiest solution is to change the shortcut to include the control modifier.
Alternatively, you can subclass the spin box and override the event function
bool MySpinBox::event(QEvent *event)
{
if( event->type() == QEvent::ShortcutOverride && !isReadOnly() )
{
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
// Ignore 'B' shortcuts
if( keyEvent->key() == Qt::Key_B )
{
Q_ASSERT( !event->isAccepted() );
return true;
}
return QSpinBox::event(event);
}
Have you tried MySpinBox -> setFocusPolicy (Qt::NoFocus) ?
I'm using a combination of SDL and OpenGL in a sort of crash course project to teach myself how this all works. I'm really only interested in OpenGL as a way to use acceleration in 2D games so I just need this to work in a 2D plane.
I have been having a lot of problems today with my current issue, I would like an object to point towards the mouse while the mouse button is clicked and then of course stay pointing in that direction after the mouse is lifted.
void Square::handle_input() {
//If a key was pressed
if( event.type == SDL_KEYDOWN ) {
//Adjust the velocity
switch( event.key.keysym.sym ) {
case SDLK_UP: upUp = false; yVel = -1; break;
case SDLK_DOWN: downUp = false; yVel = 1; break;
case SDLK_LEFT: leftUp = false; xVel = -1; break;
case SDLK_RIGHT: rightUp = false; xVel = 1; break;
case SDLK_w: wUp = false; sAng = 1; break;
case SDLK_s: sUp = false; sAng = -1; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP ) {
//Adjust the velocity
switch( event.key.keysym.sym ) {
case SDLK_UP: upUp = true; yVel = 0; break;
case SDLK_DOWN: downUp = true; yVel = 0; break;
case SDLK_LEFT: leftUp = true; xVel = 0; break;
case SDLK_RIGHT: rightUp = true; xVel = 0; break;
case SDLK_w: wUp = true; sAng = 0; break;
case SDLK_s: sUp = true; sAng = 0; break;
}
}
//If a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN ) {
switch ( event.type ) {
case SDL_MOUSEBUTTONDOWN: mouseUp = false; mousex == event.button.x; mousey == event.button.y; break;
case SDL_MOUSEBUTTONUP: mouseUp = true; break;
}
}
}
And then this is called at the end of my Object::Move call which also handles x and y translation
if (!mouseUp) {
xVect = mousex - x;
yVect = mousey - y;
radAng = atan2 ( mousey - y, mousex - x );
sAng = radAng * 180 / 3.1415926l;
}
Right now when I click the object turns and faces down to the bottom left but then no longer changes direction. I'd really appreciate any help I could get here. I'm guessing there might be an issue here with state versus polled events but from all the tutorials that I've been through I was pretty sure I had fixed that. I've just hit a wall and I need some advice!
I'm assuming that you want the object to keep pointing at the mouse's position while the mouse button is held. To fix this, you will have to update the mouse position every time the mouse is moved.
Add some code similar to this:
if( event.type == SDL_MOUSEMOVE ) {
// update the mouse position here using event.???.x / y
}
Note that I haven't got an SDL reference here, so I can't give you the exact members, but that should help.
NB: I would also guess that there would be problems with your button handling code. You have an if statement that checks for one value of event.type, but then inside its body you have a switch statement with two values. Only one of these values will ever be executed - you should probably think about just using 2 separate if statements for the button down / button up events.