当前位置:网站首页>Getting started with qpainter: drawing the chess interface
Getting started with qpainter: drawing the chess interface
2022-06-30 00:03:00 【Autodesk_ Glodon】
1、 design sketch
Figure 1 shows the optimized interface ; Figure 2 is pure QPainter draw ; Later, I will finish the game and release it .


2、 Realize the idea
It was originally planned that all interfaces would use Qpainter Class is drawn , However, it was later found that the drawn interface was not as good-looking as the picture background , Therefore, the background picture chessboard method is adopted , Chess pieces are drawn using drawing classes .
2.1 UI Design 
The interface design is relatively simple , It only took QWidget And a few tab buttons .
2.2 Source code
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui {
class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
// QWidget interface
protected:
virtual void paintEvent(QPaintEvent *event);
private slots:
void on_pushButton_clicked();
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#pragma execution_character_set("utf-8")
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
}
Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
// Enable anti aliasing ( Anti aliasing )
painter.setRenderHint(QPainter::Antialiasing, true);
QRect uiRect = ui->qipanwidget->geometry();
// Specify the picture to draw ( Replace the picture path with a valid picture path )
painter.drawPixmap(uiRect,QPixmap(":/images/qipan.png"));
int nHOffSet = 66; // The margin from the left interface
int nVOffSet = 66; // Distance from the edge of the upper interface
int nDistance = 74; // Spacing is 70px
int nRadio = 36;
// Draw a circle
QBrush brush(QColor(211,130,44));
painter.setBrush(brush);
painter.drawEllipse(QPoint(uiRect.left()+nHOffSet,uiRect.top()+nVOffSet), nRadio, nRadio);
painter.drawEllipse(QPoint(uiRect.left()+nHOffSet + nDistance,uiRect.top()+nVOffSet), nRadio, nRadio);
painter.setBrush(brush);
painter.drawEllipse(QPoint(uiRect.left()+nHOffSet,uiRect.top()+nVOffSet+9*nDistance-10), nRadio, nRadio); // The pixels of the chessboard picture are not accurate , therefore -10 To correct the deviation
painter.drawEllipse(QPoint(uiRect.left()+nHOffSet + nDistance,uiRect.top()+nVOffSet+9*nDistance-10), nRadio, nRadio);
// Draw the Chinese characters inside the black square circle
{
painter.setPen(QColor(0, 0, 0));
painter.setFont(QFont(" Chinese Xingkai ", nRadio-3, 700));
QRect textRec;
textRec.setTopLeft(QPoint(uiRect.left()+nHOffSet-nRadio,uiRect.top()+nVOffSet-nRadio));
textRec.setBottomRight(QPoint(uiRect.left()+nHOffSet+nRadio,uiRect.top()+nVOffSet+nRadio));
painter.drawText(textRec, " Car ", QTextOption(Qt::AlignCenter));
textRec.setTopLeft(QPoint(uiRect.left()+nHOffSet-nRadio+ nDistance,uiRect.top()+nVOffSet-nRadio));
textRec.setBottomRight(QPoint(uiRect.left()+nHOffSet+nRadio+ nDistance,uiRect.top()+nVOffSet+nRadio));
painter.drawText(textRec, " Horse ", QTextOption(Qt::AlignCenter));
}
// Draw the Chinese characters inside the red square circle
{
painter.setPen(QColor(255, 0, 0));
painter.setFont(QFont(" Chinese Xingkai ", nRadio-3, 700));
QRect textRec;
textRec.setTopLeft(QPoint(uiRect.left()+nHOffSet-nRadio,uiRect.top()+nVOffSet-nRadio+9*nDistance-10));
textRec.setBottomRight(QPoint(uiRect.left()+nHOffSet+nRadio,uiRect.top()+nVOffSet+nRadio+9*nDistance-10));
painter.drawText(textRec, " Car ", QTextOption(Qt::AlignCenter));
textRec.setTopLeft(QPoint(uiRect.left()+nHOffSet-nRadio+ nDistance,uiRect.top()+nVOffSet-nRadio+9*nDistance-10));
textRec.setBottomRight(QPoint(uiRect.left()+nHOffSet+nRadio+ nDistance,uiRect.top()+nVOffSet+nRadio+9*nDistance-10));
painter.drawText(textRec, " Horse ", QTextOption(Qt::AlignCenter));
}
}
void Widget::on_pushButton_clicked()
{
close();
}
边栏推荐
- Sword finger offer 14- I. cut rope
- 除子
- LC: effective Sudoku + rotating image
- 这个简单的小功能,半年为我们产研团队省下213个小时
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
- QT learning 02 GUI program example analysis
- JS绘制极坐标颜色渐变
- MySQL multi table query
- 333333333333333333333333333333
猜你喜欢

Andorid source build/envsetup.sh 该知道的细节

Use of jetpack's room in combination with flow

Basic tutorial for installing monggodb in win10

西门子低代码 9.14版本: 满足不同需求
![[译]在软件开发行业工作 6 年后,那些年我曾改过的观念](/img/69/c9829b14223f1e8fd3137d0750d8f7.png)
[译]在软件开发行业工作 6 年后,那些年我曾改过的观念

【微信小程序】认识小程序项目的基本组成结构

HPE launched ARM CPU general server product

333333333333333333333333333333

Binary search tree 230 The element with the smallest K in the binary search tree 1038 From binary search tree to larger sum tree

代码分析平台 SonarQube 实战
随机推荐
大厂试水 HPE推出Arm CPU通用服务器产品
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
Siemens low code version 9.14: meet different needs
Copy linked list with random pointer [space for time --hash record]
Solr基础操作6
Sword finger offer 14- I. cut rope
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility
Basic operations such as MySQL startup under Windows platform
Cacti settings for spin polling
招商证券靠谱吗?开股票账户安全吗?
LC:最大子数组和
xutils3传集合
云和恩墨盖国强,识别它、抓住它,在国产数据库沸腾以前
Koa2 learning and using
AI empowers new retail, the way to win "wisdom" lies in ecological thinking | selected excerpts from digital intelligence night talk live broadcast
手机开户后多久才能通过?另外,手机开户安全么?
QT learning 02 GUI program example analysis
复制带随机指针的链表[空间换时间--hash记录]
克隆无向图[bfs访问每条边而不止节点]