Component和ItemQML有什么区别?这里的文档并不完全清楚.用作多个小部件的容器的首选类型是什么?可以替换Rectangle吗?
例如,以下声明的区别是什么:
Item {
id: itemWidget
Rectangle { id: one }
Rectangle { id: two }
}
Run Code Online (Sandbox Code Playgroud)
和
Component {
id: componentWidget
Rectangle { id: one }
Rectangle { id: two }
}
Run Code Online (Sandbox Code Playgroud)
为什么我们通常Component在宣布时使用delegate?
大家好我是QT的新手,我无法通过另一个qml加载一个qml基本上我已经创建了一个qml MyTabView(MyTabView.qml)
import QtQuick 2.3
import QtQuick.Controls 1.2
TabView {
width: 360
height: 360
Component.onCompleted: {
addTab("Tab 1", tab1)
addTab("Tab 2", tab2)
}
Component {
id: tab1
Rectangle {color: "red"}
}
Component {
id: tab2
Rectangle {color: "blue"}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图通过另一个qml(main.qml)显示它在同一目录中
import QtQuick 2.3
import QtQuick.Controls 1.2
import "."
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Main")
MyTabView {}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行我的项目时,我得到了这个错误
QQmlApplicationEngine无法加载组件qrc:/qml/main.qml:11 TabView不是一个类型
请注意我在MyTabView.qml中有M Caps,MyTabView.qml和main.qml在同一目录中.
有人能指出我在做什么错吗?我想指出的一件事是,当我替换MyTabView.qml的所有代码而不是MyTabView {}内部时main.qml,程序不会给出任何错误并正确运行.提前致谢
单击按钮打开另一个 QML 窗口时,我需要隐藏 QML 窗口,我使用 Loader 打开另一个 QML 窗口,它只隐藏 QML 表单组件而不是 QML 窗口,但我目前使用窗口组件打开 QML 窗口
这是我的代码:
Button {
id: button2
x: 19
y: 54
width: 114
height: 25
text: qsTr("DIFF-R")
style: ButtonStyle {
background: Rectangle {
implicitWidth: 10
implicitHeight: 25
border.width: control.activeFocus ? 2 : 1
border.color: "#555"
radius: 10
gradient: Gradient {
GradientStop { position: 0 ; color: control.pressed ? "#ddd" : "#fff" }
GradientStop { position: 1 ; color: control.pressed ? "#8ad993" : "#528dc8" }
}
}
} …Run Code Online (Sandbox Code Playgroud) 如何获得a 中可见的第一个Item/ ?我查看了文档,也在互联网上搜索了很多,但找不到任何东西。有谁知道这是怎么做到的吗?indexListView
谢谢你!
如何制作一些可重用的 QML 对象,它可以注入另一个对象?
我曾经尝试过使用Component& Loader,但似乎不是我想要的。(仍然封装了整个QML类型,缺乏弹性,难以复用)
使用示例:
Card.qmlimport QtQuick 2.0
import QtQuick.Layouts 1.3
Rectangle {
default property var innerObject
property string titleText: "[Hello Untitled Title]"
id: root
color: "#fff"
ColumnLayout {
anchors.fill: parent
Rectangle {
id: header
height: 10
width: parent.width
color: "#666"
RowLayout {
Text { text: titleText; color: "#fff" }
}
}
// How to inject innerObject in here ?
}
}
Run Code Online (Sandbox Code Playgroud)
main.qmlimport QtQuick 2.0
import QtQuick.Layouts 1.3
Card {
titleText: "Image Information"
ColumnLayout …Run Code Online (Sandbox Code Playgroud) 我有一个QML ListView,其中委托从另一个文件加载它的组件.单击委托项时,我想要更新ListView.CurrentIndex和highlight所选项目.
它工作,当我明确设置id的ListView.但是,由于我想将委托Component也用于其他ListViews,我正在努力寻找一种如何ListView.currentIndex从委托中访问的通用方法Component.
这是代码:
main.qml
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
ListModel {
id: contactsModel
ListElement {
name: "Bill Smith"
}
ListElement {
name: "John Brown"
}
ListElement {
name: "Sam Wise"
}
}
ListView{
id: contactsView
anchors.left: parent.left
anchors.top: parent.top
width: parent.width
height: …Run Code Online (Sandbox Code Playgroud) QML类型Component和Instantiator似乎做类似的事情; 按需创建QML对象,而不是解析其定义时. 那有什么区别? 为什么我要使用一个而不是另一个?
我尝试注册一个类型,但出现此错误:
QQmlApplicationEngine 加载组件失败 qrc:/main.qml:5 模块“Komut”未安装
这是我正在使用的代码:
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
qmlRegisterType<Execom>("Komut",1,0,"Execom");
Run Code Online (Sandbox Code Playgroud) 我有一个Loader加载一些非常重的组件的对象.某些事件到达负载的中间,需要加载停止并返回清空Loader.可能吗?
当我用Qt.createComponent动态创建组件时,stutas总是Component.error但我无法理解原因.
我这样用过:
Rectangle{
function loadTest(){
function finishCreation() {
if (component.status === Component.Ready) {
console.log("ready")
} else if (component.status === Component.Error) {
// Error Handling
console.log("Error loading component:", component.errorString());
}
}
var component = Qt.createComponent("MyPage.qml");
console.log(component.status)
console.log("Error loading component:", component.errorString());
component.statusChanged.connect(finishCreation);
if (component.status === Component.Ready) {
var button = component.createObject(container);
console.log("ready")
}
}
Component.onCompleted: {
console.log("Completed Running!")
loadTest()
}
}
Run Code Online (Sandbox Code Playgroud)
如果文件MyPage.qml中不存在qrc,则错误为
qrc:/MyPage.qml:-1找不到文件"
如果我设置完整路径MyPage.qml,我得到一个Network error.
当我将SeriesSelectionPage.qml文件添加到资源文件时,它可以工作.但它不应该是动态的吗?
我只是想找到一个QML文件并在应用程序执行时动态加载它,以便应用程序可以根据用户操作加载不同的QML.
谁知道怎么做?我要疯了.