PDA

View Full Version : Simple QtPainter problem



rishid
5th February 2008, 21:50
Just trying to use a QPainter to draw a line across my widget. I thought it would be easy, just not working. Here is the entire widget code.

Thanks.



#ifndef TOPBAR_H_
#define TOPBAR_H_

#include <QtGui/QWidget>
#include <QPainter>
#include "ui_TopBar.h"

class TopBar : public QWidget, private Ui::TopBarClass {
Q_OBJECT

public:
TopBar(QWidget *parent = 0) :
QWidget(parent) {
setupUi(this);

QPainter painter(this);
painter.drawLine(0,21,240,21);
}
};

#endif /*TOPBAR_H_*/

marcel
5th February 2008, 21:55
You can draw on widgets only in their paint events.
So, override paintEvent for your widget and do all the drawing in there.
Don't forget to call the base class version if you want the widget to draw it's default contents.

rishid
5th February 2008, 21:57
Yea I JUST found this too.

They should list that in the QPainter documentation too.


http://doc.trolltech.com/4.3/porting4.html#painting-and-redrawing-widgets

jpn
5th February 2008, 22:00
They should list that in the QPainter documentation too.
It's there, right in the beginning of the detailed description. ;)