PDA

View Full Version : Problem in paintEvent()



Niamita
10th May 2011, 06:56
Hi Experts
I have problem in my painEvent() , the function is get called many times but get nothing draw on the widget.I understand why the function is gett called so much time but how to resolve this.

I am drawing a hexagon


void MainWindow::paintEvent(QPaintEvent *event )
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(Qt::black);
pen.setWidth(6);
painter.setPen(pen);

static const QPointF points[6] = {
QPointF(300.0, 160.0),//Top Line
QPointF(220.0, 160.0),//
//QPointF(300.0, 180.0),
QPointF(195.0, 210.0),
QPointF(220.0, 260.0),//
QPointF(300.0, 260.0),//Bottom Line
QPointF(325.0, 210.0)
};
painter.drawPolygon(points, 6);
qDebug() << "Cnt=" << cnt;
cnt++;

QPainter painter1(this);
QPen pen1(Qt::green);
painter1.setPen(pen1);
QLinearGradient grad1(300, 160, 325, 260);
QBrush brush1(grad1);
grad1.setColorAt(1.0, Qt::white);
painter1.setBrush(brush1);
QFont font("Times", 12);
painter1.setFont(font);
QPoint point1 = QPoint( 240, 225);
painter1.drawText( point1, text_str );
}

and



NewWindow::NewWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::NewWindow)
{
ui->setupUi(this);
QString string = "Gello";
w1 = new MainWindow(string ,this);
w1->show();
}


Please guide me i am trying to resolve this since 1 day.
Is i have to call paintEvent() manually but i read at a place there is no need to call it manually.
Thanks.
Waiting for your reply.

Lykurg
10th May 2011, 07:44
Hi,

please use the code tags next time to make your source code better readable. First there is no need to create two painters, simple reuse the first painter you created to draw the text. Second, I guess your paint works perfectly but only the widget is too small to see what you have painted or your ui file overpaint your main window paint event. As I see in your case you don't want to overwrite the main window paint event. Better create a stand alone widget, alter that and put in in your main window vie setCentralWidget or inside the ui file.

Best,
Lykurg

Niamita
10th May 2011, 09:14
Thanks for reply.

Yes you are right when i set my widget as centralwidget of window it appeares. But the problem is that i have to make saveral instances of the widget and then arrange all of them in a gridlayout.If i set widget as central widget then rest of them will not visible so how i should solve this problem.I have to create several instances at a time of widget.
Waiting for reply.
Thanks.

Lykurg
10th May 2011, 10:26
Then use the custom widget approach and use it where ever you like (promote to in designer for example)

Niamita
10th May 2011, 11:46
Ok
But can you tell me how i customize my painted button with Qt designer.
Thanks.