The following are the steps and codes to implement a beautiful early warning instrument with Qt:
1. Create a Qt project and add a main window.
2. Add a QGraphicsView control to the main window for drawing warning instruments.
3. Create a QGraphicsScene object and set it as the scene of QGraphicsView.
The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓
QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
4. Create a QGraphicsEllipseItem object to represent the frame of the warning instrument.
QGraphicsEllipseItem *outerCircle = new QGraphicsEllipseItem(QRectF(-120,-120,240,240));
outerCircle->setPen(QPen(Qt::black, 4));
scene->addItem(outerCircle);
5. Create a QGraphicsEllipseItem object to represent the center point of the early warning instrument.
QGraphicsEllipseItem *centerCircle = new QGraphicsEllipseItem(QRectF(-15,-15,30,30));
centerCircle->setBrush(QBrush(Qt::red));
centerCircle->setPen(QPen(Qt::black, 2));
centerCircle->setZValue(1);
scene->addItem(centerCircle);
6. Create some QGraphicsLineItem objects to represent the tick marks of the warning instrument.
for (int i = 0; i < 12; i++) {
QGraphicsLineItem *line = new QGraphicsLineItem(-100, 0, -80, 0);
line->setPen(QPen(Qt::black, 3));
line->setRotation(i * 30);
line->setPos(0, 0);
scene->addItem(line);
}
7. Create a QGraphicsPolygonItem object to represent the pointer of the warning instrument.
QPolygonF polygon;
polygon << QPointF(-10, 0) << QPointF(0, -120) << QPointF(10, 0);
QGraphicsPolygonItem *pointer = new QGraphicsPolygonItem(polygon);
pointer->setBrush(QBrush(Qt::red));
pointer->setPen(QPen(Qt::black, 2));
pointer->setZValue(2);
pointer->setPos(0, 0);
scene->addItem(pointer);
8. Update the rotation angle of the pointer in the timer of the main window.
void MainWindow::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
int angle = ui->spinBox->value();
QGraphicsItem *item = ui->graphicsView->scene()->items().at(3);
item->setRotation(angle);
}
The complete code is as follows:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGraphicsScene>
#include <QGraphicsEllipseItem>
#include <QGraphicsPolygonItem>
#include <QGraphicsLineItem>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
void timerEvent(QTimerEvent *event);
private:
Ui::MainWindow *ui;
QGraphicsScene *scene;
QGraphicsEllipseItem *outerCircle;
QGraphicsEllipseItem *centerCircle;
QGraphicsPolygonItem *pointer;
QList<QGraphicsLineItem*> lines;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 创建场景
scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
// 创建外框
outerCircle = new QGraphicsEllipseItem(QRectF(-120,-120,240,240));
outerCircle->setPen(QPen(Qt::black, 4));
scene->addItem(outerCircle);
// 创建中心点
centerCircle = new QGraphicsEllipseItem(QRectF(-15,-15,30,30));
centerCircle->setBrush(QBrush(Qt::red));
centerCircle->setPen(QPen(Qt::black, 2));
centerCircle->setZValue(1);
scene->addItem(centerCircle);
// 创建指针
QPolygonF polygon;
polygon << QPointF(-10, 0) << QPointF(0, -120) << QPointF(10, 0);
pointer = new QGraphicsPolygonItem(polygon);
pointer->setBrush(QBrush(Qt::red));
pointer->setPen(QPen(Qt::black, 2));
pointer->setZValue(2);
pointer->setPos(0, 0);
pointer->setRotation(0);
scene->addItem(pointer);
// 创建刻度线
for (int i = 0; i < 12; i++) {
QGraphicsLineItem *line = new QGraphicsLineItem(-100, 0, -80, 0);
line->setPen(QPen(Qt::black, 3));
line->setRotation(i * 30);
line->setPos(0, 0);
lines.append(line);
scene->addItem(line);
}
// 启动定时器
startTimer(100);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerEvent(QTimerEvent *event)
{
Q_UNUSED(event);
int angle = ui->spinBox->value();
QGraphicsItem *item = ui->graphicsView->scene()->items().at(3);
pointer->setRotation(angle);
}
Use Qt to draw a beautiful early warning instrument with a three-dimensional effect, code, and describe the design steps
The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