QPainter
1.绘制文字
2.绘制线
3.绘制图形
4.绘制融合图片
绘制渲染的信号槽
1.void paintEvent(QPaintEvent *ev);
2.update();
ui_widget.h
#ifndef UI_WIDGET_H
#define UI_WIDGET_H
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QFontComboBox>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_Widget
{
public:
QFontComboBox *fontComboBox;
void setupUi(QWidget *Widget)
{
if (Widget->objectName().isEmpty())
Widget->setObjectName(QString::fromUtf8("Widget"));
Widget->resize(800, 600);
fontComboBox = new QFontComboBox(Widget);
fontComboBox->setObjectName(QString::fromUtf8("fontComboBox"));
fontComboBox->setGeometry(QRect(540, 40, 228, 25));
retranslateUi(Widget);
QObject::connect(fontComboBox, &QFontComboBox::currentFontChanged, Widget, qOverload<>(&QWidget::update));
QMetaObject::connectSlotsByName(Widget);
} // setupUi
void retranslateUi(QWidget *Widget)
{
Widget->setWindowTitle(QCoreApplication::translate("Widget", "Widget", nullptr));
} // retranslateUi
};
namespace Ui {
class Widget: public Ui_Widget {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
void Widget::paintEvent(QPaintEvent *ev)
{
//设置绘制设备
QPainter p(this);
//p.begin(this);
//设置画笔的颜色
p.setPen(QColor(255,0,0,150));
//设置字体
//p.setFont(QFont(QStringLiteral("黑体"),30));
QFont font = ui->fontComboBox->currentFont();
font.setPixelSize(30);
p.setFont(font);
p.drawText(100,100,QStringLiteral("测试的显示文字"));
}
Widget::~Widget()
{
delete ui;
}