Results 1 to 17 of 17

Thread: Howto draw stuff

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Howto draw stuff

    How should one draw stuff?
    There are a few different things that can be done,
    1)Draw lines, polygons, circles etc.
    2)Draw letters
    3)Draw something (lines, pixels, etc.) and then redraw it at a sligthliy different position (i.e. animation).
    4)Load an image from a file and draw it, and possible also move it around as some kind of animation.

    I read about the Arthur painting system and look at the tutorials but I can't make a connection between these two. How should these things REALLY be done?
    Any ideas?

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

    Default Re: Howto draw stuff

    What exactly do you want to do?
    Here are some painting examples

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    Quote Originally Posted by wysota
    What exactly do you want to do?
    Here are some painting examples
    I have a lot of ideas, a simple game where you fly around in a space craft? A plotting tool (i.e. a lot of datapoints), etc.
    I'm just interested in good strategies for drawingstuff.

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

    Default Re: Howto draw stuff

    Take a look at QPainter class and its superclass.

    And remember to paint only in paintEvents. The examples mentioned above will give you a good start.

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    If I make a program where I can plot things, and then wish to select the things in the graph, by clicking them, how do I do this? I'm totaly clueless on these things.

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

    Default Re: Howto draw stuff

    Store a list of your objects and in the mousePressEvent of your widget check if you hit one of the items based on the coords of the click and stored items.

  7. #7
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    Quote Originally Posted by Morea
    I have a lot of ideas, a simple game where you fly around in a space craft? A plotting tool (i.e. a lot of datapoints), etc.
    I'm just interested in good strategies for drawingstuff.
    3D and 2D plotting are very different.
    For a 3D space flight I would use open GL or other external libs that do 3D rendering.
    For 2D plotting Qt comes very well equiped, you should read the docs on painting and QPainter.

  8. #8
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    QCanvas, that was Qt3, right?

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

    Default Re: Howto draw stuff

    Quote Originally Posted by Morea
    QCanvas, that was Qt3, right?
    Yes. It is not needed in Qt4 anymore. All the magic features it had are now present in QWidget.

  10. #10
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    If I create a class for drawing
    Qt Code:
    1. class Render:public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. Render(QWidget* parent=0);
    6. };
    To copy to clipboard, switch view to plain text mode 

    how can I set the size and position of the area where I should be doing the drawings if I'm not using the layout classes? Or should I really start using the layout tools?
    Last edited by Morea; 17th March 2006 at 22:30.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    Quote Originally Posted by Morea
    how can I set the size and position of the area where I should be doing the drawings if I'm not using the layout classes?
    QWidget::setGeometry()
    Or should I really start using the layout tools?
    Yes, you should. Don't forget to reimplement QWidget::sizeHint().

  12. #12
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    I'm having some problem understanding how stuff is drawn on a widget with the QPainter.
    If I do

    Qt Code:
    1. void Render::paintEvent(QPaintEvent* event)
    2. {
    3. int i;
    4. QPainter painter(this);
    5. for (i=0;i<10;i++)
    6. {
    7.  
    8. painter.drawLine(QLine(rand()%250,rand()%100,rand()%250,rand()%100));
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Where Render is a only derived from QWidget and it is put in a Layout to stretch it out and make it big. The code works fine.
    I make new lines by clicking a button that calls a slot in Render class that then runs
    Qt Code:
    1. update();
    To copy to clipboard, switch view to plain text mode 
    to generate a QPaintEvent

    I get 10 new lines every time, is it possible to keep the old lines and just draw new lines on the old ones?

  13. #13
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    Quote Originally Posted by Morea
    I get 10 new lines every time, is it possible to keep the old lines and just draw new lines on the old ones?
    set Qt::WA_NoSystemBackground flag of QWidget.
    Last edited by munna; 18th March 2006 at 12:16.

  14. #14
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    But that gives me an ugly black backgroud, can I get a white background?

  15. #15
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    When the flag Qt::WA_NoSystemBackground is set the background is not automatically repainted and that is why you get black background. Therefore once you set this flag you have to take care paiting even the background. Also, double buffering is disabled once you set this flag.

    On the other hand, if you dont want to use this flag, you can draw your older lines and the new lines every time the paintEvent is called. I think this will be pretty fast and you will not find any kind of flicker or redrawing that is actually taking place.

  16. #16
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    But what if that means drawing tens of thounds random points? I will really have to store them all then? I'll try that then.

  17. #17
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Howto draw stuff

    You can either

    paint background only once and also keep track of newly shown regions and paint their background also only once. Here you do not have to store your points and just keep drawing the new points.

    or

    Store all your points or line ...and then for every paintEvent redraw them. Here you do not have to keep track of the newly show regions which need to be painted white for the first time.

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.