.h文件
# 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 ( ) ;
void Draw_Element ( QPainter * p, QPoint point, float height) ;
void Draw_Level_One ( QPainter * p, QPoint point, float height) ;
void Draw_Level_Two ( QPainter * p, QPoint point, float height) ;
void Draw_Level_Third ( QPainter * p, QPoint point, float height) ;
void Draw_Level_Four ( QPainter * p, QPoint point, float height) ;
void Draw_Level_Five ( QPainter * p, QPoint point, float height) ;
private :
Ui:: Widget * ui;
protected :
virtual void paintEvent ( QPaintEvent * event) override ;
} ;
# endif
.cpp文件
# include "widget.h"
# include "ui_widget.h"
# include <QPaintEvent>
# include <QPainter>
# include <QPoint>
# include <QtMath>
Widget :: Widget ( QWidget * parent)
: QWidget ( parent)
, ui ( new Ui:: Widget)
{
ui-> setupUi ( this ) ;
}
Widget :: ~ Widget ( )
{
delete ui;
}
void Widget :: Draw_Element ( QPainter* p, QPoint point, float height)
{
QPoint point_1 = point;
QPoint point_2 = QPoint ( point_1. x ( ) - ( 2 * height) / qSqrt ( 3 ) , point_1. y ( ) + height) ;
QPoint point_3 = QPoint ( point_1. x ( ) + ( 2 * height) / qSqrt ( 3 ) , point_1. y ( ) + height) ;
p-> drawLine ( point_1, point_2) ;
p-> drawLine ( point_2, point_3) ;
p-> drawLine ( point_3, point_1) ;
}
void Widget :: Draw_Level_One ( QPainter* p, QPoint point, float height)
{
Draw_Element ( p, point, height) ;
QPoint point_1 = QPoint ( point. x ( ) - height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_2 = QPoint ( point. x ( ) + height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_3 = QPoint ( point. x ( ) , point. y ( ) + height) ;
p-> drawLine ( point_1, point_2) ;
p-> drawLine ( point_2, point_3) ;
p-> drawLine ( point_3, point_2) ;
p-> drawLine ( point_3, point_1) ;
}
void Widget :: Draw_Level_Two ( QPainter* p, QPoint point, float height)
{
Draw_Element ( p, point, height) ;
QPoint point_1 = QPoint ( point. x ( ) - height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_2 = QPoint ( point. x ( ) + height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_3 = QPoint ( point. x ( ) , point. y ( ) + height) ;
Draw_Level_One ( p, point, height / 2 ) ;
Draw_Level_One ( p, point_1, height / 2 ) ;
Draw_Level_One ( p, point_2, height / 2 ) ;
}
void Widget :: Draw_Level_Third ( QPainter* p, QPoint point, float height)
{
Draw_Element ( p, point, height) ;
QPoint point_1 = QPoint ( point. x ( ) - height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_2 = QPoint ( point. x ( ) + height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_3 = QPoint ( point. x ( ) , point. y ( ) + height) ;
Draw_Level_Two ( p, point, height / 2 ) ;
Draw_Level_Two ( p, point_1, height / 2 ) ;
Draw_Level_Two ( p, point_2, height / 2 ) ;
}
void Widget :: Draw_Level_Four ( QPainter* p, QPoint point, float height)
{
Draw_Element ( p, point, height) ;
QPoint point_1 = QPoint ( point. x ( ) - height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_2 = QPoint ( point. x ( ) + height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_3 = QPoint ( point. x ( ) , point. y ( ) + height) ;
Draw_Level_Third ( p, point, height / 2 ) ;
Draw_Level_Third ( p, point_1, height / 2 ) ;
Draw_Level_Third ( p, point_2, height / 2 ) ;
}
void Widget :: Draw_Level_Five ( QPainter* p, QPoint point, float height)
{
Draw_Element ( p, point, height) ;
QPoint point_1 = QPoint ( point. x ( ) - height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_2 = QPoint ( point. x ( ) + height / qSqrt ( 3 ) , point. y ( ) + height / 2 ) ;
QPoint point_3 = QPoint ( point. x ( ) , point. y ( ) + height) ;
Draw_Level_Four ( p, point, height / 2 ) ;
Draw_Level_Four ( p, point_1, height / 2 ) ;
Draw_Level_Four ( p, point_2, height / 2 ) ;
}
void Widget :: paintEvent ( QPaintEvent * event)
{
QPainter painter ( this ) ;
painter. setPen ( Qt:: blue) ;
painter. setRenderHint ( QPainter:: Antialiasing, true ) ;
float element_hight = 100 ;
int init_point_x = 120 ;
float init_point_y = 40 ;
Draw_Element ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
init_point_x = 360 ;
Draw_Level_One ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
init_point_x = 600 ;
Draw_Level_Two ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
init_point_x = 850 ;
Draw_Level_Third ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
init_point_x = 1100 ;
Draw_Level_Four ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
init_point_x = 1350 ;
Draw_Level_Five ( & painter, QPoint ( init_point_x, init_point_y) , element_hight) ;
}