Results 1 to 5 of 5

Thread: Problems with painting a Qgradient

  1. #1

    Default Problems with painting a Qgradient

    Hey everyone!

    I am using QT 4.5 on windows 7 and having problems with painting a qgradient.

    I only have a MainWindow class (drived form the QMainWIndow). In the constructor I create a tabwidget which I set as the central widget. Then I append one widget to each of the 2 tabs. And I want to fill the widget of the second tab with a QGradient.

    Actually it should be pretty easy, but I keep getting the following errors when executing the program (I see the mainwindow with both tabs, but the second tab is not filled with the gradient):
    - QPainter::begin: Paint device returned engine == 0, type1
    - QPainter::setBrush: Painter not active
    - QPainter::setPen: Painter not active
    - QPainter::drawRects: Painter not active

    As far as I found out yet the first error means that the is QWidget outside of its PaintEvent. But unfortunately I don't really know what that means and especially what I should change to make it work.

    Here is the code:

    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3.  
    4. tabWidget_ = new QTabWidget(); //tabWidget_ is a member of MainWindow
    5. setCentralWidget(tabWidget_);
    6. tabWidget_->addTab(new QWidget(), "Histograms");
    7. .....
    8. .....
    9.  
    10. gradientWidget_ = new QWidget(); //gradientWidget_ is a member of MainWindow
    11. tabWidget_->addTab(gradientWidget_, "Gradient" );
    12.  
    13. Qpainter *painter_ = new QPainter();
    14.  
    15. QGradient gradient;
    16. QPointF startPoint(10.0,10.0);
    17. QPointF endPoint(50.0,50.0);
    18. gradient = QLinearGradient(startPoint, endPoint);
    19.  
    20. gradient.setColorAt(0.2,QColor(0, 0, 255, 127));
    21. gradient.setColorAt(0.5,QColor(255, 0, 0, 127));
    22.  
    23. gradient.setSpread(QGradient::PadSpread);
    24. painter_->begin(gradientWidget_);
    25. painter_->setBrush(gradient);
    26. painter_->setPen(Qt::NoPen);
    27. painter_->drawRect(gradientWidget_->rect());
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 


    Thanks for your help
    Regards scr

  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: Problems with painting a Qgradient

    That's not the right way to do it. Subclass QWidget and there, use the paint event and do your drawings. Then you can use that subclassed widget in your main window constructor.

  3. #3

    Default Re: Problems with painting a Qgradient

    I created a subclass of QWidget now and added the paint method to it. But when I execute the code I still get the same error msges as above. I assume that its because I dont properly use a paintevent. Could that be the reason?
    In the description of the QPaintEvent it says "Paint events are sent to widgets that need to update themselves", but how and where can I send this event?
    Is the principal approach on how I use the painter and gradient correct or am I on a entirely wrong way?

    Here the current code:

    Qt Code:
    1. //Maindwindow constructor
    2. MainWindow::MainWindow()
    3. {
    4. gw_ = new GradientWidget(this); //gw_ is a member of the MainWindow class
    5. setCentralWidget(gw_);
    6. QPainter *painter_ = new QPainter();
    7. gw_->paint(painter_);
    8. update();
    9. }
    10.  
    11. //Gradientwidget header
    12. class GradientWidget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. GradientWidget(QWidget *parent);
    17. void paint(QPainter *painter_ );
    18. };
    19.  
    20. //Gradientwidget constructor
    21. GradientWidget::GradientWidget(QWidget *parent)
    22. : QWidget(parent)
    23. {
    24. //nothing atm
    25. }
    26.  
    27. //Gradientwidget paint method
    28. void GradientWidget::paint(QPainter *painter_)
    29. {
    30.  
    31. QGradient gradient;
    32. QPointF startPoint(10.0,10.0);
    33. QPointF endPoint(50.0,50.0);
    34. gradient = QLinearGradient(startPoint, endPoint);
    35.  
    36. gradient.setColorAt(0.2,QColor(0, 0, 255, 127));
    37. gradient.setColorAt(0.5,QColor(255, 0, 0, 127));
    38.  
    39. gradient.setSpread(QGradient::PadSpread);
    40. painter_->begin(this);
    41. painter_->setBrush(gradient);
    42. painter_->setPen(Qt::NoPen);
    43. painter_->drawRect(rect());
    44. update();
    45. }
    To copy to clipboard, switch view to plain text mode 

    By the way: I try to follow the "../demos/gradients" example provided in QT. But it seems to be too complex for me to understand since I am a beginner. Is there probably an easier example on that topic?

  4. #4
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with painting a Qgradient

    To begin with: remove lines 6, 7, 8 from the constructor.

    All painting has to be done in the paint() method.
    Also, remove the update() call from the paint() method, because now update() calls paint() from within paint(). You don't want that.

  5. #5
    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: Problems with painting a Qgradient

    Small note: painter_->begin(this); is also not necessary.

Similar Threads

  1. errors
    By manu in forum Newbie
    Replies: 4
    Last Post: 16th June 2008, 12:29
  2. color table with QGradient and QImage
    By desch in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2008, 11:32
  3. Errors
    By hgedek in forum Newbie
    Replies: 7
    Last Post: 26th November 2007, 00:21
  4. Example with errors
    By fahmi in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2007, 02:37
  5. About painting
    By Pang in forum Qt Programming
    Replies: 3
    Last Post: 28th March 2007, 18:21

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.