Results 1 to 8 of 8

Thread: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

  1. #1
    Join Date
    Mar 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    I can paint using QCanvas or QPainter with no problems but when the Widget gets resized my overloaded drawContents method doesn't work the way I want it to:

    Using QPainter it only paints to the difference area of the picture and messes everything up. I have to change windows/cover the widget by something else to get it repainted correctly. I already tried setWindow, setViewport, drawArea, update, setAllChanged and setClipping but nothing helps. I also tried to create a workaround by hiding and then showing the widget again. Unfortunately I was unable to get rid of the infinite loop I created by that.

    Using QCanvas it repaints everything correctly - but I didn't find any method that clears all QCanvasItems linked to it, so it paints more and more and more each time the method gets called.

    The goal is to draw a chart. I am not allowed to use any external program, KDE classes, Qt>3.3.4 or similar, so I am stuck with QCanvas and QPainter. The chart should be able to be completely repainted (scale!) if the size gets changed, so I can't use QCanvas without a method to get rid of everything. I also tried to create a new instance of QCanvas and link it to QCanvasView but unfortunately this also creates an infinite loop.

    Trolltech's examples and documentation could not help me with that problem. Unfortunately it has to be solved till tomorrow, so it's quite urgent. Please tell me what I am doing wrong - I would be just fine if I could either clear QCanvas' drawing stack, reinstantiate it or being able to paint to the whole space using QPainter.

    Thanks for any comments on this!

    audiochart.h:
    Qt Code:
    1. #ifndef INCLUDED_AUDIOCHART_H
    2. #define INCLUDED_AUDIOCHART_H
    3.  
    4. #include <qcanvas.h>
    5.  
    6. class AudioChart : protected QCanvasView {
    7. Q_OBJECT
    8.  
    9. public:
    10. AudioChart( QWidget *parent, const char *name );
    11. void drawContents ( QPainter * p, int cx, int cy, int cw, int ch );
    12. private:
    13. QCanvas* myCanvas;
    14. int border;
    15. };
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 

    audiochart.cpp
    Qt Code:
    1. #include "audiochart.h"
    2. #include <qpainter.h>
    3. #include "guicommonhelper.h"
    4. #include <iostream>
    5.  
    6. AudioChart::AudioChart( QWidget *parent=0, const char *name=0 ) : QCanvasView::QCanvasView( parent, name ) {
    7. //QLabel* tmp = new QLabel( "implement me", parent );
    8.  
    9. this->myCanvas = new QCanvas( this, "" );
    10. this->setCanvas( this->myCanvas );
    11. this->myCanvas->setBackgroundColor( QColor( 255, 255, 255 ) );
    12.  
    13. this->border = 10;
    14. this->setVScrollBarMode( QScrollView::AlwaysOff );
    15. this->setHScrollBarMode( QScrollView::AlwaysOff );
    16.  
    17. //this->myCanvas->setDoubleBuffering( false );
    18. }
    19.  
    20. void AudioChart::drawContents( QPainter * p, int cx, int cy, int cw, int ch ) {
    21. this->myCanvas->resize(this->visibleWidth(), this->visibleHeight());
    22.  
    23. QPen pen = QPen( white, 0 );
    24. QBrush brush = QBrush( white );
    25.  
    26. p->setClipping( false );
    27. p->setPen( pen );
    28. p->fillRect( 0, 0, this->myCanvas->width(), this->myCanvas->height(), brush );
    29.  
    30. pen = QPen( black, 1 );
    31. QFont font = QFont( "Helvetica [Cronyx]", 8 );
    32. QFontMetrics* fm = new QFontMetrics( font );
    33.  
    34. char tmpString[255];
    35.  
    36. p->setPen( pen );
    37. p->drawLine( 20, 50, 50, 20 );
    38. p->setFont( font );
    39.  
    40. int w, h, x, y;
    41.  
    42. int max = 10;
    43. int offsetXAxis = 10;
    44. int totalWidth = this->myCanvas->width() - 2*this->border - 10 - offsetXAxis;
    45. int totalHeight = this->myCanvas->height() - 2*this->border;
    46.  
    47. std::cout<<"w "<<totalWidth<<" h "<<totalHeight<<std::endl;
    48.  
    49. std::cout<<"-"<<std::endl;
    50. for (int i = 0; i <= max; i++) {
    51. QString string = QString(intToChar(tmpString, i*3823/max));
    52. w = fm->width(string);
    53. h = fm->height();
    54.  
    55. x = totalWidth*i/max + this->border + offsetXAxis;
    56. y = totalHeight + this->border;
    57.  
    58. std::cout<<i<<": x "<<x<<" y "<<y<<std::endl;
    59.  
    60. p->drawText( x, y, string );
    61. //p->drawRect( x, y-h, w, h );
    62. p->drawLine( x+w/2-1, y-h, x+w/2-1, y-h-2 );
    63. }
    64. std::cout<<"-"<<std::endl;
    65.  
    66. p->flush();
    67. this->myCanvas->setAllChanged();
    68. this->update();
    69. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    I think you should use a normal widget for drawing.

    BTW where are the canvas items you needed to be deleted?

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

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    Using QPainter it only paints to the difference area of the picture and messes everything up.
    This can be contolled with the widget flags.
    Try playing with Qt::WStaticContents and Qt::WNoAutoErase and see if it helps.

  4. #4
    Join Date
    Mar 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    Thanks for the hint, unfortunately it still doesn't work.

    WStaticContents seems to be exactly what I want to avoid while WNoAutoErase sounds like a solution. But it still looks the same:

    The original painter output:



    Window shrinked:



    Window enlarged again:



    Window repainted due to putting another window in front of it:




    Do you have any other ideas?

  5. #5
    Join Date
    Mar 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    Okay, just wanted to let you know I am using a very dirty workaround for now as time is slowly running out.

    As I thought it is possible to call hide() and show() on the widget to get it repainted (repaint() didn't work for me neither...). Since I can't call it from drawContents I decided to write a small event handler for the resized() signal emitted by QCanvas. Unfortunately this is only half the way: It works great but only when enlarging the widget.

    I couldn't find any better way to repaint it on being shrinked but by creating a QTimer that calls an event handler to check for decrease in the main widgets size and then calls hide() and show() on all widgets that need it.

    As I said that's veeeery dirty but at least it works.

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

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    What do you need QCanvas for? Where are the QCanvasItems?

  7. #7
    Join Date
    Mar 2006
    Posts
    6
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    As I said I cannot use QCanvasItems since I draw dynamically changing content and don't know how to clear a QCanvas from all its items.

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

    Default Re: [Qt 3.3.4] How do I repaint anything using QCanvas or QPainter?

    Quote Originally Posted by EnQ
    As I said I cannot use QCanvasItems since I draw dynamically changing content and don't know how to clear a QCanvas from all its items.
    So why do you use QCanvas and QCanvasView? Wouldn't it be easier to use plain QWidget?

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.