PDA

View Full Version : Issues using QGraphicsDropShadowEffect with QMouseEvent



ankalagon
17th April 2017, 14:48
Hi there and happy easter,

I've the follwing issues:

I made a prog, where there are some labels with drop shadow effect, wich is calculatet relative to the mouse cursor position. (As is the cursor is a light source, it's like in the "Populous 3" menu.

So far it's working, BUT:

Even though I've activated the mousetracking property on every element, when I hover over one of the labels, the effect stops to work, it does only work outside the labels.

And another strange issue is:
Sometimes the hole gui freezes and the output says:


QPainter::begin: Paint device returned engine == 0, type: 3
QPainter::scale: Painter not active
QPainter::setRenderHint: Painter must be active to set rendering hints
QPainter::end: Painter not active, aborted

Then for some short time, it's working again, but next, the shadow completely disappears, and the GUI freezes totally.
And the really strange thing is, that this issue is not there when I run the prog with the debugger! oO

I'm really stuck on this and haven't any clue where the bug might be.

I'm using Qt 5.8.0 64bit on openSUSE 42.1

This is the code:

mainwindow.h:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void mouseMoveEvent(QMouseEvent *event);

private:
Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

main.cpp:

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}


mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsDropShadowEffect>
#include <QDebug>
#include <QMouseEvent>


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setMouseTracking(true);
ui->label->setMouseTracking(true);
ui->label_2->setMouseTracking(true);
ui->label_3->setMouseTracking(true);
ui->label_4->setMouseTracking(true);
ui->widget->setMouseTracking(true);
ui->centralWidget->setMouseTracking(true);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::mouseMoveEvent(QMouseEvent *event){
this->repaint();
QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
dse->setBlurRadius(8);
dse->setColor(QColor(0,0,0,180));

QGraphicsDropShadowEffect * dse2 = new QGraphicsDropShadowEffect();
dse2->setBlurRadius(8);
dse2->setColor(QColor(0,0,0,180));

QGraphicsDropShadowEffect * dse3 = new QGraphicsDropShadowEffect();
dse3->setBlurRadius(8);
dse3->setColor(QColor(0,0,0,180));

QGraphicsDropShadowEffect * dse4 = new QGraphicsDropShadowEffect();
dse4->setBlurRadius(8);
dse4->setColor(QColor(0,0,0,180));

int centralX = ui->centralWidget->width()/2;
int centralY = ui->centralWidget->height()/2;
int off1x;
int off1y;


if (event->pos().x() > centralX) {
off1x = centralX - event->pos().x();
//qDebug() << off1x;
}
else if (event->pos().x() < centralX) {
off1x = (event->pos().x() - centralX)*-1;

}

if (event->pos().y() > centralY) {
off1y = centralY - event->pos().y();

}
else if (event->pos().y() < centralY) {
off1y = (event->pos().y() - centralY)*-1;

}

dse->setOffset((off1x+(ui->label->x()/4))/20,(off1y+(ui->label->y()/4))/20);
dse2->setOffset((off1x+(ui->label_2->x()/4))/20,(off1y+(ui->label_2->y()/4))/20);
dse3->setOffset((off1x+(ui->label_3->x()/4))/20,(off1y+(ui->label_3->y()/4))/20);
dse4->setOffset((off1x+(ui->label_3->x()/4))/20,(off1y+(ui->label_4->y()/4))/20);
ui->label->setGraphicsEffect(dse);
ui->label_2->setGraphicsEffect(dse2);
ui->label_3->setGraphicsEffect(dse3);
ui->label_4->setGraphicsEffect(dse4);

}



I would appreciate any help!

wysota
18th April 2017, 23:16
First of all:
1. use update() and not repaint()
2. you're leaking memory creating the effects at each mouseMove event. Reuse the effects instead.
3. you're handling mouse events on the main window but not on the labels which is probably the direct reason why you're observing such an effect. Disable mouse tracking on the labels, you certainly don't need it there - only the main window should be getting the events.

ankalagon
19th April 2017, 12:45
Okay thanks!
I'll try that!