Qt:自定义 LineEditDelegate 的绘制功能

Qt: Paint function of custom LineEditDelegate

本文关键字:绘制 功能 LineEditDelegate 自定义 Qt      更新时间:2023-10-16

我已经实现了ComboBoxDelegate。它源自QStyledItemDelegate .绘画功能用于在未编辑节点时显示单元格的内容。

void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    QStyleOptionComboBox comboBoxOption;
    comboBoxOption.rect = option.rect;
    comboBoxOption.state = QStyle::State_Active | QStyle::State_Enabled;
    comboBoxOption.frame = true;
    comboBoxOption.currentText = index.model()->data(index).toString();
    QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
    QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
}

现在我正在尝试实现LineEditDelegate。我不知道如何编写它的绘画功能。QLineEdit有QStyleOptionComboBox类吗?如果有人做过,你能分享你的代码吗?

试试这个答案。

它使用带有QStyle::PE_PanelLineEdit元素的QStyle::drawPrimitive