PDA

View Full Version : Beginner C++ question



masoroso
19th April 2006, 14:43
I hope someone finds the time to answer my C++ beginner question!

I `designed' a new class around the QPixMap like this:


class Image : public QPixmap
{
private:
int m_x;
int m_y;
QString m_fileName;

public:
Image( const QString &filename, int x, int y );

};

It only adds some new properties to the class.

Then I wrote another class that contains a list of these classes

class Map : public QWidget
{
Q_OBJECT

private:
QList<Image *> m_imageList;

public:
Map( QWidget *parent=0 );
...
protected:
void paintEvent(QPaintEvent*);

};

In the paintEvent function I want to draw the Images to the screen but I don't know how to do that :D How can I use the drawPixmap function to draw the pixmap in the Image class??

This is my (erronous) code to drawthe first image


void Map::paintEvent(QPaintEvent*)
{
if(m_imageList.count() > 0){
QPainter p( this );
p.save();
p.drawPixmap( 0, 0, m_imageList.first()); <= ERROR
p.restore();
}
}

Could I just cast the Image to a Pixmap class? And if so, how?

Thanks in advance.. the inheritance options in C++ still confuse me :o

masoroso
19th April 2006, 14:51
sorry.. found the solution myself


p.drawPixmap( 0, 0, *(dynamic_cast<QPixmap*>(m_imageList.first())));

not realy nice to read actualy :)

jacek
19th April 2006, 15:15
IMO this should be enough:
p.drawPixmap( 0, 0, *(m_imageList.first()));