Results 1 to 4 of 4

Thread: Painting the same thing in two subclasses of QPaintDevice

  1. #1
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Painting the same thing in two subclasses of QPaintDevice

    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.

  2. #2
    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: Painting the same thing in two subclasses of QPaintDevice

    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:
    Qt Code:
    1. class DotPaintDevice
    2. {
    3. public:
    4. DotPaintDevice();
    5. ~DotPaintDevice();
    6. void mySpecialPainitingMethod();
    7. };
    8.  
    9. class DotCanvas : public DotPaintDevice, public QWidget
    10. {
    11. void paintEvent(...)
    12. {
    13. .....
    14. mySpecialPainitingMethod();
    15. ....
    16. }
    17. };
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2017
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Painting the same thing in two subclasses of QPaintDevice

    Doesn't DotPaintDevice have to inherit QPaintDevice?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Painting the same thing in two subclasses of QPaintDevice

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 18
    Last Post: 3rd August 2013, 14:24
  2. Replies: 4
    Last Post: 27th December 2011, 11:34
  3. Creating signals in subclasses
    By Cotlone in forum Newbie
    Replies: 2
    Last Post: 22nd June 2010, 00:16
  4. Error creating subclasses
    By agerlach in forum Qt Programming
    Replies: 2
    Last Post: 25th May 2010, 13:49
  5. declaring subclasses in C++
    By Paat in forum Newbie
    Replies: 4
    Last Post: 23rd October 2009, 08:40

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.