Results 1 to 5 of 5

Thread: QWidget and QRects

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QWidget and QRects

    Hi all!

    I have a QWidget subclassed and I need to implement some repaints with mouse move event, but when I use the QRect contains to check if the mouse position is inside the QRect it gives me a different position as the drawed QRect.

    What is going on here?

    Code
    fluxocaixawidget.h
    Qt Code:
    1. #ifndef FLUXOCAIXAWIDGET_H
    2. #define FLUXOCAIXAWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QPainter>
    6.  
    7. class fluxoCaixaWidget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit fluxoCaixaWidget(QWidget *parent = 0);
    12.  
    13. QRect rectLinhaTempo();
    14.  
    15. protected:
    16. void paintEvent(QPaintEvent *);
    17. void mouseMoveEvent(QMouseEvent *);
    18.  
    19. private:
    20. bool dentro;
    21. QRect linhaTempo;
    22.  
    23. signals:
    24.  
    25. public slots:
    26.  
    27. };
    28.  
    29. #endif // FLUXOCAIXAWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    fluxocaixawidget.cpp
    Qt Code:
    1. #include "fluxocaixawidget.h"
    2.  
    3. #include <QDebug>
    4.  
    5. fluxoCaixaWidget::fluxoCaixaWidget(QWidget *parent) :
    6. QWidget(parent)
    7. {
    8. this->setMouseTracking(true);
    9. this->dentro = false;
    10. }
    11.  
    12. void fluxoCaixaWidget::paintEvent(QPaintEvent *)
    13. {
    14. QPainter painter(this);
    15.  
    16. linhaTempo.setRect(20, this->parentWidget()->height() / 2 - 20, this->parentWidget()->width()-65, 30);
    17.  
    18. QPen caneta;
    19.  
    20. QBrush pincel(Qt::white);
    21.  
    22. painter.setBrush(pincel);
    23.  
    24. painter.drawRect(parentWidget()->rect());
    25.  
    26. if(this->dentro == true)
    27. {
    28. caneta.setColor(Qt::red);
    29. caneta.setWidth(4);
    30. }
    31. else
    32. {
    33. caneta.setColor(Qt::black);
    34. caneta.setWidth(2);
    35. }
    36.  
    37. painter.setPen(caneta);
    38.  
    39. painter.drawRect(linhaTempo);
    40. }
    41.  
    42. void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
    43. {
    44. if(this->linhaTempo.contains(QCursor::pos()))
    45. {
    46. qDebug() << "OK";
    47. this->dentro = true;
    48. }
    49. else
    50. {
    51. qDebug() << "Fora";
    52. this->dentro = false;
    53. }
    54. this->update();
    55. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2012
    Location
    India.
    Posts
    88
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget and QRects

    Read MapToParent and see if it is required to do in your case while setting rect in paint event.

  3. #3
    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: QWidget and QRects

    Why are you accessing parentWidget() in the paint event of a different widget? The widget paints on itself, not on its parent. There may even be no parent widget at all. What is the effect you are trying to achieve?
    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.


  4. #4
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget and QRects

    Thanks for the reply.

    I need to paint relatively to the parent width and check if the muse is inside the painted object.

  5. #5
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWidget and QRects

    SOLVED

    Thanks for your help and time wysota.

    Cheers!

    Qt Code:
    1. void fluxoCaixaWidget::mouseMoveEvent(QMouseEvent *)
    2. {
    3. if(this->linhaTempo.contains(this->mapFromGlobal(QCursor::pos())))
    4. {
    5. qDebug() << "OK";
    6. this->dentro = true;
    7. }
    8. else
    9. {
    10. qDebug() << "Fora";
    11. this->dentro = false;
    12. }
    13. this->update();
    14. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 30th October 2010, 13:28
  2. Replies: 1
    Last Post: 16th September 2010, 16:57
  3. Replies: 1
    Last Post: 12th April 2010, 13:55
  4. Replies: 3
    Last Post: 2nd April 2010, 00:56
  5. Replies: 1
    Last Post: 2nd May 2006, 22:11

Tags for this Thread

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.