这里写自定义目录标题
利用Qt的paint绘制水印层
绘制使用了Qt的PaintEvent事件
void CLS_Watermark::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen;
pen.setBrush(QBrush(m_color));
painter.setPen(pen);
QFont font;
font.setFamily(m_qstrFamily);
font.setPixelSize(m_iFontSize);
painter.setFont(font);
painter.translate(width()/2,height()/2);
painter.rotate(m_iRotate);
//计算要画的水印的宽和高
int iTxtW = 0;
int iTxtH = 0;
foreach (QString _qstrTxt, m_qstrTxtList)
{
QFontMetrics fm(font);
iTxtW = qMax(fm.width(_qstrTxt), iTxtW);
iTxtH = qMax(fm.height(), iTxtH);
}
QRect qrectThis = rect();
painter.translate(qrectThis.topLeft());
int iColCount = qMax(qrectThis.width() / iTxtW, CONST_Height_Space);
int iRowCount = qMax(qrectThis.height() / iTxtH, CONST_Height_Space);
int iStartX = qrectThis.x() - qrectThis.width();
int iStartY = qrectThis.y() - qrectThis.height();
for (int i = 0; i < iRowCount; ++i)
{
for (int j = 0; j < iColCount; ++j)
{
foreach (QString _qstrContent, m_qstrTxtList)
{
QPoint qpointOrig = QPoint(iStartX + j * (iTxtW + m_uiSpaceW), iStartY + i * (iTxtH + m_uiSpaceH));
QRect qrect(qpointOrig, QSize(iTxtW, iTxtH));
painter.drawText(qrect, _qstrContent);
}
}
}
}
}
解析
注意点:
- 需要有一个画板;
- 水印可能是倾斜的、再考虑到其位置时先以水平设计,然后再去旋转;
- 可以通过Qt的接口给水印的内容设置字体、颜色、大小等属性;