Results 1 to 4 of 4

Thread: Problem with QPainter

  1. #1
    Join Date
    Jul 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with QPainter

    Hi,
    I was just beginning to use QPainter and this my sorce code :

    QLine line( 0, 10, 10, 10 );

    QPen pen( Qt::blue, 10 );

    QPainter painter;

    painter.begin(wave->device));

    painter.drawLine(line);


    wave->device is a pointer to a QPushButton object.
    painter.begin is returning false and at the application output i could see the statment

    QPainter::begin: Paint device returned engine == 0, type: 1

    I am unable to make out why my painter object is unable to begin
    Is there anything wrong with my source code.
    Please help.
    I am using Windows XP .

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with QPainter

    As far as i know QPainter can paint on widgets only within paintevent, something like this:
    Qt Code:
    1. void YourWidget::paintEvent(QPaintEvent *) {
    2. //your painting code goes here
    3. }
    To copy to clipboard, switch view to plain text mode 
    link to reference

  3. #3
    Join Date
    Jul 2010
    Posts
    16
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with QPainter

    I tried your suggestion by making a function in my class Waveform which has a member function device (ptr. to a QPushButton object) and i called this function from my MainWindow's constructors with no parameters.I got same problem once again i.e. application output showed the statement

    QPainter::begin: Paint device returned engine == 0, type: 1

    and begin function is returning false.

    Here is the code of the public member function paintEvent that i made in my class WaveForm

    void WaveForm:: paintEvent(QPaintEvent * event=0)
    {
    QLine line( 0, height() / 2.0, width(), height() / 2.0 );
    QPen pen( Qt::blue, 10 ); // red solid line, 1 pixels wide

    QPainter *painter=new QPainter();
    if(!painter->begin(device))
    qDebug()<<"Painter failed to begin";

    painter->drawLine(line);
    }

    Please HELP!!
    Last edited by vijay_kansal; 24th July 2010 at 12:43. Reason: spelling mistake

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Problem with QPainter

    You didn't understood me, the QPainter can paint to a widget from it's own paintEvent (so use that code in the paintEvent of widget you are trying to paint <your "device" paintEvent> not in your mainWindow)

    LE: You will call: painter->begin(this); //the pointer to the object itself

Similar Threads

  1. problem in QPainter::begin
    By wagmare in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2009, 10:31
  2. QPainter problem (a HW issue perhaps?)
    By jonks in forum Qt Programming
    Replies: 10
    Last Post: 10th June 2009, 03:05
  3. Qt 2.3.10 QPainter problem on arm
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 7th October 2008, 12:29
  4. QT 4.4.0 Upgrade QPainter Problem
    By ChrisReath in forum Qt Programming
    Replies: 4
    Last Post: 13th May 2008, 15:25
  5. Problem with QScrollview and QPainter
    By saida in forum Qt Programming
    Replies: 14
    Last Post: 7th March 2006, 05:14

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.