Unexpected behaviour with QMouseMoveEvent and QKeyEvent modifiers - qt

I am encountering unexpected behaviour in my code.
I have a QGraphicsView containing a QGraphicsScene. Now I want to detect the mouse wheel for zooming the view and mouse moving for moving items in the scene, the latter only while controll is pressed. Now I have two Problems:
MouseMoveEvent is called even when the mouse is not moved but only the mouse wheel.
Moving with and without controll pressed works fine but when I stop moving while controll is pressed and continue using the mouse wheel not only the mousemoveevent is called but also the controllmodifier is still active. What is the problem?
main.cpp
#include "ppi.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PPI w;
w.show();
return a.exec();
}
ppi.h
#ifndef PPI_H
#define PPI_H
#include <QtGui/QMainWindow>
#include <QGraphicsView>
#include <QDebug>
#include <QWheelEvent>
#include "ui_ppi.h"
#include "ppiView.h"
#include "ppiscene.h"
class PPI : public QMainWindow
{
Q_OBJECT
public:
PPI(QWidget *parent = 0, Qt::WFlags flags = 0);
~PPI();
int i;
private:
Ui::ppiClass ui;
PPIScene* ppiScene;
protected slots:
void onZoom(QWheelEvent *event);
void onMouseMoved(QGraphicsSceneMouseEvent *event);
};
#endif // PPI_H
ppi.cpp
#include "ppi.h"
PPI::PPI(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ppiScene = new PPIScene(this);
connect(ppiScene, SIGNAL(mouseMoved(QGraphicsSceneMouseEvent*)), this, SLOT(onMouseMoved(QGraphicsSceneMouseEvent*)));
connect(ui.gVPPI, SIGNAL(zoom(QWheelEvent*)), this, SLOT(onZoom(QWheelEvent*)));
ppiScene->setSceneRect(0,0,1024,1024);
ui.gVPPI->setScene(ppiScene);
ui.gVPPI->setMouseTracking(true);
i = 0;
}
PPI::~PPI()
{
}
void PPI::onZoom(QWheelEvent *event)
{
if(event->delta() > 0)
ui.gVPPI->scale(1.01, 1.01);
else
ui.gVPPI->scale(1/1.01, 1/1.01);
}
void PPI::onMouseMoved(QGraphicsSceneMouseEvent *event)
{
i++;
qDebug() << "slot" << i << event->modifiers();
if(event->modifiers() & Qt::ControlModifier)
{
qDebug() << "ctrl pressed";
}
}
ppiview.h
#ifndef PPIVIEW_H
#define PPIVIEW_H
#include <QGraphicsView>
#include <QMouseEvent>
class PPIView : public QGraphicsView
{
Q_OBJECT
public:
PPIView(QWidget * parent = 0);
~PPIView();
private:
void wheelEvent(QWheelEvent *event);
signals:
void zoom(QWheelEvent *event);
};
#endif // PPIVIEW_H
ppiview.cpp
#include "ppiview.h"
PPIView::PPIView(QWidget * parent)
: QGraphicsView(parent)
{
}
PPIView::~PPIView()
{
}
void PPIView::wheelEvent(QWheelEvent *event)
{
emit zoom(event);
}
ppiscene.h
#ifndef PPISCENE_H
#define PPISCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QDebug>
class PPIScene : public QGraphicsScene
{
Q_OBJECT
public:
PPIScene(QObject *parent);
~PPIScene();
int i;
private:
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
signals:
void mouseMoved(QGraphicsSceneMouseEvent *event);
};
#endif // PPISCENE_H
ppiscene.cpp
#include "ppiscene.h"
PPIScene::PPIScene(QObject *parent)
: QGraphicsScene(parent)
{
i = 0;
}
PPIScene::~PPIScene()
{
}
void PPIScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
i++;
qDebug() << "signal" << i << event->modifiers();
emit mouseMoved(event);
}

It is really strange. It looks like the Qt::KeyboardModifiers status, which QGraphicsSceneMouseEvent::modifiers returns, is only updated, when the mouse is physically moved. What is even more strange, is that in your code QGraphicsSceneMouseEvents of type QGraphicsSceneMouseMove are sent even when the mouse is not moved at all, but only the wheel is turned. Maybe the relative movement due to your scaling counts as movement, but not as movement, which updates the modifiers.
I was able to reproduce your problem: The status of the modifiers don't change unless the mouse is physically moved.
Fortunately there is an easy workaround. In
void PPI::onMouseMoved(QGraphicsSceneMouseEvent *event)
{
i++;
qDebug() << "slot" << i << event->modifiers();
if(event->modifiers() & Qt::ControlModifier)
{
qDebug() << "ctrl pressed";
}
}
replace:
if(event->modifiers() & Qt::ControlModifier)
with:
if(QApplication::queryKeyboardModifiers() & Qt::ControlModifier)
QApplication::queryKeyboardModifiers() is updated immediately when you press or release the control key.

