QT在Label上画矩形

来源:互联网 发布:淘宝裘皮 编辑:程序博客网 时间:2024/05/13 06:14
一、首先定义一个mylabel.h,继承于QLabel
#ifndef MYLABEL_H
#define MYLABEL_H
#include <QLabel>
#include <QMouseEvent>
#include <QPainter>
#include <QPen>
#include <QPoint>
class mylabel : public QLabel
{
    Q_OBJECT
public:
    explicit mylabel(QWidget *parent = 0);
    void mousePressEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    int x1,y1,x2,y2;
signals:
    void clicked();
    void sendData(QString);
public slots:
    void onClicket();
};
#endif
二、mylabel.cpp文件
#include "mylabel.h"
#include <QMessageBox>
#include <QString>
mylabel::mylabel(QWidget *parent) :
    QLabel(parent)
{
   connect(this,SIGNAL(clicked()),this,SLOT(onClicket()));
}
void mylabel::mouseReleaseEvent(QMouseEvent *event)
{
    x1=0;
    x2=0;
    y1=0;
    y2=0;
    update();
}
void mylabel::mousePressEvent(QMouseEvent *event) {
    int x=event->x();
    int y=event->y();
    //endPnt.x()=startPnt.x()+100;
    //endPnt.y()=startPnt.y()+100;
    //if (event->button() == Qt::LeftButton) {
    //    emit clicked();
    //}
    if((x>12 && x<81) && (y>9 && y<63))
    {
        x1=12;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>86 && x<155) && (y>9 && y<63))
    {
        x1=86;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>160 && x<229) && (y>9 && y<63))
    {
        x1=163;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>234 && x<303) && (y>9 && y<63))
    {
        x1=238;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>308 && x<377) && (y>9 && y<63))
    {
        x1=312;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>382 && x<451) && (y>9 && y<63))
    {
        x1=388;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>456 && x<525) && (y>9 && y<63))
    {
        x1=463;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>530 && x<599) && (y>9 && y<63))
    {
        x1=538;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>604 && x<673) && (y>9 && y<63))
    {
        x1=613;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>678 && x<747) && (y>9 && y<63))
    {
        x1=688;
        x2=68;
        y1=7;
        y2=57;
    }
    else if((x>762 && x<853) && (y>9 && y<63))
    {
        x1=763;
        x2=90;
        y1=7;
        y2=57;
    }
    else if((x>60 && x<129) && (y>72 && y<126))
    {
        x1=62;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>135 && x<204) && (y>72 && y<126))
    {
        x1=137;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>210 && x<279) && (y>72 && y<126))
    {
        x1=212;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>285 && x<354) && (y>72 && y<126))
    {
        x1=287;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>360 && x<429) && (y>72 && y<126))
    {
        x1=362;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>435 && x<504) && (y>72 && y<126))
    {
        x1=437;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>510 && x<579) && (y>72 && y<126))
    {
        x1=512;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>585 && x<654) && (y>72 && y<126))
    {
        x1=587;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>660 && x<729) && (y>72 && y<126))
    {
        x1=662;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>735 && x<804) && (y>72 && y<126))
    {
        x1=737;
        x2=68;
        y1=68;
        y2=57;
    }
    else if((x>98 && x<167) && (y>134 && y<191))
    {
        x1=100;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>173 && x<242) && (y>134 && y<191))
    {
        x1=175;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>248 && x<317) && (y>134 && y<191))
    {
        x1=250;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>323 && x<392) && (y>134 && y<191))
    {
        x1=325;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>398 && x<467) && (y>134 && y<191))
    {
        x1=400;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>473 && x<542) && (y>134 && y<191))
    {
        x1=475;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>548 && x<617) && (y>134 && y<191))
    {
        x1=550;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>623 && x<692) && (y>134 && y<191))
    {
        x1=625;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>698 && x<767) && (y>134 && y<191))
    {
        x1=700;
        x2=68;
        y1=132;
        y2=57;
    }
    else if((x>131 && x<201) && (y>197 && y<254))
    {
        x1=133;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>206 && x<276) && (y>197 && y<254))
    {
        x1=208;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>281 && x<351) && (y>197 && y<254))
    {
        x1=283;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>356 && x<426) && (y>197 && y<254))
    {
        x1=358;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>431 && x<501) && (y>197 && y<254))
    {
        x1=433;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>506 && x<576) && (y>197 && y<254))
    {
        x1=508;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>581 && x<651) && (y>197 && y<254))
    {
        x1=583;
        x2=68;
        y1=194;
        y2=57;
    }
    else if((x>760 && x<854) && (y>197 && y<254))
    {
        x1=762;
        x2=92;
        y1=194;
        y2=57;
    }
    update();
   QLabel::mousePressEvent(event);
}
/*
void mylabel::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);
    QPainter paint(this);
    paint.setPen(QColor(Qt::red));
    paint.drawRect(20,20,160,160);
}*/
void mylabel::paintEvent(QPaintEvent *event)
{
    QLabel::paintEvent(event);
    QPainter painter(this);
    QPen pen;       //设置画笔,颜色、宽度
    pen.setColor(Qt::red);
    pen.setWidth(2);
    painter.setPen(pen);
    if(x1>0)
    {
        int xxxx=1;
    }
    painter.drawRect(x1,y1,x2,y2);
    int xxx=1;
}
//可以在这里向mainwindow主窗体发送Qstring类型的字符串
void mylabel::onClicket()
{
    emit sendData("asdklfasljfkldasjkl;fjasl;j");
}
在mainwindows.cpp对mylabel进行
connect();
                                             
0 0
原创粉丝点击