View Full Version : how to draw a line over a frame?
vinayaka
17th August 2011, 06:20
Hai,
I am doing a project which needs a Qpainter to draw line but Qpainter donot work when we apply a style sheet to the frame where we have to draw the line
stampede
17th August 2011, 06:55
This works ok:
class Frame : public QFrame{
Q_OBJECT
public:
Frame( QWidget * parent = NULL ) : QFrame(parent){
setStyleSheet( "Frame{ background: blue }" );
}
protected:
void paintEvent( QPaintEvent * event ){
QFrame::paintEvent(event);
QPainter p(this);
p.drawLine( rect().topLeft(), rect().bottomRight() );
p.drawText( rect().center(), "works!" );
}
};
athulms
17th August 2011, 08:17
can you provide me with an example. pls
vishnu717
17th August 2011, 08:20
Since iam using the form window , i cannot apply this code , please give me an Qt example of an frame with Style sheet(background image) and Qpainter drawing lines on it
vinayaka
17th August 2011, 08:37
thanx for your help but it doesnt work and gives errors while we run it. can you please give some more details
ChrisW67
17th August 2011, 08:40
You only have to change one line... clearly too hard:
#include <QtGui>
#include <QDebug>
class Frame : public QFrame{
Q_OBJECT
public:
Frame( QWidget * parent = NULL ) : QFrame(parent){
// setStyleSheet( "Frame{ background: blue }" );
setStyleSheet( "Frame{ background-image: url(plasma.png); }" );
}
protected:
void paintEvent( QPaintEvent * event ){
QFrame::paintEvent(event);
QPainter p(this);
p.drawLine( rect().topLeft(), rect().bottomRight() );
p.drawText( rect().center(), "works!" );
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Frame m;
m.show();
return app.exec();
}
#include "main.moc"
and it looks like:
6770
with a 64x64 tile background.
If you do not share the errors then you cannot expect corrections.
wysota
17th August 2011, 08:44
Since iam using the form window , i cannot apply this code
Why is that?
stampede
17th August 2011, 09:09
can you provide me with an example. pls
I already did.
thanx for your help but it doesnt work and gives errors while we run it. can you please give some more details
What errors ? It seems to work ok for ChrisW67.
athulms
17th August 2011, 11:31
in my progrm i am using 4frames(UI). I use show/hide frames using buttons. I have two draw a line in the 3rd frame. I have used the code
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
QPen linepen(Qt::red);
linepen.setWidth(25);
linepen.setCapStyle(Qt::RoundCap);
painter.setRenderHint(QPainter::Antialiasing,true) ;
painter.setPen(linepen);
painter.drawLine(point,point2);
}
since i have used the painter(this) the drawing is done on the main window so i cannot see the line on the frame. since frames(with background images) are placed over the main window.
Now i try to use painter(ui->frame3)
But it doesn't work. pls help me, thanks in advance
stampede
17th August 2011, 12:13
Asked here (http://www.qtcentre.org/threads/43965-Qpainter-donot-work) already. What part of the sample code you don't understand ?
ChrisW67
17th August 2011, 23:56
The (default) painting of the child will happen after the painting of the parent. If you override the paint event of the parent then why do you expect that the painting behaviour of a child widget will be changed? You want to override the paint event of the child widget.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.