Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重新实现QStyledItemDelegate::paint-如何获取子元素坐标?_C++_Ruby_Qt - Fatal编程技术网

C++ 重新实现QStyledItemDelegate::paint-如何获取子元素坐标?

C++ 重新实现QStyledItemDelegate::paint-如何获取子元素坐标?,c++,ruby,qt,C++,Ruby,Qt,QTreeView是在自定义QStyledItemDelegate::paint方法的帮助下呈现的。其目的是向节点添加图形元素,例如在项目文本周围绘制(并填充)一个框。树项目可能有复选框,也可能没有 下面的Ruby代码实现了目标,只是我无法获得文本元素的坐标。经验偏移量(x=29;y=4)可作为解决方法。super方法将文本绘制在框的顶部 如何获取文本元素的坐标 这是正确的方法吗,还是我必须使用drawText和drawControl而不是调用超类绘制方法?在这种情况下,如何控制子元素的布局

QTreeView是在自定义
QStyledItemDelegate::paint
方法的帮助下呈现的。其目的是向节点添加图形元素,例如在项目文本周围绘制(并填充)一个框。树项目可能有复选框,也可能没有

下面的Ruby代码实现了目标,只是我无法获得文本元素的坐标。经验偏移量(x=29;y=4)可作为解决方法。
super
方法将文本绘制在框的顶部

  • 如何获取文本元素的坐标
  • 这是正确的方法吗,还是我必须使用drawText和drawControl而不是调用超类绘制方法?在这种情况下,如何控制子元素的布局

(此问题不是Ruby特定的。欢迎使用C++的答案)

class ItemDelegate


<强> >:<强>请找到一个.< /p> 我在C++中遇到了同样的问题。不幸的是,option.rect.*属性的解决方法似乎是查找文本坐标的唯一方法

