是从C 中更新QML梯度值的最佳方法
Whats the best way to update the gradient value in QML from C++?
在我的qml文件中,我的梯度定义如下:
ColorizedRoundedButton
{
gradient: Gradient
{
GradientStop { position: 0.0; color: "#3f5c43" }
GradientStop { position: 0.33; color: "#113d14" }
GradientStop { position: 0.66; color: "#023105" }
GradientStop { position: 1.0; color: "#056508" }
}
}
因此,在这里,我希望通过样式表应用此梯度值,即从C 设置此值。
我有类似的style.h文件:
#include <QLinearGradient>
Q_DECLARE_METATYPE(QLinearGradient)
class Style: public QObject
{
Q_PROPERTY(QLinearGradient PositiveGradient READ getPositiveGradient WRITE setPositiveGradient)
QLinearGradient _positiveGradient;
QLinearGradient getPositiveGradient();
void setPositiveGradient(QLinearGradient p_grad);
}
使用这种方法将qlineargradient类型用作q_property,然后将QML文件更改为:
gradient: style.PositiveGradient
这种方法不起作用,我会遇到运行时错误,因为无法将qlineargradient分配给qquickgradient*。
任何人都知道该解决方案或任何更好的解决方案,以传递C 和QML之间的梯度值?
声明类:
class Style: public QObject
{
Q_OBJECT
public:
explicit Style(QObject *parent = nullptr)
{}
public slots:
QStringList stops()
{
QLinearGradient gradeint(0, 0, 1, 100);
gradeint.setColorAt(0, "red");
gradeint.setColorAt(1, "black");
QStringList colors;
for (const auto &g : gradeint.stops())
{
colors.append(g.second.name());
}
return colors;
}
};
您写了此类后,用QML注册并写入:
Rectangle {
id: root
gradient: Gradient {
id:gradient
}
Component
{
id:stopComponent
GradientStop {}
}
Component.onCompleted:
{
var stops = Style.stops()
var stopsList = [];
var length = stops.length;
for (let i = 0; i < length; i++) {
var s1 = stopComponent.createObject(root, {"position": i / length , "color": stops[i]});
stopsList.push(s1)
}
gradient.stops = stopsList
}
}
相关文章:
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 在C++中向零方向近似的最佳方法
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 检测win32服务创建和删除的最佳方法
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 将线程中的数据存储到全局容器的最佳方法?
- 将一系列整数放入类的最佳方法是什么?
- 在派生类中使用基类的私有成员变量的最佳方法
- 在 C++ 中将非指定类型作为参数传递的最佳方法?
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 比较两个节点坐标的最佳方法是什么?
- 在nodejs中使用本机代码的最佳方法是什么?
- 将 pybind11 绑定标记为已弃用的最佳方法
- C++:将向量传递到构造函数以创建成员变量的最佳方法?
- C++中变量混叠的最佳方法
- 读取大文件(>2GB)(文本文件包含以太网数据)并通过不同参数随机访问数据的最佳方法是什么?