• 大小: 6KB
    文件类型: .zip
    金币: 2
    下载: 2 次
    发布日期: 2021-06-08
  • 语言: 其他
  • 标签: qt  画树  

资源简介

用qt的技术来实现画树的功能,主要使用QPainter painter(this);里面的方法来实现的画树功能。 思想是从最后的一列的叶子节点开始画,一直画到根节点。

资源截图

代码片段和文件信息

#include “DrawTree.h“
#include “Comm.h“

const QSize rect_size(16030);
const int Line_width(60);
const int Line_height(3);
const int padding_top(10);
const int padding_right(10);

const int height_interval(10);



DrawTree::DrawTree(QWidget *parent) : QWidget(parent)
{
    rect_wid_size = 3;
}

DrawTree::DrawTree(int rect_wid_size int rect_height_size QWidget *parent)
{
    this->rect_wid_size = rect_wid_size ;
    this->rect_height_size = rect_height_size ;
}


void DrawTree::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    // 反走样
    painter.setRenderHint(QPainter::Antialiasing true);
    painter.setBrush(Qt::NoBrush);
    // 设置字体:微软雅黑、点大小50、斜体
    QFont font;
    font.setFamily(“Microsoft YaHei“);
    font.setPointSize(10);
//    font.setItalic(true);
    painter.setFont(font);

//    drawXTree(&painter);
    drawXTree1(&painter);
}


void DrawTree::drawXTree(QPainter *painter)
{
    // 设置画笔颜色、宽度
    painter->setPen(QPen(QColor(0 160 230) 2));

    HS_Debug<<“width():  “<    int wid = width() - padding_right;
    HS_Debug<<“width:  “<    for(int i =0 ; i         int point_x = wid - rect_size.width()*(i+1) - Line_width*i;
        double point_line_y = padding_top + rect_size.height() /2;
        QRectF rect1 ( point_x padding_top rect_size.width()rect_size.height()   );

        painter->drawRect( rect1   );
        painter->drawText( rect1   Qt::AlignCenter “test:“+QString::number(i));
        // 绘制直线
        if( i            painter->drawLine(QPointF(point_x point_line_y) QPointF(point_x-Line_width point_line_y));
        }
    }


}

void DrawTree::drawXTree1(QPainter *painter)
{

    // 设置画笔颜色、宽度
    painter->setPen(QPen(QColor(0 160 230) 2));

    int widthRight = width() - padding_right;  //画图的最右边
    int heightTop =  padding_top; //画图的最上面

    int point_x = 0;
    int point_y = 0;
    double point_line_y = 0;
    QRectF rect1;
    for(int i =0 ; i         point_x = widthRight - rect_size.width()*(i+1) - Line_width*i;
        rect1 =  QRectF( point_x point_y rect_size.width()rect_size.height()   );

        if( 0 == i ){
            for( int j =0; j                point_y = heightTop+(height_interval+rect_size.height())*j;
                point_line_y = point_y + rect_size.height() /2;

                rect1 =  QRectF( point_x point_y rect_size.width()rect_size.height()   );
                painter->drawRect( rect1   );
                painter->drawText( rect1   Qt::AlignCenter “test:“+QString::number(i));
                painter->drawLine(QPointF(point_x point_line_y) QPointF(point_x-Line_width/2 point_line_y));
                if( j < rect_height_size -1){
                    painter->drawLine(QPointF(point_x-Line_width/2 point_line_y)
                                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         710  2017-09-13 15:27  Comm.h
     文件        3992  2017-09-13 17:40  DrawTree.cpp
     文件         613  2017-09-13 17:40  DrawTree.h
     文件         293  2017-09-13 17:40  main.cpp
     文件          77  2017-09-13 19:05  README.md
     文件         400  2017-09-13 15:23  testDrawTree.pro
     文件       23477  2017-09-13 14:25  testDrawTree.pro.user
     文件         225  2017-09-13 15:45  Widget.cpp
     文件         195  2017-09-13 14:25  Widget.h

评论

共有 条评论