如何在QML中重新绘制封装在QDeclarativeItem中的QWidget
How to repaint QWidget encapsulated in QDeclarativeItem in QML?
我在C++/QML环境中工作,使用Qt 4.8和QtQuick 1.0。
我有一个QWidget派生类QCustomPlot,并将其封装在一个自定义的QDeclarativeItem派生类中。我使用QGraphicsProxyWidget嵌入QWidget,它在创建时看起来很好。我想定期更新图表,但我根本无法,无论我做什么,它都会停留在我在构造函数中启动的位置。
这是我的代码(有些简化):
flow-grafik.h:
class FlowGrafik : public QDeclarativeItem
{
Q_OBJECT
public:
explicit FlowGrafik(QDeclarativeItem *parent = 0);
~FlowGrafik();
void addFlow(double flow);
signals:
public slots:
private:
QCustomPlot * customPlot;
QGraphicsProxyWidget * proxy;
QVector<double> x, y;
};
flowgrafik.cpp:
FlowGrafik::FlowGrafik(QDeclarativeItem *parent) : QDeclarativeItem(parent)
{
customPlot = new QCustomPlot();
proxy = new QGraphicsProxyWidget(this);
proxy->setWidget(customPlot);
this->setFlag(QGraphicsItem::ItemHasNoContents, false);
customPlot->setGeometry(0,0,200,200);
/* WHAT I WRITE HERE WILL BE DISPLAYED */
// pass data points to graph:
customPlot->graph(0)->setData(x, y);
customPlot->replot();
}
FlowGrafik::~FlowGrafik()
{
delete customPlot;
}
void FlowGrafik::addFlow(double flow)
{
//THIS PART DOES NOT GET DISPLAYED
for (int i=0; i<99; ++i)
{
y[i] = y[i+1];
}
y[99] = flow;
customPlot->graph(0)->setData(x, y);
customPlot->replot();
this->update();
}
mainview.qml:
Rectangle {
id: flowGrafik
objectName: "flowGrafik"
x: 400
y: 40
width: 200
height: 200
radius: 10
FlowGrafik {
id: flowGrafikItem
}
}
如果有人能告诉我为什么我的QCustomPlot QWidget没有回复,我将不胜感激。
最终的解决方案是在C++代码中创建一个指向QML项的指针。
我无意中在C++中创建了另一个实例,修改了那个实例,并期望QML实例发生更改。
QDeclarativeView mainView;
mainView.setSource(QUrl("qrc:///qml/qml/mainview.qml"));
Flowgrafik * flowGrafik = mainView.rootObject()->findChild<QObject*>(QString("flowGrafikItem"));
//or if Flowgrafik was the main element: Flowgrafik * flowGrafik = mainView.rootObject();
flowGrafik->addFlow(x);
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- QuadTree只在窗口的右上角绘制
- 绘制旋转的三角形
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 如何在QT中的自定义视频小工具t上绘制矩形
- 如何在Visual Basic中使用矩形函数OpenGL绘制矩形
- 无法使用VAO和EBO(openGL)绘制多个对象
- 为什么我的点没有在 OpenGL 中绘制鼠标所在的位置?
- 绘制顺时针三角形,重新排序点
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- SFML 文本未绘制在窗口上
- 将可变参数函数的参数封装在类实例中
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- 如何在快板的屏幕中显示子位图的绘制?
- 封装C++模板
- 如何绘制自定义形状的元素?
- 将 RTOS 队列对象封装在仅具有静态分配的 IQueue 自定义接口中
- QPainter 将 QBrush 设置为 在 QT/C++ 中绘制文本
- 从封装在对象中的函数 C++ 返回时为空的列表
- 如何在QML中重新绘制封装在QDeclarativeItem中的QWidget