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