下面是我的代表的绘制方法:

 void ThumbnailDelegate::paint(QPainter *p_painter, const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const
 {
    if(p_index.isValid())
    {
        const QAbstractItemModel* l_model = p_index.model();
        QPen l_text_pen(Qt::darkGray);

        QBrush l_brush(Qt::black, Qt::SolidPattern);

        /** background rect **/
        QPen l_pen;
        l_pen.setStyle(Qt::SolidLine);
        l_pen.setWidth(4);
        l_pen.setBrush(Qt::lightGray);
        l_pen.setCapStyle(Qt::RoundCap);
        l_pen.setJoinStyle(Qt::RoundJoin);

        p_painter->setPen(l_pen);

        QRect l_border_rect;
        l_border_rect.setX(p_option.rect.x() + 5);
        l_border_rect.setY(p_option.rect.y() + 5);
        l_border_rect.setWidth(p_option.rect.width() - 16);
        l_border_rect.setHeight(p_option.rect.height() - 16);

        QPainterPath l_rounded_rect;
        l_rounded_rect.addRect(QRectF(l_border_rect));
        p_painter->setClipPath(l_rounded_rect);

        /** background color for hovered items **/
        p_painter->fillPath(l_rounded_rect, l_brush);
        p_painter->drawPath(l_rounded_rect);

        /** image **/
        QPixmap l_pixmap = bytearrayToPixmap(l_model->data(p_index, ImageRole).toByteArray()).scaled(150, 150, Qt::KeepAspectRatio);

        QRect l_img_rect = l_border_rect;

        int l_img_x = (l_img_rect.width()/2 - l_pixmap.width()/2)+l_img_rect.x();

        l_img_rect.setX(l_img_x);
        l_img_rect.setY(l_img_rect.y() + 12);
        l_img_rect.setWidth(l_pixmap.width());
        l_img_rect.setHeight(l_pixmap.height());
        p_painter->drawPixmap(l_img_rect, l_pixmap);

        /** label **/
        QRect l_txt_rect = p_option.rect;
        l_txt_rect.setX(l_border_rect.x()+5);
        l_txt_rect.setY(l_border_rect.y() + l_border_rect.height() -20);
        l_txt_rect.setHeight(20);
        l_txt_rect.setWidth(l_txt_rect.width()-20);

        QFont l_font;
        l_font.setBold(true);
        l_font.setPixelSize(12);
        p_painter->setFont(l_font);
        p_painter->setPen(l_text_pen);

        QString l_text = l_model->data(p_index, TextRole).toString();
        p_painter->drawText(l_txt_rect, Qt::ElideRight|Qt::AlignHCenter, l_text);
    }
    else
    {
        qWarning() << "ThumbnailDelegate::paint() Invalid index!";
    }
}
void ThumbnailDelegate::paint(QPainter*p_painter,const qstyleoption视图项和p_选项,const QModelIndex和p_索引)const
{
if(p_index.isValid())
{
const qabstractemmodel*l_model=p_index.model();
QPen l_text_pen(Qt::darkGray);
QBrush l_笔刷(Qt::黑色,Qt::SolidPattern);
/**背景矩形**/
QPen l_pen;
l_pen.setStyle(Qt::SolidLine);
l_pen.设置宽度(4);
l_pen.挫折感(Qt::浅灰色);
l_pen.setCapStyle(Qt::RoundCap);
l_pen.setJoinStyle(Qt::RoundJoin);
p_painter->setPen(l_pen);
正确选择边界;
l_border_rect.setX(p_option.rect.x()+5);
l_border_rect.setY(p_option.rect.y()+5);
l_border_rect.setWidth(p_option.rect.width()-16);
l_border_rect.setHeight(p_option.rect.height()-16);
QPainterPath l_rect;
l_rounded_rect.addRect(QRectF(l_border_rect));
p_painter->setClipPath(l_rounded_rect);
/**悬停项目的背景色**/
p_painter->fillPath(l_rounded_rect,l_brush);
p_painter->绘图路径(l_rounded_rect);
/**形象**/
QPixmap l_pixmap=bytearrayToPixmap(l_模型->数据(p_索引,ImageRole).toByteArray())。缩放(150,150,Qt::keepasspectratio);
QRect l_img_rect=l_border_rect;
int l_img_x=(l_img_rect.width()/2-l_pixmap.width()/2)+l_img_rect.x();
l_img_rect.setX(l_img_x);
l_img_rect.setY(l_img_rect.y()+12);
l_img_rect.setWidth(l_pixmap.width());
l_img_rect.setHeight(l_pixmap.height());
p_painter->drawPixmap(l_img_rect,l_pixmap);
/**标签**/
QRect l_txt_rect=p_option.rect;
l_txt_rect.setX(l_border_rect.x()+5);
l_txt_rect.setY(l_border_rect.y()+l_border_rect.height()-20);
l_txt_rect.setHeight(20);
l_txt_rect.setWidth(l_txt_rect.width()-20);
QFont l_字体;
l_font.setBold(真);
字体设置像素大小(12);
p_painter->setFont(l_font);
p_painter->setPen(l_text_pen);
QString l_text=l_model->data(p_索引,TextRole).toString();
p_painter->drawText(l_txt_rect,Qt::elidelight | Qt::AlignHCenter,l_text);
}
其他的
{
qWarning()
 void ThumbnailDelegate::paint(QPainter *p_painter, const QStyleOptionViewItem &p_option, const QModelIndex &p_index) const
 {
    if(p_index.isValid())
    {
        const QAbstractItemModel* l_model = p_index.model();
        QPen l_text_pen(Qt::darkGray);

        QBrush l_brush(Qt::black, Qt::SolidPattern);

        /** background rect **/
        QPen l_pen;
        l_pen.setStyle(Qt::SolidLine);
        l_pen.setWidth(4);
        l_pen.setBrush(Qt::lightGray);
        l_pen.setCapStyle(Qt::RoundCap);
        l_pen.setJoinStyle(Qt::RoundJoin);

        p_painter->setPen(l_pen);

        QRect l_border_rect;
        l_border_rect.setX(p_option.rect.x() + 5);
        l_border_rect.setY(p_option.rect.y() + 5);
        l_border_rect.setWidth(p_option.rect.width() - 16);
        l_border_rect.setHeight(p_option.rect.height() - 16);

        QPainterPath l_rounded_rect;
        l_rounded_rect.addRect(QRectF(l_border_rect));
        p_painter->setClipPath(l_rounded_rect);

        /** background color for hovered items **/
        p_painter->fillPath(l_rounded_rect, l_brush);
        p_painter->drawPath(l_rounded_rect);

        /** image **/
        QPixmap l_pixmap = bytearrayToPixmap(l_model->data(p_index, ImageRole).toByteArray()).scaled(150, 150, Qt::KeepAspectRatio);

        QRect l_img_rect = l_border_rect;

        int l_img_x = (l_img_rect.width()/2 - l_pixmap.width()/2)+l_img_rect.x();

        l_img_rect.setX(l_img_x);
        l_img_rect.setY(l_img_rect.y() + 12);
        l_img_rect.setWidth(l_pixmap.width());
        l_img_rect.setHeight(l_pixmap.height());
        p_painter->drawPixmap(l_img_rect, l_pixmap);

        /** label **/
        QRect l_txt_rect = p_option.rect;
        l_txt_rect.setX(l_border_rect.x()+5);
        l_txt_rect.setY(l_border_rect.y() + l_border_rect.height() -20);
        l_txt_rect.setHeight(20);
        l_txt_rect.setWidth(l_txt_rect.width()-20);

        QFont l_font;
        l_font.setBold(true);
        l_font.setPixelSize(12);
        p_painter->setFont(l_font);
        p_painter->setPen(l_text_pen);

        QString l_text = l_model->data(p_index, TextRole).toString();
        p_painter->drawText(l_txt_rect, Qt::ElideRight|Qt::AlignHCenter, l_text);
    }
    else
    {
        qWarning() << "ThumbnailDelegate::paint() Invalid index!";
    }
}