我有 QQuickView 从 C++ 实例化组件的问题......这是我的代码:
类定义 ( vviewerqml.h):
class VViewerQml : public QObject
{
Q_OBJECT
public:
explicit VViewerQml(QSettings &systemSettings, QObject *parent = 0);
~VViewerQml();
protected slots:
void onViewStatusChanged(QQuickView::Status status);
protected:
QString _qmlFolder;
QQuickView _view;
};
Run Code Online (Sandbox Code Playgroud)
类实现(vviewerqml.cpp):
#include "vviewerqml.h"
VViewerQml::VViewerQml(QSettings &systemSettings, QObject *parent) :
QObject(parent)
{
// Initialize viewer reading from settings file
_qmlFolder = "/my/path/to/qml/files";
// Initialize the source
connect(&_view, SIGNAL(statusChanged(QQuickView::Status)),
this, SLOT(onViewStatusChanged(QQuickView::Status)));
_view.setSource(QUrl::fromLocalFile(QDir(_qmlFolder).filePath("Main.qml")));
// Show the viewer
_view.show();
}
VViewerQml::~VViewerQml()
{
// Close the viewer
_view.close(); …Run Code Online (Sandbox Code Playgroud) 我怎样才能从 C++ 中Rectangle动态地向带有 id 的那个添加更多的s root?例如,还有两个Rectangles 颜色red和green?
主.qml:
Rectangle {
id: root
}
Run Code Online (Sandbox Code Playgroud)
要在“root”下添加的典型 QML 对象:
Rectangle { color: "red"; width: 200; height: 200 }
Rectangle { color: "green"; width: 200; height: 200 }
Run Code Online (Sandbox Code Playgroud)
Qt Creator 生成main.cpp:
int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
viewer.setMainQmlFile(QStringLiteral("qml/gui/main.qml"));
viewer.showExpanded();
return app.exec()
}
Run Code Online (Sandbox Code Playgroud) 我将我的游戏移植到QML并且有一个困难.看这张图片:
三角形是一个精灵,线条是力矢量.我可以在QML中显示精灵,但我无法理解如何绘制矢量.它的长度可以超过精灵大小.
我想我可以用Rectangle用height的1px的,但我不知道如何将它旋转.
力量可以在每帧变化,因此绘图性能必须良好.
我正在学习Qt 5.5和QML.
框架很强大,有时候有很多方法可以做一件事.我认为有些可能比其他人更有效率,我想知道何时以及为何使用一个而不是另一个.
我想要一个可以解释所做出的选择的答案.当我使用新代码时,如果在C++端有用,可以使用C++ 11和C++ 14语法.
要解决的问题是:
我已经TextField链接到一个可以弹出的按钮FileDialog.我想在文本TextField是red当它是无效的,并保持不变,否则(我把它设置为green,因为我不知道如何让"默认"的颜色).它的值TextField将在C++端使用,并在应用程序退出时保持不变.
我使用自定义QValidator,QML方面的一些属性,使用onTextChanged:和onValidatorChanged:修改文本的颜色编写了一个版本.根据validQML中的属性()设置文本颜色,该属性是从C++端(在验证器中)设置的.要设置属性,C++必须按名称查找调用者(TextField命名directoryToSave),因为我还没有找到将对象本身作为参数传递的方法.
以下是包含在以下内容中的QML代码MainForm.ui.qml:
TextField {
property bool valid: false
id: directoryToSave
objectName: 'directoryToSave'
Layout.fillWidth:true
placeholderText: qsTr("Enter a directory path to save to the peer")
validator: directoryToSaveValidator
onTextChanged: if (valid) textColor = 'green'; else textColor = 'red';
onValidatorChanged:
{
directoryToSave.validator.attachedObject = directoryToSave.objectName;
// forces validation
var …Run Code Online (Sandbox Code Playgroud) 我已经看过关于QML中内容余量的大量问题,但所有答案都指向缺少的spacing: 0属性。我已经完成了所有这些操作,但是仍然得到了无法消除的怪异空间。谁能解释为什么这个QML代码:
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Test")
RowLayout {
spacing: 0
anchors.margins: 0, 0, 0, 0
anchors.fill: parent;
Pane {
anchors.margins: 0, 0, 0, 0
id: menuPane
anchors.top: parent.top;
anchors.bottom: parent.bottom;
width: 200
ColumnLayout {
spacing: 0
anchors.fill: parent
anchors.margins: 0, 0, 0, 0
Rectangle {
id: testRect
Layout.fillWidth: true
anchors.top: parent.top
anchors.left: parent.left
anchors.right: parent.right
height: 20
color: "green"
}
}
}
Pane …Run Code Online (Sandbox Code Playgroud) 我在c ++中定义了以下QML对象:
class MyObj : public QQuickItem {
Q_OBJECT
Q_PROPERTY(QVariant func MEMBER func)
public slots:
void callFunc(){
//Call function pointed by "func" somehow
}
private:
QVariant func;
};
Run Code Online (Sandbox Code Playgroud)
在QML中我使用MyObj如下:
MyObj{
func: function test(){ console.log("Hi!"); }
Button{
text: "Call func"
onClicked: parent.callFunc()
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Unable to assign a function to a property of any type other than var.
Run Code Online (Sandbox Code Playgroud)
我不明白,是不是一个QVariant应该是相同的属性property var?这样做的正确方法是什么?
显然,children属性被记录为a list<Item>,没有内置的Javascript indexOf(element).以下代码:
Item{
id: exampleParent
Item{ id: exampleChild }
}
Button{
text: "Get index"
onClicked: console.log(exampleParent.children.indexOf(exampleChild))
}
Run Code Online (Sandbox Code Playgroud)
会抛出TypeError: Property 'indexOf' of object [object Object] is not a function错误.
为什么会这样,有什么具体原因吗?有没有比手动遍历children阵列更好的解决方案?
我想动态重新翻译Qt Quick GUI字符串.
重新创建受影响的字符串属性存在侵入性技巧,其中有关更改的通知无法集中.
是否有可能使qsTr(和其他)string类似返回的对象,其行为完全相同string,但行为类似于连接到常见"valueChanged"信号的全局属性(我想QEvent::LanguageChange在QCoreApplication发生时发出它).
我想我可以使用twitching Loader的active属性,它包含整个顶级GUI元素,使所有用户可见的字符串重新翻译,但这种方法导致所有项目和组件的状态丢失,连接到Loader并且没有差异从完整的应用程序重启为我.
是否有可能创造这样的myQsTr功能?
是否可以在一个页面中显示多个listView?
我有3个单独的列表视图,我想在一个页面上显示它们,我正在努力将它们列出来.它们彼此重叠.
布局的简单示例:
ListView {
id: listOne
property string headertitle: "list one header"
spacing: 5
header: headerComponent
model: ListOneModel
}
ListView {
id: listTwo
property string headertitle: "list two header"
spacing: 5
header: headerComponent
model: ListTwoModel
}
ListView {
id: listThree
property string headertitle: "list three header"
spacing: 5
header: headerComponent
model: ListThreeModel
}
Run Code Online (Sandbox Code Playgroud) 我在C ++中有一个QQuickItem,它在QML应用程序中实例化。如果我的QQuickItem的width属性改变,我想在c ++端触发一个自定义函数。
在QML中,我将执行onWidthChanged:{我的代码}。我该如何在c ++方面做到这一点?
qtquick2 ×10
qml ×9
qt ×6
qt5 ×5
c++ ×3
javascript ×2
qt-quick ×2
arrays ×1
translation ×1
validation ×1