QLabel을 상속받은 클래스를 만든후  Q디자이너에서 Prompt to...로 기존의 라벨객체를 PaintLabel 객체로 승격


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class PaintLabel : public QLabel
{
    Q_OBJECT
public:
    PaintLabel(QWidget *parent = nullptr);
    void mousePressEvent ( QMouseEvent * ev );
    void paintEvent      ( QPaintEvent * e  );
 
signals:
    void roiSelect(QVector<QPoint> _points);
    void roiRelesed();
 
private:
 
    QPoint p;
    QVector<QPoint> points;
};
 
cs



MousePressEvent와 Paint 이벤트 지정



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void PaintLabel::mousePressEvent ( QMouseEvent* e )
{
    p = e->pos();
    if(points.size() > 4) points.clear();
 
    if(e -> button() == Qt::LeftButton){
        points.push_back(p);
 
        if(points.size() > 3){
            emit roiSelect(points);
            points.push_back(points.at(0));
        }
 
    }else if(e->button() == Qt::RightButton){
        points.clear();
        emit roiRelesed();
    }
 
    update();
 
}
 
void PaintLabel::paintEvent( QPaintEvent * e ){
 
    QPainter painter(this);
    QPen paintpen(Qt::red);
    paintpen.setWidth(5);
 
    painter.setPen(paintpen);
//    painter.drawPoint(p);
 
    painter.drawPolyline(points);
 
 
}
 
cs


+ Recent posts