PDA

View Full Version : Painting the same thing in two subclasses of QPaintDevice



PierreA
6th August 2017, 08:04
I have a class DotCanvas: public QWidget that shows a pattern of dots. I also have some blank buttons which I'd like to label with dot patterns. So what I'm thinking of doing is making a class DotPixmap: public QPixmap with 256×256 pixel array, draw the patterns on them, load them into the icons, and load the icons into the buttons.

So I try to create a class DotPaintDevice which has the dot-painting code, and subclass DotCanvas from both QWidget and DotPaintDevice. I've done multiple inheritance before, so I figure I can do it, using "using" statements to tell the compiler from which superclass a subclass inherits an ambiguous function, if necessary. I compile it and get this:

/home/phma/src/mirasol/dotcanvas.h:38: Warning: Class DotCanvas inherits from two QObject subclasses QWidget and DotPaintDevice. This is not supported!
followed by lots of errors. I manage to fix some errors, but there are errors in compiling the moc output, which I haven't a clue what to do with.

So it looks like I have to duplicate the dot-painting code in two classes, one subclass of QWidget and one subclass of QPixmap. For this program the dot-painting code is simple, but how do you do it when you have some complicated stuff to display and you want to display and print the same stuff? Which I will, when I get around to adding the GUI to another program I'm working on.

high_flyer
6th August 2017, 23:49
As the warning you posted says, you can't inherit multiple times a QObject.


but how do you do it when you have some complicated stuff to display and you want to display and print the same stuff? Which I will, when I get around to adding the GUI to another program I'm working on.
You could make DotPaintDevice not derive form QObject.
There is really no need to as its only doing some specialized painting.

Pseudo code:


class DotPaintDevice
{
public:
DotPaintDevice();
~DotPaintDevice();
void mySpecialPainitingMethod();
};

class DotCanvas : public DotPaintDevice, public QWidget
{
void paintEvent(...)
{
.....
mySpecialPainitingMethod();
....
}
};

PierreA
13th August 2017, 09:53
Doesn't DotPaintDevice have to inherit QPaintDevice?

d_stranz
13th August 2017, 18:16
Doesn't DotPaintDevice have to inherit QPaintDevice?

No, QWidget inherits QPaintDevice, so the class DotCanvas *is* a QPaintDevice by virtue of multiple inheritance. You will need to pass a pointer or reference to the QPainter used in the paintEvent() to your special paint method.