Related

Qt Signal Slot connection issue

Trying to create 2 objects (clients) living in different threads and communicating with 1 object(server) living on the main thread.
However I'm not seeing any activity on the server object.
The main goal of this code is to see how the signal slot will behave when 2 signals coming from 2 different threads are communicating with a single object on the main thread.
objects.cpp:
#include "objects.h"
Objects::Objects(QObject* parent): QObject (parent)
{
}
void Objects::printData(const QString& data)
{
qDebug() << data;
}
void Objects::process(const QString& data)
{
emit sendData(data+":processed");
}
Q_NORETURN void Objects::run()
{
while (true) {
//qDebug() << "hit" << m_rate;
emit sendData("test"+QString::number(m_rate));
QThread::sleep(m_rate);
}
}
objects.h:
#ifndef OBJECTS_H
#define OBJECTS_H
#include <QObject>
#include <QDebug>
#include <QThread>
class Objects: public QObject
{
Q_OBJECT
public:
explicit Objects(QObject* parent = Q_NULLPTR);
signals:
void sendData(const QString&);
public slots:
void printData(const QString&);
void process(const QString&);
void run();
public:
ulong m_rate;
};
#endif // OBJECTS_H
main.cpp:
#include <QCoreApplication>
#include "objects.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Objects ser;
Objects cl1;
Objects cl2;
cl1.m_rate = 1;
cl2.m_rate = 2;
QThread cl1_t;
QThread cl2_t;
QObject::connect(&cl1_t, &QThread::started, &cl1, &Objects::run);
QObject::connect(&cl2_t, &QThread::started, &cl2, &Objects::run);
QObject::connect(&cl1, &Objects::sendData, &ser, &Objects::process);
QObject::connect(&cl2, &Objects::sendData, &ser, &Objects::process);
QObject::connect(&ser, &Objects::sendData, &cl1, &Objects::printData);
QObject::connect(&ser, &Objects::sendData, &cl2, &Objects::printData);
cl1.moveToThread(&cl1_t);
cl2.moveToThread(&cl2_t);
cl1_t.start();
cl2_t.start();
return a.exec();
}
Looks like your void run() function-member blocks Qt internal event queue. Just add QCoreApplication::processEvents(); in to your void run() and it will solve your problem.
void Objects::run()
{
while (true) {
qDebug() << "hit" << m_rate;
emit sendData("test" + QString::number(m_rate));
QThread::sleep(m_rate);
QCoreApplication::processEvents();
}
}
UPD:
I will suggest you to read THIS wiki article for more detailed explanation on your problem.

how to get drop events of object which is placed in graphic scene?

