Results 1 to 5 of 5

Thread: draw points on mouse click shows error painter not active.

  1. #1
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default draw points on mouse click shows error painter not active.

    void MainWindow::mousePressEvent(QMouseEvent *e)
    {
    if(e->button()==Qt::LeftButton)
    {
    QPoint point = e->pos();
    int x=point.rx();
    int y=point.ry();
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setWidth(30);
    linepen.setCapStyle(Qt::RoundCap);
    painter.setRenderHint(QPainter::Antialiasing,true) ;
    painter.setPen(linepen);
    painter.drawPoint(x,y);
    }
    }


    What is the error in the code. Waiting for rply.

  2. #2
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: draw points on mouse click shows error painter not active.

    The reason of the error is using QPainter not inside paintEvent().Move the drawing to the paintEvent(),here in mousePressEvent just save points to draw on some list or so.

  3. #3
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: draw points on mouse click shows error painter not active.

    void MainWindow:aintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setWidth(30);
    linepen.setCapStyle(Qt::RoundCap);
    painter.setRenderHint(QPainter::Antialiasing,true) ;
    painter.setPen(linepen);
    }


    void MainWindow::mousePressEvent(QMouseEvent *e)
    {
    if(e->button()==Qt::LeftButton)
    {
    QPoint point = e->pos();
    int x=point.rx();
    int y=point.ry();
    QPainter painter(this);
    painter.drawPoint(x,y);
    }
    }


    This also shows the same error

  4. #4
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: draw points on mouse click shows error painter not active.

    Of course it shows the same error,as you have not corrected it
    Quote Originally Posted by athulms View Post
    void MainWindow::mousePressEvent(QMouseEvent *e)
    {
    if(e->button()==Qt::LeftButton)
    {
    QPoint point = e->pos();
    int x=point.rx();
    int y=point.ry();
    QPainter painter(this);//this is fckig wrong.QPainter instances may not be created outside the paintEvent
    painter.drawPoint(x,y);
    }
    }

  5. #5
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: draw points on mouse click shows error painter not active.

    ya i have used the update(); method. now it worked

Similar Threads

  1. Painter not active
    By bainedyal in forum Qt Programming
    Replies: 3
    Last Post: 4th August 2011, 10:06
  2. How to draw arc between two points.
    By nikhil in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2011, 13:55
  3. painter's drawPie() function how to draw few sensible PIEZ..
    By salmanmanekia in forum Qt Programming
    Replies: 15
    Last Post: 31st August 2008, 16:52
  4. use Qpainter to draw lines + problem in my painter
    By ReSu in forum Qt Programming
    Replies: 4
    Last Post: 5th March 2008, 15:44
  5. Painter not active!
    By Caius Aérobus in forum Qt Programming
    Replies: 7
    Last Post: 30th March 2006, 15:44

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.