如何在paintEvent中保留可编辑的QLabel

时间:2017-12-15 07:20:12

标签: qt qt4 qt5

我想在QLabel内的水平布局中保留一个椭圆和paintEvent,并希望QLabel在双击时可以编辑。

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

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

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

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

    int rectYPos = m_contRect.y() + 55;

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

    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(224,90,90));
    painter.drawEllipse(ellipseRect);

    QLabel dummy;
    dummy.setText("Density");
    dummy.setTextInteractionFlags(Qt::TextEditable);

    QStyleOptionFrameV2 panel;
    panel.initFrom(&dummy);
    panel.rect = QRect(m_contRect.x() + 54, m_contRect.y() + 48, 100, 30);  // QFontMetric could provide height.
    panel.lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth, &panel, &dummy);
    style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panel, &painter, this);

}

1 个答案:

答案 0 :(得分:0)

当用户指向焦点到QLabel图像时,创建新的QLabel对象,移动到适当的窗口位置并设置文本。用户将看到"生活"编辑。当用户聚焦时,移除QLabel对象并使用QLabel图片重新绘制图像。