class derivated from QImage
-
Hi, I have problems with my class derivated from QImage for the copy and/or affectation (Ihave defined a copy constructor and an = operator). the copy and affectation don't work.
shouldn't we derive from QImage and use a class containing a QImage instead ? -
No, call the base class operator= method.
-
#ifndef IMAGE_H #define IMAGE_H #include <QImage> #include <QString> class Image : public QImage { bool used; QString filename; public: Image():QImage(),used(false){}; Image(const QSize &size, Format format):QImage(size,format),used(false){}; Image(const Image & i){used=i.used;filename=i.filename;}; Image(const QImage &i ){used=false;filename=QString("");}; Image & operator=(const Image & i); Image & operator=(const QImage &i); ~Image(){}; Image scaled(const QSize); void setused(bool usd) {used=usd;}; bool isused(){return used;}; void setfilename(QString & s){filename=s;}; QString & getfilename(){return filename;}; }; Q_DECLARE_METATYPE(Image) Q_DECLARE_OPAQUE_POINTER(Image *) #endif // IMAGE_H
#include "image.h"
Image & Image::operator=(const Image & i)
{
if(this!=&i)
{used=i.used;filename=i.filename;
}
return *this;
}
Image & Image::operator=(const QImage & i)
{
if(this!=&i)
{used=false;filename=QString("");
}
return *this;
}
Image Image::scaled(const QSize sz)
{QImage res2=((QImage*)this)->scaled(sz); Image res=res2; return (res);
}
-
But I can test with a class containing a QImage(instead of derivating) and my two other variables to see if the bug comes from QImage
-
Hi,
You might want to call the base class operator, currently you are only assigning your custom fields.
-
No, call the base class operator= method.
-
@SGaist said in class derivated from QImage:
No, call the base class operator= method.
Thank you I have done :
QImage::operator=(i);
and for the copy constructor ? should I do something else?
-
Good !
Then please mark the thread as solved using the "Topic Tools" or use the three doted menu beside the answer that you deem correct so that other forum users may know a solution has been found.