Results 1 to 11 of 11

Thread: Paint a compass on a GUI

  1. #1
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Paint a compass on a GUI

    I need to paint a compass with a 360° rotating rectangle on a GUI.
    The compass is like the one in this image:



    The only part that needs to be painted is the rectangle ...
    Is there some quick way to do that or do I have to paint it by myself with a QGraphicsView object ?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Paint a compass on a GUI

    Use a normal Widget and do all the stuff in its paint event. Draw background picture and then rotate the painter and draw the rect. You don't need graphics view for that!

  3. #3
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Ok thanks, but how can I rotate the painter ?

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Paint a compass on a GUI

    QPainter::rotate() or you use a more complex QPainter::setTransform().

  5. #5
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Uhm, it's not so clear ...
    I have a GUI with a lot of objects like buttons that will not rotate, of course.
    Inside this GUI I put a QFrame ? QWidget ? something that has a painter, I guess.
    Then I draw the rect (how ?) and rotate the painter ...

    I have experience with QGraphicsView but not with drawing in normal QWidgets.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Paint a compass on a GUI

    It is basic subclassing!
    Qt Code:
    1. class Compass : public QWidget
    2. {
    3. Compass(QWidget* parent = 0) : QWidget(parent) {}
    4. protected:
    5. void paintEvent(QPaintEvent* event)
    6. {
    7. Q_UNUSED(event);
    8. QPainter p(this);
    9. // do your paintings here
    10. }
    11. };
    To copy to clipboard, switch view to plain text mode 

    Then you also have to provide a proper size hint method.

  7. #7
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Maybe I've got it
    I've found this example: http://cartan.cas.suffolk.edu/qtdocs...alogclock.html

  8. #8
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Yes, it's clear now.
    Thanks a lot

  9. #9
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Everything works now, but I have a problem ...
    I have a QLabel 160x160 as a widget where I draw an ellipse 160x160 and a rectangle 160x20 in the center.
    So I do the following to rotate the rectangle:

    Qt Code:
    1. QTransform transform;
    2. transform.translate(80, 80);
    3. transform.rotate(m_RotateAngle);
    4. p.setTransform(transform);
    To copy to clipboard, switch view to plain text mode 

    but the rectangle moves around the center, it doesn't rotate.
    Any suggestion ?

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Paint a compass on a GUI

    First you have to translate the painter! See here:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class Compass : public QWidget
    4. {
    5. public:
    6. Compass(QWidget* parent = 0) : QWidget(parent)
    7. {
    8. rect = QRect(0, 0, 100, 100);
    9. }
    10. QSize sizeHint() const
    11. {
    12. return rect.size();
    13. }
    14. protected:
    15. void paintEvent(QPaintEvent* event)
    16. {
    17. Q_UNUSED(event);
    18. QPainter p(this);
    19. p.setRenderHint(QPainter::Antialiasing, true);
    20. p.drawEllipse(rect);
    21.  
    22. p.save();
    23. p.translate(rect.center());
    24. p.rotate(15);
    25. p.drawRect(QRect(-20, -10, 40, 20));
    26. p.restore();
    27. }
    28. private:
    29. QRect rect;
    30. };
    31.  
    32. int main(int argc, char* argv[])
    33. {
    34. QApplication a(argc, argv);
    35. Compass c;
    36. c.show();
    37. return a.exec();
    38. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Aug 2009
    Posts
    92
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Paint a compass on a GUI

    Ah ok, I had to change the rectangle coordinates.
    That's perfect now, thanks again

Similar Threads

  1. Immediate paint
    By zgulser in forum Qt Programming
    Replies: 11
    Last Post: 15th March 2010, 07:34
  2. paint events
    By freekill in forum Newbie
    Replies: 3
    Last Post: 18th February 2010, 06:29
  3. paint
    By xyzt in forum Qt Programming
    Replies: 3
    Last Post: 22nd March 2008, 18:22
  4. Replies: 2
    Last Post: 14th March 2008, 12:38
  5. Rotation problem trying to draw a compass widget
    By yellowmat in forum Qt Programming
    Replies: 2
    Last Post: 18th February 2007, 19:03

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.