【原创】paintEvent()函数显示文本

时间:2023-03-08 18:56:49
【原创】paintEvent()函数显示文本

【代码】

void MainWindow::paintEvent(QPaintEvent*)

{

QPainter p(this);

QRect r;

p.setPen(Qt::red);

p.drawText(20, 80, 120, 20, 0, "Hello World!", &r);

//

}

【结果】

【原创】paintEvent()函数显示文本

QPoint 点的位置坐标

QPoint::QPoint(int xpos, int ypos)

Constructs a point with the given coordinates (xpos, ypos).

See also setX() and setY().

[static] int QPoint::dotProduct(const QPoint &p1, const QPoint &p2)

示例:

QPoint p( 3, 7);

QPoint q(-1, 4);

int lengthSquared = QPoint::dotProduct(p, q);       // lengthSquared becomes 25

Returns the dot product of p1 and p2.     返回值,点 p1,p2 的距离

【拓展阅读】

1、paintEvent 实现绘制钟表的官方例子   http://blog.csdn.net/ljt350740378/article/details/51611975

2、PaintEvent 绘制窗体背景图片       http://blog.csdn.net/lcalqf/article/details/50662433

QPixmap m_pBg;

m_pBg.load("Demo_bg.png");

void CDemoWnd::paintEvent(QPaintEvent* pEvent)

{

QPainter painter;

painter.begin(this);

painter.drawPixmap(rect(),m_pBg);

painter.end();

}