[英]Qml QColor Type Error
我试图通过将QColor从C ++传递到QML来设置自定义QQuickPaintedItem的QColor属性。 我尝试了以下方法:
QML代码:
m_DisplayScreens[m_DisplayScreens.length].backgroundColor = m_Model.getBackgroundColor(i_Timer);
m_DisplayScreens是我的自定义QML小部件的列表。 我可以通过做类似的事情来设置backgroundColor属性。
DisplayScreen
{
backgroundColor: "Red"
}
“ m_Model”对象只是一个QObject,它是QML形式的“后端”。 getBackgroundColor的代码如下:
Q_INVOKABLE QString getBackgroundColor(int index);
QString CountDownPanelModel::getSegmentColor(int index)
{
return "#003300";
}
具体的错误是:xxx.js:19:TypeError:类型错误
任何帮助,将不胜感激。 我已经为此努力了几个小时。
谢谢,
杰克
第一次编辑:
好的,这是我返回QColor时的尝试。
class CountDownPanelModel : public QObject
{
Q_OBJECT
public:
explicit CountDownPanelModel(QObject *parent = 0);
~CountDownPanelModel() = default;
Q_INVOKABLE QColor getBackgroundColor(int index);
Q_INVOKABLE QColor getSegmentColor(int index);
};
QColor CountDownPanelModel::getBackgroundColor(int index)
{
return QColor(44, 44, 44);
//return m_TimerList->at(index)->getTimerData()->getBackgroundColor();
}
QColor CountDownPanelModel::getSegmentColor(int index)
{
return QColor(200, 200, 200);
//return m_TimerList->at(index)->getTimerData()->getSegmentColor();
}
使用QColor的结果与使用QString的结果相同。 在分配颜色的行上出现“类型错误”。 例如:
var m_DisplayScreens = [];
function createDisplays()
{
m_DisplayScreens = [];
var timerCount = m_Model.getTimerCount();
var bg = m_Model.getBackgroundColor(0);
var fg = m_Model.getSegmentColor(0)
for (var i_Timer = 0;
i_Timer < timerCount;
++i_Timer)
{
var component = Qt.createComponent("DynamicSevenSegmentDisplay.qml");
var display = component.createObject(m_Panel);
display.initialize(m_Model.getSevenSegmentDisplayInitializer())
display.y = 100 * (i_Timer);
m_DisplayScreens[m_DisplayScreens.length] = display;
m_DisplayScreens[m_DisplayScreens.length].backgroundColor = m_Model.getBackgroundColor(i_Timer);
m_DisplayScreens[m_DisplayScreens.length].segmentColor = m_Model.getSegmentColor(i_Timer)
}
m_Panel.height = 100 * timerCount;
}
为了完整起见,这里是DynamicSevenSegmentDisplay.qml
SevenSegmentDisplayScreen
{
y: 0
height: 100
width: parent.width - x
backgroundColor: "Black"
borderPercentage: 10
displayCount: 20
text: "1234567890"
anchors.left: m_SettingsButton.right
anchors.leftMargin: 8
}
我对为什么不能将QColor分配给backgroundColor感到完全困惑。 调试器在“值”列中什么也不显示。 我认为这是'null'的JS版本。
这不是一个明确的答案,但我发现了几个问题。 我以前使用Linux Mint进行开发。 我最近改用Ubuntu,发现在C ++和QML之间传递数据存在一些问题。 我收到一条有关CRC错过比赛的消息。
“在“ /usr/lib/x86_64-linux-gnu/dri/i915_dri.so”中找到的调试信息与“ /usr/lib/x86_64-linux-gnu/dri/i965_dri.so”不匹配(CRC不匹配)。 ”
因此,这也可能是问题的一部分。 但在大多数情况下,它看起来像是我的开发机器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.