Results 1 to 10 of 10

Thread: passing a QPainter object to widgets

  1. #1
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default passing a QPainter object to widgets

    Hello,
    I'm writing a program that acts this way:
    - I have a principal QWidget over which I create a QPainter,
    - I draw on the widget,
    - I pass the Painter to other widgets so I can use those widgets to draw on the principal one,
    - Finally,in the principal widget,I call end() on the painter.

    The program works properly but ends with a segmentation fault. I runned gdb and it sais that the problem was that it doesn't found qlist.h (quite strange I think).

    However the output has no apparent problem:I can display the result of my painting on video and I can eventually draw it on a file. The problem arises when I try to convert the .ps image created with my program to a .pdf,in fact I obtain this error using dvipdf ( in fact I use the .ps file I create as an image in a latex text):

    /usr/bin/dvipdf: line 40: 20896 Broken pipe exec dvips -q -f "$infile"
    20897 Segmentation fault | gs $OPTIONS -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$outfile" $OPTIONS -c .setpdfwrite -

    I'm sure it's not a problem of the latex file or of my dvipdf because if I do the same things but without passing the QPainter to other widgets,I have no problem.

    What can I do?

    thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Hi,

    You can try to do something like

    Qt Code:
    1. // Principal widget
    2. void ChildWidget::myFunc(QPainter* painter)
    3. {
    4. painter->begin(parentWidget());
    5. // Drawing
    6. painter->end();
    7. }
    To copy to clipboard, switch view to plain text mode 

    PS. If you use Qt 4.1 you can print to a PDF file directly using

    Qt Code:
    1. printer.setPrinterMode(QPrinter::HighResolution);
    2. printer.setOutputFormat(QPrinter::PdfFormat);
    To copy to clipboard, switch view to plain text mode 

    PPS. Sorry for my very bad English.

    PPPS. For vratojr: I'm italian like you. If you like you can write me in private post in Italian
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Quote Originally Posted by vratojr
    - I draw on the widget,
    - I pass the Painter to other widgets so I can use those widgets to draw on the principal one,
    QCanvas does similar thing, so there shouldn't be any problems with this approach.

    How do those widget draw themselves?

    Quote Originally Posted by vratojr
    The program works properly but ends with a segmentation fault. I runned gdb and it sais that the problem was that it doesn't found qlist.h (quite strange I think).
    Make sure you compile your application in debug mode (you should have "CONFIG += debug" in .pro file). Then run your application from gdb. When is crashes issue "bt" command to print the backtrace and post it here.

  4. #4
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    How do those widget draw themselves?
    Here is a brief description:

    Qt Code:
    1. //Inside the Main widget's paintEvent:
    2. QPainter painter;
    3. painter.begin( graficoPaintDevice );
    4. //drawing
    5. assex = new AsseNumerico (/*options*/,painter,this);
    6. assex.draw();
    7. painter.end();
    8.  
    9. //stucture of the second widget
    10. class AsseNumerico,public QWidget{
    11. public:
    12. AsseNumerico(....);
    13. void draw(QPainter &painter);
    14. private:
    15. };
    16.  
    17. AsseNumerico::AsseNumerico(/*options*/,QPainter &painter,QWidget *parent):QWidget(parent),p(painter){};
    18.  
    19. void AsseNumerico::draw(Qpainter &painter){
    20. p.drawSomething();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Make sure you compile your application in debug mode (you should have "CONFIG += debug" in .pro file). Then run your application from gdb. When is crashes issue "bt" command to print the backtrace and post it here.
    Well, first. I will explain better and briefly how the program works. I do some calculation to draw an istogram on screen and afterward I give the opportunity to print it.
    If I display the istogram on screen WITHOUT trying to print it I got the the error:

    *** glibc detected *** double free or corruption (!prev): 0x0825f648 ***

    And, if i use the file in the .ps and then use dvipdf I got the error:
    /usr/bin/dvipdf: line 40: 28347 Broken pipe exec dvips -q -f "$infile"
    28348 Segmentation fault | gs $OPTIONS -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$outfile" $OPTIONS -c .setpdfwrite -
    Segmentation fault

    And this is the ouput from gdb:

    *** glibc detected *** double free or corruption (!prev): 0x08252e38 ***

    Program received signal SIGABRT, Aborted.
    [Switching to Thread -1221314880 (LWP 31932)]
    0xffffe410 in ?? ()
    (gdb) bt
    #0 0xffffe410 in ?? ()
    #1 0xbfffed94 in ?? ()
    #2 0x00000006 in ?? ()
    #3 0x00007cbc in ?? ()
    #4 0xb738c6e5 in raise () from /lib/tls/libc.so.6
    #5 0xb738e049 in abort () from /lib/tls/libc.so.6
    #6 0xb73c07ba in __fsetlocking () from /lib/tls/libc.so.6
    #7 0xb73c6d9c in malloc_trim () from /lib/tls/libc.so.6
    #8 0xb73c737a in free () from /lib/tls/libc.so.6
    #9 0xb756a6f1 in operator delete () from /usr/lib/libstdc++.so.6
    #10 0xb7bca5d7 in ~QPainterPrivate (this=0x82644b0) at qpainter_p.h:120
    #11 0xb7bbe78a in ~QPainter (this=0x819629c) at painting/qpainter.cpp:635
    #12 0x08081a4b in ~AsseRiferimento (this=0x8196238) at .moc/moc_assenumerico.cpp:77

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Quote Originally Posted by vratojr
    *** glibc detected *** double free or corruption (!prev): 0x08252e38 ***

    Program received signal SIGABRT, Aborted.
    [...]
    #12 0x08081a4b in ~AsseRiferimento (this=0x8196238) at .moc/moc_assenumerico.cpp:77
    It seems that you delete memory twice. What happens in that AsseRiferimento destructor?

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Quote Originally Posted by vratojr
    Here is a brief description:

    Qt Code:
    1. //Inside the Main widget's paintEvent:
    2. QPainter painter;
    3. painter.begin( graficoPaintDevice );
    4. //drawing
    5. assex = new AsseNumerico (/*options*/,painter,this);
    6. assex.draw();
    7. painter.end();
    8.  
    9. //stucture of the second widget
    10. class AsseNumerico,public QWidget{
    11. public:
    12. AsseNumerico(....);
    13. void draw(QPainter &painter);
    14. private:
    15. };
    16.  
    17. AsseNumerico::AsseNumerico(/*options*/,QPainter &painter,QWidget *parent):QWidget(parent),p(painter){};
    18.  
    19. void AsseNumerico::draw(Qpainter &painter){
    20. p.drawSomething();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Try to use QPainter pointer as function parameter instead o reference

    Qt Code:
    1. AsseNumerico::AsseNumerico(/*options*/,QPainter* painter,QWidget *parent):QWidget(parent),p(painter){};
    To copy to clipboard, switch view to plain text mode 

    Using copy constructor you force the creation of a copy object and destruction of it. Passing pointer you don't.
    A camel can go 14 days without drink,
    I can't!!!

  7. #7
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Qt Code:
    1. It seems that you delete memory twice. What happens in that AsseRiferimento destructor?
    To copy to clipboard, switch view to plain text mode 

    Nothing.
    I didn't wrote code for the destructor.
    should I?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Quote Originally Posted by vratojr
    Nothing.
    I didn't wrote code for the destructor.
    should I?
    No, you don't have to write it. So the problem is probably because you create a copy that QPainter. Try what mcosta suggests.

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    Quote Originally Posted by jacek
    So the problem is probably because you create a copy that QPainter.
    Qt Official Documentation says
    QPainter::QPainter ( QPaintDevice * pd )
    Constructs a painter that begins painting the paint device pd immediately.
    This constructor is convenient for short-lived painters, e.g. in a paint event and should be used only once.

    The constructor calls begin() for you and the QPainter destructor automatically calls end().
    So, you must use QPainter pointer.
    A camel can go 14 days without drink,
    I can't!!!

  10. #10
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing a QPainter object to widgets

    You were right, I resolved doing that way.

    Thank you very much boys!

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2008, 16:36
  2. Passing an object from one form to the other.
    By cbarmpar in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2008, 15:12
  3. passing an object
    By mickey in forum General Programming
    Replies: 3
    Last Post: 16th January 2008, 11:27
  4. Replies: 2
    Last Post: 17th May 2006, 22:01
  5. Passing Object to dll
    By ankurjain in forum Qt Programming
    Replies: 2
    Last Post: 1st April 2006, 10:50

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.