• 4798阅读
  • 6回复

【提问】自定义画图对象消失 [复制链接]

上一主题 下一主题
离线yunyun0220
 
只看楼主 倒序阅读 楼主  发表于: 2008-06-05
— 本帖被 XChinux 执行加亮操作(2008-06-05) —
各位好,我构造了一个类,当它生成的时候就调用PaintEvent函数来画图,我把它加载到一个容器里,但是为什么当我改变窗体大小的时候,就会看到它在不断的往下移,然后就看不到了。。。怎么样才能解决啊。。。急~~谢谢大家~~是不是根SizeHint有关啊。。但是我设置了一下,好像不起作用。。。
离线yunyun0220
只看该作者 1楼 发表于: 2008-06-08
??没有人懂还是怎么得啊。。自己顶~~

PS:可不可以设置一个信号,只有发射它的时候,树才生成一层,即逐步画图。。可以吗、????
离线linshihaoma

只看该作者 2楼 发表于: 2008-06-08
还是把源码贴出来吧
离线yunyun0220
只看该作者 3楼 发表于: 2008-06-09
那个源码好大的啊~~可不可以只帖函数出来啊。。
离线yunyun0220
只看该作者 4楼 发表于: 2008-06-09
----------------------------------MinimizeResult.cpp---------------------------------

#include"MinimizeResult.h"
MinimizeResultTree::MinimizeResultTree(QWidget *parent,Qt::WindowFlags f):QWidget(parent,f)
{
    Up_Left_x=0;
    Up_Left_y=20;
    IncreaceMent=20;
    FontSize=14;
    PenWidth=2;
    LeftMargin=QPoint(Up_Left_x,Up_Left_y);
    IncreaceMent=20;
    SetLength=0;
    BoxLength=0;
}
//return the last char of the spicifyed string
QChar MinimizeResultTree::GetLastState_InASet(int LayerNumber,int SetNumber,QList<QStringList> MinimizeResult)
{
    return (MinimizeResult.at(LayerNumber).at(SetNumber).at(MinimizeResult.at(LayerNumber).at(SetNumber).size()-1));
}
QSize MinimizeResultTree::minimumSizeHint()const
{
    return QSize(400, 400);
}
QSize MinimizeResultTree::sizeHint()const
{
    return QSize(400, 400);
}
//state to pain the tree
void MinimizeResultTree::paintEvent(QPaintEvent *event)
{
    QString str="0,12345678;1,12345;2,678;3,12345;4,67;5,8;6,123;7,45;8,67;9,8;";
    QFile file("../file.txt");
    QDataStream _stream(&file);
    _stream.setByteOrder(QDataStream::LittleEndian);
    if(file.open(QIODevice::WriteOnly))
    {
        int length=str.length();
        for(int i=0;i<length;i++)
        {
          int a=str.data().toAscii();
          _stream<<a;
        }
    }
    file.close();
    file.open(QIODevice::ReadOnly);
    QDataStream stream(&file);
    stream.setByteOrder(QDataStream::LittleEndian);
    QList<QStringList> MinimizeResult;
    QStringList CurrLayer;QString CurrSet;
    int LastState;int CurrInput;int LastInput;int LastSetNumber;int CurrLayerSetsCount=0;;
    CurrSet="";
    LastSetNumber=-1;
    while(!stream.atEnd())
    {
        stream>>CurrInput;
        while(CurrInput!=','&&!stream.atEnd())
        {
            LastInput=CurrInput;
            stream>>CurrInput;   
        }
        LastSetNumber++;
 
        CurrLayerSetsCount++;
        stream>>CurrInput;
        while(!stream.atEnd()&&CurrInput!=';')
        {
        CurrSet+=QString(QChar(CurrInput));
        LastInput=CurrInput;
        stream>>CurrInput;
        }
        if(LastSetNumber==0)
            LastState=LastInput;
        CurrLayer<<CurrSet;
        CurrSet.clear();
        if(0==QString::compare(QString(QChar::fromAscii(LastInput)),QString(QChar::fromAscii(LastState))))
        {
            CurrLayer<<QString::number(CurrLayerSetsCount);
            MinimizeResult<<CurrLayer;
            CurrLayer.clear();
            CurrLayerSetsCount=0;
        }
    }
    QPainter painter;
    QPen pen(Qt::black);
    painter.begin(this);
   
    pen.setWidth(PenWidth);
    painter.setPen(pen);
    QFont font("Arial",FontSize,QFont::Bold);
    painter.setFont(font);
   
    painter.fillRect(event->rect(), QColor(224,224,224));
   
    int MaxSetNumber=MinimizeResult.at(MinimizeResult.count()-1).count();//the biggest set in the tree
    int MaxStateNumber=MinimizeResult.at(0).at(0).count();//return the state number
    MaxBoxLength=FontSize*(MaxStateNumber*1.5+0.5*MaxSetNumber)+IncreaceMent*MaxStateNumber;//the biggest Length of the set
    QString BoxString;//the string after modified added comma
    int LayerNumber=0;
    int SetNumber=0;
    int LastState_InALayer;
    int Flag_Line_Connected=1;//需记录左边的线的点
    QLine CurrLineLeftLine;//remeber the first left bar line to insert the Horizontal Line
   
    SpaceToBar=5;
    while(LayerNumber<MinimizeResult.count())
    {
        LastState_InALayer=0;//no number in the last layer is set to some one's father.
        while(SetNumber<MinimizeResult.at(LayerNumber).count()-1)
        {
            SetLength=GetCurrSetLength(MinimizeResult.at(LayerNumber).at(SetNumber));//get current setLength
            BoxLength=MinimizeResult.at(LayerNumber).at(SetNumber).size()*MaxBoxLength/MaxStateNumber;//get the box length,according to the state the set has.
            BoxString=GetCurrBoxString(MinimizeResult.at(LayerNumber).at(SetNumber));//get the string to show
     
            if(LayerNumber!=MinimizeResult.count()-1)//if it is not at the end of the layer
                painter.drawLine(SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()+SpaceToBar),BoxLength));
            if(LayerNumber!=0)//if it is not the first layer
                painter.drawLine(SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()-2*FontSize+PenWidth),BoxLength));
            //draw text
            painter.drawText(SetBarQLine(LeftMargin,BoxLength).x1()-SetLength/2+2*PenWidth,
                    SetBarQLine(LeftMargin,BoxLength).y1(),BoxString);
   
            if(Flag_Line_Connected==1)//we have just draw a HoriLine,so we have to remeber the next first left line
            {
                CurrLineLeftLine=SetBarQLine(QPoint(LeftMargin.x(),LeftMargin.y()-3*FontSize),BoxLength);
                Flag_Line_Connected=0;//set the first left line is not set again unless the
            }
            LeftMargin=SetLeftMargin(LeftMargin,BoxLength);   
   
            if(LayerNumber!=0)//it is not the first layer and we find the last number in the last layer
            if(GetLastState_InASet(LayerNumber,SetNumber,MinimizeResult)
                    ==GetLastState_InASet(LayerNumber-1,LastState_InALayer,MinimizeResult))
            {
                painter.drawLine(SetLineQLine(CurrLineLeftLine,SetBarQLine(QPoint(LeftMargin.x()
                                    -BoxLength,LeftMargin.y()-3*FontSize),BoxLength)));//we draw the HoriLine
                LastState_InALayer++;
                Flag_Line_Connected=1;//to set the first left line of the HoriLine
            }           
            SetNumber++;       
   
        }
        Flag_Line_Connected=1;
        SetNumber=0; LayerNumber++;
        LeftMargin=SetLeftMargin(QPoint(Up_Left_x,LeftMargin.y()+3*(FontSize+PenWidth)),0);
    }
}
//set the BarLine using the leftMargin and the boxLength
QLine MinimizeResultTree::SetBarQLine(QPoint Point,int BoxLength) { return
    QLine(Point.x()+BoxLength/2,Point.y()+PenWidth*2,Point.x()+BoxLength/2,Point.y()+2*PenWidth+FontSize);
}
//set the HoriLine using the first left line and the last right line
QLine MinimizeResultTree::SetLineQLine(QLine LeftVLine ,QLine RightVLine) {
    return    QLine(LeftVLine.x1(),LeftVLine.y1()+FontSize,RightVLine.x1(),RightVLine.y1()+FontSize);
}
//set the text point unsing the firstMargin and the setLength ,BoxLength
QPoint MinimizeResultTree::SetTextPoint(QPoint LeftMargin,int BoxLength,int
        SetLength) { return
    QPoint(LeftMargin.x()+(BoxLength-SetLength)/2,LeftMargin.y()); }
