Results 1 to 5 of 5

Thread: Trouble with rendering to the widget

  1. #1
    Join Date
    Sep 2008
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trouble with rendering to the widget

    Hello forum,

    I am trying to render and see how different gradient types work and started with the linear gradient one.

    The folloowing code snippet only shows a blank window widget that i have declared.

    Qt Code:
    1. QApplication app(argc,argv);
    2.  
    3. QWidget window;
    4.  
    5. QPainter painter;
    6.  
    7. QLinearGradient gradient(0,0,100,100);
    8.  
    9. gradient.setColorAt(0,Qt::red);
    10. gradient.setColorAt(0.5,Qt::green);
    11. gradient.setColorAt(0,Qt::blue);
    12.  
    13. painter.setBrush(gradient);
    14.  
    15.  
    16. painter.drawRect(0,0,100,100);
    17.  
    18. window.show();
    19.  
    20.  
    21. return app.exec();
    To copy to clipboard, switch view to plain text mode 

    Any idea what i missed?


    Regards
    Sajjad
    Last edited by jpn; 28th September 2008 at 19:37. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with rendering to the widget

    Hello,

    It seems to me you created a painter that has nothing to do with the window, so it has no effect.

    Anyway, what you should do if you want to handle painting of the widget yourself is subclass QWidget and reimplement its paintEvent, otherwise even if you managed to draw into the window, its own paintEvent would redraw it.

    The "analog clock" example in Qt docs shows how to do it: http://doc.trolltech.com/4.2/widgets-analogclock.html

  3. The following user says thank you to hkvm for this useful post:

    dosto.walla (29th September 2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    21
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with rendering to the widget

    Hello,

    Thanks again for the hint!!

    Now i have another issue.


    I would like to have multiple viewport on the same device (may that be QWidget, QPixmap, QPicture , QImage or QPrinter). I am not sure though if that will be efficient way to do that .

    I attached an image where you will see three different shapes drawn with three
    different types of gradient at three different placements. Now if i change the size of the window those shapes do not increase or decrease by size proportionally.


    then i thought that having multiple viewport will be one of the solutions.

    Can you suggest anything better?



    Regards
    Sajjad
    Attached Images Attached Images

  5. #4
    Join Date
    Sep 2008
    Posts
    18
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Trouble with rendering to the widget

    To draw independently of the underlying device, you can simply do something like this:
    Qt Code:
    1. QPainter painter(this);
    2. painter.setWindow(QRect(0, 0, 100, 100));
    To copy to clipboard, switch view to plain text mode 

    Then you draw as if you had a window 100 x 100 pixels, and your drawing will be stretched to whatever size the window (or something else you're drawing to) actually has.

    You can read more about this in the docs:
    http://doc.trolltech.com/4.1/coordsy...ort-conversion
    It also says how to maintain the aspect-ratio for your drawing to prevent deformation when you resize the window so it's not square.

  6. #5
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Trouble with rendering to the widget

    Hi
    I would suggest using QGraphicsScene, QGraphicsView, QGraphicsItem. These classes were made for mulitple viewports. Check the examples/graphicsview directory.

Similar Threads

  1. Qt widget for graph rendering, like GrapViz
    By dennis714 in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2013, 21:35
  2. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  3. Problem rendering QImage from Phonon Widget
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 12th July 2008, 15:31
  4. Qwt/custom widget trouble?
    By Nick2463 in forum Qwt
    Replies: 6
    Last Post: 17th June 2008, 02:48
  5. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35

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.