i have graphic view(QGraphics view) setted scene(QGraphic scene) i am dropping objects on scene its working fine, i have to assign parameter for dropped object by dragging parameter from parameter list .i have implemented drag drop events of object.but when i am dragging parameter from param list non- acceptance symbol on object.how to assign param to object by dropping ? Any other suggestions and examples are welcome where i can get ideas to implementation.
image of gui
speedometer.cpp
#include <QMimeData>
SpeedoMeter::SpeedoMeter( QWidget *parent ):
QwtDial( parent ),
d_label( "km/h" )
{
setAcceptDrops(true);
}
void SpeedoMeter::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat(paramlistMimeType()))
{
qDebug()<<"dragenter event in speedo" ;
event->accept();
}
}
void SpeedoMeter::dragMoveEvent(QDragMoveEvent *event)
{
if (event->mimeData()->hasFormat(paramlistMimeType()))
{
qDebug()<<"dragmove event in speedo" ;
event->acceptProposedAction();
}
}
void SpeedoMeter::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasFormat(paramlistMimeType()))
{
qDebug()<<"dragmove event in speedo" ;
event->accept();
}
}
The following example shows how to implement the logic to accept the drag-and-drop:
speedometer.h
#ifndef SPEEDOMETER_H
#define SPEEDOMETER_H
#include <qwt_dial.h>
class SpeedoMeter : public QwtDial
{
public:
SpeedoMeter(QWidget *parent=nullptr);
protected:
void dragEnterEvent(QDragEnterEvent *event);
void dropEvent(QDropEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void paintEvent(QPaintEvent *event);
private:
QString d_label;
};
#endif // SPEEDOMETER_H
speedometer.cpp
#include "speedometer.h"
#include <qwt_dial_needle.h>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QPainter>
SpeedoMeter::SpeedoMeter(QWidget *parent):
QwtDial(parent),
d_label( "km/h" )
{
setAcceptDrops(true);
QwtDialSimpleNeedle *nd = new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Arrow, Qt::white, Qt::red);
setNeedle(nd);
setValue(80);
}
void SpeedoMeter::dragEnterEvent(QDragEnterEvent *event)
{
if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
event->acceptProposedAction();
}
void SpeedoMeter::dropEvent(QDropEvent *event)
{
const QMimeData *mimedata = event->mimeData();
if(mimedata->hasFormat("application/x-qabstractitemmodeldatalist")){
QString text;
// https://stackoverflow.com/questions/1723989/how-to-decode-application-x-qabstractitemmodeldatalist-in-qt-for-drag-and-drop
QByteArray encoded = mimedata->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&encoded, QIODevice::ReadOnly);
while (!stream.atEnd()) {
int row, col;
QMap<int, QVariant> roleDataMap;
stream >> row >> col >> roleDataMap;
if(roleDataMap.contains(Qt::DisplayRole)){
text = roleDataMap[Qt::DisplayRole].toString();
break;
}
}
// your text
d_label = text;
update();
}
}
void SpeedoMeter::dragMoveEvent(QDragMoveEvent *event)
{
if(event->mimeData()->hasFormat("application/x-qabstractitemmodeldatalist"))
event->accept();
}
void SpeedoMeter::paintEvent(QPaintEvent *event)
{
// https://stackoverflow.com/questions/43904204/qwt-dial-show-unit
QwtDial::paintEvent(event);
QPainter painter(this);
painter.setPen(Qt::black);
QFont font;
font.setPointSize(11);
painter.setFont(font);
QString text = QString("%1 %2").arg(value()).arg(d_label);
QPoint c = rect().center();
QSize Size = painter.fontMetrics().size(Qt::TextSingleLine, text);
painter.drawText(QPointF(c.x() -Size.width()/2, c.y() + 2.5*Size.height()), text);
}
main.cpp
#include "speedometer.h"
#include <QApplication>
#include <QGraphicsView>
#include <QHBoxLayout>
#include <QListWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
QHBoxLayout *layout = new QHBoxLayout(&w);
QListWidget listWidget;
listWidget.setDragDropMode(QAbstractItemView::DragOnly);
listWidget.addItems({"km/h", "ft/s", "m/s", "miles/h"});
QGraphicsView view;
QGraphicsScene scene;
view.setScene(&scene);
SpeedoMeter speed;
scene.addWidget(&speed);
layout->addWidget(&listWidget);
layout->addWidget(&view);
w.show();
return a.exec();
}
In the following link you can find the complete example.

Connecting signals and slots in a QTextEdit subclass?

