Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
class painter : public QWidget
{
    Q_OBJECT
public:
    painter(QWidget 
*parent = 0, Qt::WFlags flags = 0);
    
~painter();
private:
    
void paintEvent(QPaintEvent* e);
    
void clcPos(int x,int y,int r);
    
void renderCircle(QPainter& painter,int x,int y,int r);
    
void renderLine(int p1,int p2);
    
void mousePressEvent(QMouseEvent*);
    
void mouseReleaseEvent(QMouseEvent*);
    
int  length(int x1,int y1,int x2,int y2);
    
int  min(int x,int y,int& m);
private:
    
bool mMousePress;
    QVector
<QPair<int,int> > mPosList;
};

#endif

painter::painter(QWidget* parent,Qt::WFlags flags):
QWidget(parent,flags)
{
    setGeometry(100,100,640,480);
    setBackgroundRole(QPalette::Base);
    setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    mMousePress = false;
}

painter::~painter()
{
}

void painter::paintEvent(QPaintEvent* e)
{
    mPosList.clear();

    int w = width();
    int h = height();

    float p = 3.14159265f;

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);

    for(int i=0;i<5;i++)
    {
        float delta = p*0.4*i - .5*p;
        
        float cos = cosf(delta)*148;
        float sin = sinf(delta)*148;
        int x = w/2 + cos;
        int y = h/2 + sin;
        mPosList.push_back(QPair<int,int>(x,y));
    }

    renderLine(0,2);
    renderLine(1,3);
    renderLine(2,4);
    renderLine(3,0);
    renderLine(4,1);

    for(int i=0;i<5;i++)
    {
        QPair<int,int> pos = mPosList[i];
        renderCircle(painter,pos.first,pos.second,48);
    }
}

void painter::clcPos(int x,int y,int r)
{
    mPosList.push_back(QPair<int,int>(x,y));
}

void painter::renderCircle(QPainter& painter0,int x,int y,int r)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);

    painter.setPen(QPen(Qt::blue,2,Qt::SolidLine));  
    painter.setBrush(QBrush(Qt::green,Qt::SolidPattern));
    painter.drawRoundRect(x-r/2,y-r/2,r,r,99,99);
}

void painter::mousePressEvent(QMouseEvent* e)
{
    if(e->button() == Qt::LeftButton)
    {
        mMousePress = true;
        int x = e->x();
        int y = e->y();
        const int off2 = 48*48;
        
        int m = 9999;
        int offset = min(x,y,m);
        if(m < 36*36)
            std::cout<<"here:"<<offset<<std::endl;
    }
}

int painter::min(int x,int y,int& m)
{
    int offset = 0;
    for(int i=0;i<5;i++)
    {
        QPair<int,int> pos(mPosList[i]);
        int len = length(x,y,pos.first,pos.second);
        if(len < m)
        {
            m = len;
            offset = i;
        }
    }
    return offset;
}

void painter::mouseReleaseEvent(QMouseEvent* e)
{
    mMousePress = false;
}

int painter::length(int x1,int y1,int x2,int y2)
{
    return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
}

void painter::renderLine(int p1,int p2)
{
    QPair<int,int> pos1 = mPosList[p1];
    QPair<int,int> pos2 = mPosList[p2];

    float k = pos2.second - pos1.second;
    k /= (float)(pos2.first - pos1.first);
    std::cout<<k<<std::endl;

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    painter.setPen(QPen(Qt::blue,2,Qt::DotLine));
    painter.drawLine(pos1.first,pos1.second,pos2.first,pos2.second);
}

posted on 2014-12-11 22:07 ccsdu2009 阅读(2069) 评论(0)  编辑 收藏 引用 所属分类: QT编程

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理