//set the leftMargin add the LastBoxLength
QPoint MinimizeResultTree::SetLeftMargin(QPoint Point,int BoxLength) {
    return QPoint(Point.x()+BoxLength,Point.y());
}
//set the string after added comma
QString MinimizeResultTree::GetCurrBoxString(QString String)
{
    QString Result="";
    Result+=QString("{");
    Result+=QString(String.at(0));
    int i=1;
    while(i<String.size())
    {
        Result+=QString(",");
        Result+=QString(String.at(i++));
    }
    Result+=QString("}");
    return Result;   
}
//return the special char specified
int MinimizeResultTree::GetCurrSetLength(QString String)
{
  return FontSize*(String.count()+1)/2+FontSize*String.count();
}
//return the boxlength of the current string
int MinimizeResultTree::GetCurrBoxLength(int StateNumber)
{
    return (StateNumber*MaxBoxLength/MaxStateNumber);
}
离线yunyun0220
只看该作者 5楼 发表于: 2008-06-09
---------------------------------------------MinimizeResult.h----------------------------

#ifndef MINIMIZERESULT_H
#define MINIMIZERESULT_H
#include<QPainter>
#include<QWidget>
#include<QPaintEvent>
#include<QFile>
#include<QDataStream>
#include<QTextEdit>
#include<QMessageBox>
class MinimizeResultTree:public QWidget
{
    public:
    MinimizeResultTree(QWidget *parent=0,Qt::WindowFlags f=0);
    QSize minimumSizeHint()const;
    QSize sizeHint()const;
    public:
    void paintEvent(QPaintEvent *);
    QLine SetBarQLine(QPoint,int);
    QLine SetLineQLine(QLine,QLine);
    QPoint SetTextPoint(QPoint,int,int);
    QPoint SetLeftMargin(QPoint,int);
    int GetCurrBoxLength(int);
    int GetCurrSetLength(QString);
    QString GetCurrBoxString(QString);
    QChar GetLastState_InASet(int LayerNumber,int SetNumber,QList<QStringList> MinimizeResult);
   
    public:
    int FontSize;
    QPoint LeftMargin;
    int BoxLength;
    int SetLength;
    int MaxStateNumber;
    int MaxBoxLength;
    int IncreaceMent;
    int PenWidth;
    int SpaceToBar;
    int Up_Left_x;
    int Up_Left_y;
   
};
#endif
离线yunyun0220
只看该作者 6楼 发表于: 2008-06-09
调用该类对象地方代码如下~~

   
w_resultTree=new MinimizeResultTree();
w_Layout_main->addWidget(w_resultTree,0,1);//将该对象加入到容器中
快速回复
限100 字节
 
上一个 下一个