PDA

View Full Version : Problems with painting a Qgradient



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

Lykurg
20th January 2010, 12:44
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.

sacrif
20th January 2010, 14:21
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:


//Maindwindow constructor
MainWindow::MainWindow()
{
gw_ = new GradientWidget(this); //gw_ is a member of the MainWindow class
setCentralWidget(gw_);
QPainter *painter_ = new QPainter();
gw_->paint(painter_);
update();
}

//Gradientwidget header
class GradientWidget : public QWidget
{
Q_OBJECT
public:
GradientWidget(QWidget *parent);
void paint(QPainter *painter_ );
};

//Gradientwidget constructor
GradientWidget::GradientWidget(QWidget *parent)
: QWidget(parent)
{
//nothing atm
}

//Gradientwidget paint method
void GradientWidget::paint(QPainter *painter_)
{

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(this);
painter_->setBrush(gradient);
painter_->setPen(Qt::NoPen);
painter_->drawRect(rect());
update();
}

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?

boudie
20th January 2010, 14:31
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.

Lykurg
20th January 2010, 16:27
Small note: painter_->begin(this); is also not necessary.