Results 1 to 3 of 3

Thread: Issues using QGraphicsDropShadowEffect with QMouseEvent

  1. #1
    Join Date
    Apr 2017
    Location
    Hohenlimburg, Germany
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Issues using QGraphicsDropShadowEffect with QMouseEvent

    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:

    Qt Code:
    1. QPainter::begin: Paint device returned engine == 0, type: 3
    2. QPainter::scale: Painter not active
    3. QPainter::setRenderHint: Painter must be active to set rendering hints
    4. QPainter::end: Painter not active, aborted
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. private slots:
    19. void mouseMoveEvent(QMouseEvent *event);
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QGraphicsDropShadowEffect>
    4. #include <QDebug>
    5. #include <QMouseEvent>
    6.  
    7.  
    8. MainWindow::MainWindow(QWidget *parent) :
    9. QMainWindow(parent),
    10. ui(new Ui::MainWindow)
    11. {
    12. ui->setupUi(this);
    13. this->setMouseTracking(true);
    14. ui->label->setMouseTracking(true);
    15. ui->label_2->setMouseTracking(true);
    16. ui->label_3->setMouseTracking(true);
    17. ui->label_4->setMouseTracking(true);
    18. ui->widget->setMouseTracking(true);
    19. ui->centralWidget->setMouseTracking(true);
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24. delete ui;
    25. }
    26.  
    27. void MainWindow::mouseMoveEvent(QMouseEvent *event){
    28. this->repaint();
    29. QGraphicsDropShadowEffect * dse = new QGraphicsDropShadowEffect();
    30. dse->setBlurRadius(8);
    31. dse->setColor(QColor(0,0,0,180));
    32.  
    33. QGraphicsDropShadowEffect * dse2 = new QGraphicsDropShadowEffect();
    34. dse2->setBlurRadius(8);
    35. dse2->setColor(QColor(0,0,0,180));
    36.  
    37. QGraphicsDropShadowEffect * dse3 = new QGraphicsDropShadowEffect();
    38. dse3->setBlurRadius(8);
    39. dse3->setColor(QColor(0,0,0,180));
    40.  
    41. QGraphicsDropShadowEffect * dse4 = new QGraphicsDropShadowEffect();
    42. dse4->setBlurRadius(8);
    43. dse4->setColor(QColor(0,0,0,180));
    44.  
    45. int centralX = ui->centralWidget->width()/2;
    46. int centralY = ui->centralWidget->height()/2;
    47. int off1x;
    48. int off1y;
    49.  
    50.  
    51. if (event->pos().x() > centralX) {
    52. off1x = centralX - event->pos().x();
    53. //qDebug() << off1x;
    54. }
    55. else if (event->pos().x() < centralX) {
    56. off1x = (event->pos().x() - centralX)*-1;
    57.  
    58. }
    59.  
    60. if (event->pos().y() > centralY) {
    61. off1y = centralY - event->pos().y();
    62.  
    63. }
    64. else if (event->pos().y() < centralY) {
    65. off1y = (event->pos().y() - centralY)*-1;
    66.  
    67. }
    68.  
    69. dse->setOffset((off1x+(ui->label->x()/4))/20,(off1y+(ui->label->y()/4))/20);
    70. dse2->setOffset((off1x+(ui->label_2->x()/4))/20,(off1y+(ui->label_2->y()/4))/20);
    71. dse3->setOffset((off1x+(ui->label_3->x()/4))/20,(off1y+(ui->label_3->y()/4))/20);
    72. dse4->setOffset((off1x+(ui->label_3->x()/4))/20,(off1y+(ui->label_4->y()/4))/20);
    73. ui->label->setGraphicsEffect(dse);
    74. ui->label_2->setGraphicsEffect(dse2);
    75. ui->label_3->setGraphicsEffect(dse3);
    76. ui->label_4->setGraphicsEffect(dse4);
    77.  
    78. }
    To copy to clipboard, switch view to plain text mode 

    I would appreciate any help!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Issues using QGraphicsDropShadowEffect with QMouseEvent

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2017
    Location
    Hohenlimburg, Germany
    Posts
    4
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Android

    Default Re: Issues using QGraphicsDropShadowEffect with QMouseEvent

    Okay thanks!
    I'll try that!

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2014, 14:31
  2. Alternative to qgraphicsdropshadoweffect
    By reginaldt in forum Qt Programming
    Replies: 4
    Last Post: 3rd July 2014, 10:17
  3. Replies: 0
    Last Post: 14th February 2014, 09:45
  4. QGraphicsDropShadowEffect is half baked?
    By liversedge in forum Qt Programming
    Replies: 1
    Last Post: 4th June 2011, 20:59
  5. QGraphicsDropShadowEffect causing a QPainter error
    By Berryblue031 in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2011, 18:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.