2017-12-13 22 views
1

사용자가 제공 한 색상으로 원을 그려서 수평 정렬로 선 편집 조정을하고 싶습니다.전화 슬롯에 위젯 페인팅

중고 화가 기능 슬롯에 전화,하지만

#include <QPainter> 
#include "cascadeColorHighlightWidget.h" 

CascadeColorHighlightWidget::CascadeColorHighlightWidget(QWidget *parent) : QWidget(parent) 
{ 
    setWindowFlags(Qt::FramelessWindowHint | Qt::Widget); 
    setAttribute(Qt::WA_DeleteOnClose, true); 
    setFixedSize(187,164); 
    setContentsMargins(0,0,0,0); 
} 

void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event) 
{ 
    Q_UNUSED(event); 

    QPainter painter(this); 
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); 
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1); 
    painter.setPen(QPen(QColor(176, 176, 176),1)); 
    painter.setBrush(QColor(255,255,255)); 
    painter.drawRect(contRect); 

    painter.setPen(QPen(QColor(51,51,51),1)); 
    QFont font("Calibri"); 
    font.setPixelSize(14); 
    painter.setFont(font); 

    painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight"); 
} 

void CascadeColorHighlightWidget::focusOutEvent(QFocusEvent *event) 
{ 
    Q_UNUSED(event); 
    close(); 
} 
void CascadeColorHighlightWidget::setColors(QColor color) 
{ 
    QPainter painter(this); 
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); 
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1); 

    int rectYPos = contRect.y() + 55; 

    painter.setPen(Qt::NoPen); 
    QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16); 

    painter.setPen(Qt::NoPen); 
    painter.setBrush(color); 
    painter.drawEllipse(ellipseRect); 
    /*After this ellipse I need to draw a line edit where user can edit anytime*/ 

    } 

작동하지 않습니다하지만 그 위젯에 타원을 그리기하지 setcolot 호출하여. paintEvent의 항목 만 작동했습니다.

화가와 할 수 있습니까, 아니면 widgetItems를 유지하고이 widget에 삽입해야합니다. 몇 가지 제안을 해주십시오.

+0

'QImage'에 페인트해야합니다. 그리고이 이미지를'paintEvent'에 출력하십시오. –

+0

'setColors'를 사용하여 타원을 그리는 데 사용되는 색상을 업데이트해야합니다 (색상이 클래스의 구성원이라고 가정). 그러면 결국'paintEvent'를 트리거하는'QWidget :: update() '를 호출해야합니다. 그런 다음 당신의'paintEvent' 메소드에서 최근 업데이트 된 회원 "color"를 사용하는 타원의 그림을 추가하십시오. – Scab

답변

0

모든 그림 작업은 paintEvent에서 이루어져야합니다. 당신은 상태를 유지하고 이에 따라 아이템을 칠해야합니다. QPainter을 인수로 사용하고 거기에서 만든 QPainter 개체로 전달하는 메서드를 paintEvent 메서드 내에서 호출합니다.

예 : 위젯 헤더에서

가지고 : 당신이 볼 수 있듯이

private: 
    void setColors(QColor c) { color = c; } 
    void drawEllipse(QPainter & painter); 
    QColor color; 
    bool draw_ellipse; 

는 setColors 방법 만 색상을 설정하면 color 변수 개인 인스턴스에서 해당 색상을 유지합니다.

새로운 방법은 (이전에 setColors에) 도장 작업 호스트 :이 줄

painter.setBrush(color); 

void CascadeColorHighlightWidget::drawEllipse(QPainter &painter) 
{ 
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); 
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1); 

    int rectYPos = contRect.y() + 55; 

    painter.setPen(Qt::NoPen); 
    QRectF ellipseRect = QRectF(contRect.x() + 18, rectYPos, 16, 16); 

    painter.setPen(Qt::NoPen); 
    painter.setBrush(color); 
    painter.drawEllipse(ellipseRect); 
    /*After this ellipse I need to draw a line edit where user can edit anytime*/ 
} 

변수 color하면이 setColors 방법을 사용하여 설정 한 것이다. 그것의 끝에

void CascadeColorHighlightWidget::paintEvent(QPaintEvent *event) 
{ 
    Q_UNUSED(event); 

    QPainter painter(this); 
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing); 
    QRectF contRect = contentsRect().adjusted(1, 1, -1, -1); 
    painter.setPen(QPen(QColor(176, 176, 176),1)); 
    painter.setBrush(QColor(255,255,255)); 
    painter.drawRect(contRect); 

    painter.setPen(QPen(QColor(51,51,51),1)); 
    QFont font("Calibri"); 
    font.setPixelSize(14); 
    painter.setFont(font); 

    painter.drawText(QPointF(contRect.x() + 18, contRect.y() + 28), "Color Highlight"); 

    if(draw_ellipse) 
    { 
     drawEllipse(painter); 
    } 
} 

, 당신은 draw_ellipse을 (생성자에서 false로 초기화하는 것을 잊지 마세요) 테스트하고 true을의 경우 drawEllipse 메소드를 호출처럼

paintEvent 방법이 있어야한다.

의 사용 예를 QWidget의를 위해, 타원을 그려 보자 mousePressEvent :

void CascadeColorHighlightWidget::mousePressEvent(QMouseEvent *event) 
{ 
    setColors(QColor(Qt::red)); 
    draw_ellipse = true; 
    update(); 
} 
여기

, 당신이 전화를 truedraw_ellipse은, 다음 (그리고 많은 문제), 먼저 설정 한 다음 색상을 설정 는 QWidget의 update slot :

[...]은 Qt는 메인 이벤트 루프로 복귀 처리 그것이 일정 페인트 이벤트.

그래서 paintEvent 메서드가 호출되고, 당신의 그림은 클래스의 상태 (colordraw_ellipse 변수)에 따라 업데이트.

+0

코드에 감사 드리며 완벽하게 작동합니다. – Sijith

+0

사용자가 각 색상에 대해 텍스트를 입력 할 수 있도록 타원, 가로 레이아웃 옆에 선 편집을 유지할 수있는 방법이 있습니까? 사용자는 언제든지 텍스트를 편집하고 변경할 수 있어야합니다. – Sijith