这是一个使用Qt C++绘制白色三角形的示例代码:
在.h文件中添加以下内容:
#include <QWidget>
#include <QPainter>
class MyLabel : public QLabel {
Q_OBJECT
public:
explicit MyLabel(QWidget* parent = nullptr);
protected:
void paintEvent(QPaintEvent* event) override;
};
在.cpp文件中实现以下内容:
MyLabel::MyLabel(QWidget* parent)
: QLabel(parent) {}
void MyLabel::paintEvent(QPaintEvent* event) {
QLabel::paintEvent(event);
QPainter painter(this);
painter.setPen(Qt::NoPen);
painter.setBrush(Qt::white);
QPolygonF polygon;
polygon << QPointF(0, 0) << QPointF(width(), 0) << QPointF(width() / 2, height() / 2);
painter.drawPolygon(polygon);
}
然后,将标签设置为MyLabel即可:
MyLabel* label = new MyLabel();
label->setGeometry(100, 100, 200, 200);
label->show();