I derived a class from QTextEdit and use it as a "logbook". I equipped it with a slot to receive log-messages.
class CLogbook : public QTextEdit
{
Q_OBJECT;
public:
void log(QString msg) {append(msg)};
public slots:
void recvLogSignal(const QString message)
{
append("hallo");
std::cout << "signal received.\n";
log(message);
}
};
another class then emits a signal like this:
// in the header
signals:
void logMessage(const QString);
// in the implementation
emit logMessage("qt is cute");
std::cout << "if you can read this the logMessage was emitted\n";
and also i connect the signal to the slot
connect(tableeditor, SIGNAL(logMessage(const QString)), logbook, SLOT(recvLogSignal(const QString)));
However the message is never shown in the "logbook". What am i missing here?
SOLVED: The connect method was called after emitting the signal :-(
It is hard to see exactly what is wrong with your implementation without a full example. Sometimes signals or slots will fail if an object goes out of scope if it isn't initialized on the heap.
Another way that it could fail is if your QApplication hasn't reached the exec() call.
I haven't experimented with using const in signal and slot calls, and I haven't seen it in any examples before, so that could be causing the problem; but it seems to work fine in the example below.
Working Example With a Derived Class of QTextEdit
Here is a simple example I put together that does some basic logging with a push button and a line edit.
Here is the header:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
//#include <QTextEdit>
#include "clogbook.h"
#include <QLineEdit>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget() {}
public slots:
void on_pushButton();
void on_lineEditReturn();
private:
CLogBook * text_edit_log;
QLineEdit * line_edit;
};
#endif // WIDGET_H
Here is the source:
#include "widget.h"
#include <QBoxLayout>
#include <QPushButton>
#include <QTimer>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QBoxLayout * box_layout = new QBoxLayout(QBoxLayout::TopToBottom);
text_edit_log = new CLogBook;
line_edit = new QLineEdit("Type Here and press Enter.");
QPushButton * push_button = new QPushButton("Click to Add To Log");
text_edit_log->setText("My Log Book");
box_layout->addWidget(text_edit_log);
box_layout->addWidget(line_edit);
box_layout->addWidget(push_button);
this->setLayout(box_layout);
QObject::connect(push_button, SIGNAL(clicked()), this, SLOT(on_pushButton()));
QObject::connect(line_edit, SIGNAL(returnPressed()), this, SLOT(on_lineEditReturn()));
}
void Widget::on_pushButton()
{
// text_edit_log->append("Push Button Logging Test");
text_edit_log->recvLogSignal("Push button test.");
}
void Widget::on_lineEditReturn()
{
// text_edit_log->append(line_edit->text());
text_edit_log->recvLogSignal(QString("LineEdit: ") + line_edit->text() );
line_edit->clear();
}
And here is the main:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
And here is the CLogBook class:
#ifndef CLOGBOOK_H
#define CLOGBOOK_H
#include <QTextEdit>
#include <iostream>
class CLogBook : public QTextEdit
{
Q_OBJECT
public:
explicit CLogBook(QWidget *parent = 0) : QTextEdit(parent) { }
void log (QString msg) { append(msg); }
public slots:
void recvLogSignal(const QString message)
{
append("hallo");
std::cout << "signal received.\n" << std::endl;
log(message);
}
};
#endif // CLOGBOOK_H

Horde3D Particle System not rendering in Qt OpenGL

Horde3d come with 2 samples, compiled with GLFW. One of them, Knight, shows a particle emitter. I've ported the samples to Qt, with a thin layer I wrote that leave unchanged the applicative code (i.e. scene setup, rendering and events handling).
Indeed, the functionality is ok, except the particle emitter doesn't show. I can't see anything specific in GLFW initialization, and I tried some setting I found in Qt, without success. Horde3d takes care of OpenGL interface, and expose a higher level, clean C interface. Any clue?
EDIT : Most Rilevant Sources of qtKnight.pro
here (cleaned :) main.cpp
#include "glwidget.h"
#include <QApplication>
int main(int argc, char* argv[]) {
QApplication app(argc, argv);
GLWidget glw;
glw.show();
return app.exec();
}
here glWidget.h
#ifndef GL_WIDGET_H
#define GL_WIDGET_H
#include <QtOpenGL>
#include <QTimer>
#include <QKeyEvent>
#include <Horde3D.h>
#include <Horde3DUtils.h>
#include <sstream>
#include <app.h>
class GLWidget : public QGLWidget, Application {
Q_OBJECT
public:
GLWidget();
~GLWidget();
QSize minimumSizeHint() const { return sizeHint(); }
QSize sizeHint() const { return QSize(640, 480); }
protected:
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *e) { keyEvent(e, true); QGLWidget::keyPressEvent(e); }
void keyReleaseEvent(QKeyEvent *e) { keyEvent(e, false); QGLWidget::keyReleaseEvent(e); }
void keyEvent(QKeyEvent *, bool);
public slots:
void appLoop() { updateGL(); }
private:
QPoint lastPos;
QTimer evloop;
};
#endif
and here glWidget.cpp
#include "glwidget.h"
#include <stdexcept>
#include <QtDebug>
#include <QTextStream>
#include <QGLFormat>
GLWidget::GLWidget() :
QGLWidget(QGLFormat(QGL::AlphaChannel | QGL::SampleBuffers)),
Application("/home/carlo/horde3d/SDK_1.0.0_Beta5/Horde3D/Binaries/Content")
{
connect(&evloop, SIGNAL(timeout()), this, SLOT(appLoop()));
evloop.start(0);
}
GLWidget::~GLWidget()
{
h3dutDumpMessages();
h3dRelease();
}
void GLWidget::initializeGL()
{
if (!init())
throw std::runtime_error("Could not initialize renderer");
}
void GLWidget::paintGL()
{
keyStateHandler();
mainLoop(30);
}
void GLWidget::resizeGL(int width, int height)
{
Application::resize(width, height);
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
QPoint cPos = event->pos();
float dX = cPos.x() - lastPos.x(); //event->x() - lastPos.x();
float dY = cPos.y() - lastPos.y(); //event->y() - lastPos.y();
Application::mouseMoveEvent(dX, dY);
lastPos = cPos;
}
void GLWidget::mouseReleaseEvent(QMouseEvent * /* event */)
{
}
void GLWidget::keyEvent(QKeyEvent *k, bool on_off)
{
#define setK(X,Y) case Qt::X: setKeyState(Y, on_off); break;
#define setF(X) case Qt::Key_##X: setKeyState(X, on_off); break;
#define R(X, Y) (v >= #X[0] && v <= #Y[0])
int v = k->key();
switch (v) {
case Qt::Key_F1: if (on_off) showFullScreen(); break;
case Qt::Key_F2: if (on_off) showNormal(); break;
setF(F3)
setF(F6)
setF(F7)
setF(F8)
setK(Key_Space, SP)
default:
if (R(A, Z) || R(0, 9))
setKeyState(v, on_off);
}
}
Since you say it is only the particles that aren't shown, it could have to do with alpha blending. I also saw from your code that you don't specify a format when constructing the QGLWidget, in which case the default format is used which explicitly disables the alpha channel.
Though I don't know if this has any effect (shouldn't there always be an A in RGBA?), but maybe this really requests a pixel format where there is no storage for the A channel, in which case things like alpha blending (used for the transparent particles) won't work.
So just try to request it explicitly by using an appropriate format in the QGLWidget constructor:
GLWidget::GLWidget()
: QGLWidget(QGLFormat(QGL::AlphaChannel))
You actually were on the right track with your outcommented QGLFormat argument, but it hasn't anything to do with the HasOverlay option, which you don't really need, as Horde3d does it's own overlay rendering.

