Qt: QPainter Klasse ableiten funktioniert nicht



  • Hallo,
    ich hab probiert die QPainterklasse abzuleiten, aber waerend des ausfuehrens bekomme ich immer folgenden fehler:

    m_good_cannon->setAngle(5);
    QPainter::begin: Widget painting can only begin as a result of a paintEvent
    Painter must be active to set rendering hints
    QPainter::save(), painter not active
    QPainter::setMatrix(), painter not active
    QPainter::setMatrix(), painter not active
    QPainter::restore(), unbalanced save/restore
    m_good_cannon->setAngle(15);

    Es bewegt sich auch nichts. erst wenn ich das fenster minimiere und wiederherstelle, wird das widget neugezeichnet (dann ohne fehler).

    hier die abgeleitete klasse:

    #ifndef MYQPAINTER_H
    #define MYQPAINTER_H
    
    #include <QPainter>
    #include <QPoint>
    
    /**
    @author Adam Celarek
    */
    class MyQPainter : public QPainter
    {
    	// methods:
    public:
    	MyQPainter( QPaintDevice * pd );
    	~MyQPainter();
    	void drawCannon( int angle, int force, QPoint coord, bool cannon_shoots_to_the_right = true);
    	void drawBullet( QPoint coord );
    
    	// variables:
    private:
    	QPaintDevice* m_cannon_field;
    };
    
    #endif
    
    #include "myqpainter.h"
    
    MyQPainter::MyQPainter( QPaintDevice * pd )
    		: QPainter( pd ), m_cannon_field ( pd )
    {}
    MyQPainter::~MyQPainter()
    {}
    
    /*!
        \fn MyQPainter::drawCannon(int angle, int force)
     */
    void MyQPainter::drawCannon( int angle, int force, QPoint coord, bool cannon_shoots_to_the_right)
    {
    	int x = coord.x();
    	int y = m_cannon_field->height() - coord.y();
    	int phi = ( -1 ) * angle;
    	if ( !cannon_shoots_to_the_right )
    		phi -= 90;
    
    	save();			   // 2. fehler hier
    	translate( x, y );	// 3. f
    	rotate( phi );		// 4. f
    	drawEllipse( -35, -35, 70, 70 );
    	drawRect( 0, -10, 40, 20 );
    	drawLine( 0, 0, force + 40, 0 );
    	restore();			// 5.f
    }
    

    und hier wird sie aufgerufen:

    /// protected:
    void CannonField::paintEvent(QPaintEvent * /* event */)
    {
    	MyQPainter* painter= new MyQPainter(this);
    	painter->setBrush(Qt::blue);
    	painter->setRenderHint(QPainter::Antialiasing, true);		// 1. fehler hier
    
    	painter->drawCannon(m_good_cannon->angle(), m_good_cannon->force(), QPoint(0,0));
    	delete painter;
    }
    

    danke schon mal..
    mfg aMan..

    btw:
    was ist besser, QPainter als pointer oder normal?



  • Bei welchem Aufruf kommen die Fehler genau?



  • habs jetzt im quellcode markiert..
    (haßt du das gemeint?)

    mfg aMan..



  • QPainter::begin: Widget painting can only begin as a result of a paintEvent

    Das sieht stark danach aus, dass du irgendwie manuell paintEvent aufrufst. Kann das sein?

    Ansonsten wäre der ganze Quellcode im .tar.gz gut, damit man sich das mal genau anschauen kann. Quellcode im Browser zu lesen ist nicht mein Ding.



  • argh, stimmt..

    thx.. funzt jetzt..

    kann mir bitte noch jemand sagen, ob ein QPainter* oder ein QPainter besser ist?

    mfg aMan..


Anmelden zum Antworten