Results 1 to 5 of 5

Thread: Problem in paintEvent()

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Problem in paintEvent()

    Hi Experts
    I have problem in my painEvent() , the function is get called many times but get nothing draw on the widget.I understand why the function is gett called so much time but how to resolve this.

    I am drawing a hexagon

    Qt Code:
    1. void MainWindow::paintEvent(QPaintEvent *event )
    2. {
    3. QPainter painter(this);
    4. painter.setRenderHint(QPainter::Antialiasing);
    5. QPen pen(Qt::black);
    6. pen.setWidth(6);
    7. painter.setPen(pen);
    8.  
    9. static const QPointF points[6] = {
    10. QPointF(300.0, 160.0),//Top Line
    11. QPointF(220.0, 160.0),//
    12. //QPointF(300.0, 180.0),
    13. QPointF(195.0, 210.0),
    14. QPointF(220.0, 260.0),//
    15. QPointF(300.0, 260.0),//Bottom Line
    16. QPointF(325.0, 210.0)
    17. };
    18. painter.drawPolygon(points, 6);
    19. qDebug() << "Cnt=" << cnt;
    20. cnt++;
    21.  
    22. QPainter painter1(this);
    23. QPen pen1(Qt::green);
    24. painter1.setPen(pen1);
    25. QLinearGradient grad1(300, 160, 325, 260);
    26. QBrush brush1(grad1);
    27. grad1.setColorAt(1.0, Qt::white);
    28. painter1.setBrush(brush1);
    29. QFont font("Times", 12);
    30. painter1.setFont(font);
    31. QPoint point1 = QPoint( 240, 225);
    32. painter1.drawText( point1, text_str );
    33. }
    To copy to clipboard, switch view to plain text mode 

    and


    Qt Code:
    1. NewWindow::NewWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::NewWindow)
    4. {
    5. ui->setupUi(this);
    6. QString string = "Gello";
    7. w1 = new MainWindow(string ,this);
    8. w1->show();
    9. }
    To copy to clipboard, switch view to plain text mode 


    Please guide me i am trying to resolve this since 1 day.
    Is i have to call paintEvent() manually but i read at a place there is no need to call it manually.
    Thanks.
    Waiting for your reply.
    Last edited by Lykurg; 10th May 2011 at 06:40.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem in paintEvent()

    Hi,

    please use the code tags next time to make your source code better readable. First there is no need to create two painters, simple reuse the first painter you created to draw the text. Second, I guess your paint works perfectly but only the widget is too small to see what you have painted or your ui file overpaint your main window paint event. As I see in your case you don't want to overwrite the main window paint event. Better create a stand alone widget, alter that and put in in your main window vie setCentralWidget or inside the ui file.

    Best,
    Lykurg

  3. #3
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in paintEvent()

    Thanks for reply.

    Yes you are right when i set my widget as centralwidget of window it appeares. But the problem is that i have to make saveral instances of the widget and then arrange all of them in a gridlayout.If i set widget as central widget then rest of them will not visible so how i should solve this problem.I have to create several instances at a time of widget.
    Waiting for reply.
    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Problem in paintEvent()

    Then use the custom widget approach and use it where ever you like (promote to in designer for example)

  5. #5
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem in paintEvent()

    Ok
    But can you tell me how i customize my painted button with Qt designer.
    Thanks.

Similar Threads

  1. paintEvent method problem
    By zgulser in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 12:32
  2. paintEvent problem
    By di_zou in forum Newbie
    Replies: 5
    Last Post: 2nd September 2009, 16:13
  3. Problem with QListWidget and paintEvent()
    By joksi in forum Qt Programming
    Replies: 2
    Last Post: 29th May 2009, 10:50
  4. paintEvent problem
    By anafor2004 in forum Newbie
    Replies: 1
    Last Post: 29th June 2008, 14:18
  5. paintEvent problem
    By MoaLaiSkirulais in forum Qt Programming
    Replies: 5
    Last Post: 12th February 2008, 13:59

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.