Qt - Moving Frameless window (slow repaint?)

Hy there,
I'm new to Qt and i tried to create a Frameless window, which is dragable. The Problem is, if i move the window.. there are are thousands of (it? sorry, don't know how to describe it in english) until i stop.
Is there a way to fix this, or am i thinking the wrong way?
MyWindow.h
#include <QMainWindow>
#include <QLabel>
#include <QPushButton>
#include <QMouseEvent>
class MyWindow : public QMainWindow {
Q_OBJECT
private:
QLabel *label_title,*label_quit;
QPushButton *bn_exit;
QPixmap *pixmap;
QPoint m_dragPosition;
public:
MyWindow(QMainWindow *parent = 0, Qt::WindowFlags flags = 0);
protected:
void paintEvent(QPaintEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
};
MyWindow.cpp
#include "MyWindow.h"
#include <QApplication>
#include <QPainter>
#include <QLabel>
MyWindow::MyWindow(QMainWindow *parent, Qt::WindowFlags flags) : QMainWindow(parent,flags) {
resize(1024,576);
setWindowTitle("Versuch1");
//Titel
label_title = new QLabel("irgendwas",this);
label_title->setGeometry(10,10,500,40);
label_title->setStyleSheet("font-family: Arial; letter-spacing: 4px;font-weight:bold; color : white;font-size: 30px");
//Exit-Button
bn_exit = new QPushButton("[Exit]",this);
bn_exit->setGeometry(975,10,40,20);
bn_exit->setStyleSheet("QPushButton {font-family: Verdana; font-size:15px; top:0px; border: none; background-color:#101010; color:white} QPushButton:hover {color: red }");
connect(bn_exit,SIGNAL(clicked()),qApp,SLOT(quit()));
}
void MyWindow::paintEvent(QPaintEvent *event) // Painter
{
QPainter painter(this);
painter.setPen(Qt::NoPen); // deactivate Border
painter.setBrush(QBrush("#101010")); // black title and footer
painter.drawRect(0, 0, 1024, 60);
painter.setBrush(QBrush("#101010"));
painter.drawRect(0, 516, 1024, 576);
}
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition);
event->accept();
}
}
void MyWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
m_dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
main.cpp
#include <QApplication>
#include "MyWindow.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MyWindow* window = new MyWindow(0, Qt::FramelessWindowHint);
window->show();
return app.exec();
}
Greetings,
Alex
You might want to try calling the base class methods in your protected methods. For example:
void MyWindow::paintEvent(QPaintEvent *event) // Painter
{
QWidget::paintEvent(event);
...
}
void MyWindow::mouseMoveEvent(QMouseEvent *event)
{
QWidget::moveMouseEvent(event);
...
}
I could not reproduce your problem (Ubuntu or XP) but how about trying to process repaint events:
if (event->buttons() & Qt::LeftButton) {
move(event->globalPos() - m_dragPosition);
event->accept();
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
}
It might be that your system is running low on resources or your display driver is "slow". Try it with a clean reboot and only your app open.

Resources