sacrif
20th January 2010, 12:23
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:
MainWindow::MainWindow()
{
tabWidget_ = new QTabWidget(); //tabWidget_ is a member of MainWindow
setCentralWidget(tabWidget_);
tabWidget_->addTab(new QWidget(), "Histograms");
.....
.....
gradientWidget_ = new QWidget(); //gradientWidget_ is a member of MainWindow
tabWidget_->addTab(gradientWidget_, "Gradient" );
Qpainter *painter_ = new QPainter();
QGradient gradient;
QPointF startPoint(10.0,10.0);
QPointF endPoint(50.0,50.0);
gradient = QLinearGradient(startPoint, endPoint);
gradient.setColorAt(0.2,QColor(0, 0, 255, 127));
gradient.setColorAt(0.5,QColor(255, 0, 0, 127));
gradient.setSpread(QGradient::PadSpread);
painter_->begin(gradientWidget_);
painter_->setBrush(gradient);
painter_->setPen(Qt::NoPen);
painter_->drawRect(gradientWidget_->rect());
}
Thanks for your help
Regards scr
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:
MainWindow::MainWindow()
{
tabWidget_ = new QTabWidget(); //tabWidget_ is a member of MainWindow
setCentralWidget(tabWidget_);
tabWidget_->addTab(new QWidget(), "Histograms");
.....
.....
gradientWidget_ = new QWidget(); //gradientWidget_ is a member of MainWindow
tabWidget_->addTab(gradientWidget_, "Gradient" );
Qpainter *painter_ = new QPainter();
QGradient gradient;
QPointF startPoint(10.0,10.0);
QPointF endPoint(50.0,50.0);
gradient = QLinearGradient(startPoint, endPoint);
gradient.setColorAt(0.2,QColor(0, 0, 255, 127));
gradient.setColorAt(0.5,QColor(255, 0, 0, 127));
gradient.setSpread(QGradient::PadSpread);
painter_->begin(gradientWidget_);
painter_->setBrush(gradient);
painter_->setPen(Qt::NoPen);
painter_->drawRect(gradientWidget_->rect());
}
Thanks for your help
Regards scr