qt 实现画树功能

先上图片 

qt 实现画树功能_第1张图片


贴上主要的代码

    // 设置画笔颜色、宽度
    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 <rect_wid_size; ++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<rect_height_size; ++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),
                                      QPointF(point_x-Line_width/2, point_line_y+rect_size.height()+height_interval));
                }
            }
            point_line_y = (point_line_y - padding_top - rect_size.height()/2 ) /2 + padding_top+rect_size.height()/2;
            painter->drawLine(QPointF(point_x-Line_width/2, point_line_y), QPointF(point_x-Line_width, point_line_y));
        }else{
            rect1 =  QRectF( point_x, point_line_y - rect_size.height()/2, rect_size.width(),rect_size.height()   );
            painter->drawRect( rect1   );
            painter->drawText( rect1 ,  Qt::AlignCenter, "test:"+QString::number(i));
            // 绘制直线
            if( i<rect_wid_size-1 ){
                painter->drawLine(QPointF(point_x, point_line_y), QPointF(point_x-Line_width, point_line_y));
            }
        }

全部代码见: http://download.csdn.net/download/chen136258/9977286






你可能感兴趣的:(qt)