简体   繁体   English

我可以将矩形连接到C ++模型吗?

[英]Can I connect rectangle to C++ model?

I have connected QML's ListView to my C++ model and it updates when model changes which is cool. 我已经将QML的ListView连接到我的C ++模型,并且在模型更改时更新,这很酷。 However I don't want to display my data in ListView bur rather in custom way in rectangle (ideally a plain view which doesn't exist). 但是我不想在ListView bur中显示数据,而是以自定义方式在矩形中显示(理想情况下是不存在的纯视图)。

How could I do this? 我该怎么办?

The issue I see obviously is rectangle is not a view and there is other plain view which allows custom drawing. 我显然看到的问题是rectangle不是视图,并且还有其他允许自定义绘图的纯视图。 Is there way around it? 周围有办法吗?

Addon 添加在

Following up on the answer and comment, let me give context why I am doing it. 在回答问题和评论之后,让我说明为什么这样做。 I have various information and if I use list, I will have to use multiple lists on one screen which doesn't look good. 我有各种各样的信息,如果使用列表,则必须在一个屏幕上看起来不太好使用多个列表。 What I want to implement is what I would call 'document view'. 我要实现的就是所谓的“文档视图”。 Header goes here, title goes there, data here and footer here. 页眉位于此处,标题位于此处,数据位于此处,页脚位于此处。 It is a custom presentation of my model's data. 它是模型数据的自定义表示。

@Folibis, I like your first point. @Folibis,我喜欢你的第一点。 It seems like if do something like: 好像要执行以下操作:

Rectangle 
{
   Text { text: mySingleton.getFruitName() }
   Text { text: mySingleton.getFruitPrice() }
}

Note I have intentionally not included anchors or geometry to keep focus on my question but assume price appears next to fruit name. 注意:我故意不添加锚点或几何形状来关注我的问题,但假设价格出现在水果名称旁边。

Does this mean if I update fruit name or price of the exact same object in model else where in GUI, the above will be updated automatically? 这是否意味着如果我在模型中更新完全相同对象的水果名称或价格,而在GUI中其他位置,则以上内容会自动更新?

You have several ways to implement custom drawing. 您有几种实现自定义图形的方法。 I can't really imagine what data can be supplied to Rectangle but anyway: 我真的无法想象可以向Rectangle提供什么数据,但是无论如何:

  1. You can create custom Item in C++, for example singleton an fetch needed data from it. 您可以在C ++中创建自定义项,例如单例从中获取所需数据。
Rectangle {
    width: mySingleton.getWidth();
    height: mySingleton.getHeight();
    color: mySingleton.getColor();
}
  1. You can create custom element derived from QQuickPaintedItem . 您可以创建派生自QQuickPaintedItem自定义元素。 All you need is reimplement QQuickPaintedItem::​paint(QPainter * painter) to draw your own rectangle. 您只需要重新实现QQuickPaintedItem::​paint(QPainter * painter)即可绘制自己的矩形。 It's simpliest way to create ow element but not efficient since it uses QPainter . 这是创建ow元素的最简单方法,但效率不高,因为它使用QPainter

  2. Create custom element deriver from QQuickItem . QQuickItem创建自定义元素QQuickItem You will need to reimplement QSGNode * QQuickItem::​updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData) . 您将需要重新实现QSGNode * QQuickItem::​updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData) That's fast and reliable way but requires OpenGL experience. 这是一种快速可靠的方法,但是需要OpenGL经验。

  3. Also as (1), but painting on Canvas element 也如(1),但在Canvas元素上绘画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM