我有一个带有值参数的项目。我想知道如何捕捉它的变化事件?
假设有一个 RectComp.qml:
Item{
property alias currentX: rect.x
Rectangle {
id: rect
x: 617
y: 450
}
}
Run Code Online (Sandbox Code Playgroud)
处理currentX创建其实例的应用程序的更改说明如何
Rectangle {
id: host
x: 617
y: 450
RectComp{ id: MyRC}
OnMyRCcurrentXChange(int){log("hello!")}
}
Run Code Online (Sandbox Code Playgroud) import QtQuick 2.0
Item
{
width: 660
height: 660
Rectangle
{
id : dial
width: 360
height: 360
color: "gray"
Rectangle
{
id: dot
height: 5
width: 5
color: "red"
x: dial.x + (dial.width/2);
y: dial.y + (dial.height/2);
}
Image
{
id: line
source: "/home/.../documents/test/straightLine.jpg"
height: 50
width: 50
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
transform: Rotation
{
origin.x: dial.x + (dial.width/2);
origin.y: dial.y + (dial.height/2);
angle: 40
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
是dot原点的表示。线的中心点应位于该原点。
当我应用 时angle : 40,线条会远离其原点。
如何告诉它在旋转时在原点说?
我有两个ColumnLayout,我希望它们可以滚动,但是滚动条不显示。如果我删除一列,它就会显示出来。
代码:
ApplicationWindow {
id: applicationWindow1
visible: true
width: 400
height: 500
title: qsTr("Hello World")
menuBar: MenuBar {
Menu {
title: qsTr("File")
MenuItem {
text: qsTr("Exit")
onTriggered: Qt.quit();
}
}
}
Item {
id : scroll;
anchors.centerIn: parent
}
ScrollView {
anchors.left: parent.left
anchors.top : parent.top
id:col1
width: 240
height: 618
anchors.leftMargin: 0
anchors.topMargin: 0
contentItem : fl0
frameVisible :true
ColumnLayout {
id:fl0
anchors.fill:parent
Rectangle{
width:200
height:200
color : "#585ef3"
}
Rectangle{
width:200
height:200
color : "#585ef3"
} …Run Code Online (Sandbox Code Playgroud) 我有 QVariantMap (不是 QObject,因为属性名称没有预定义)。我将 QVariantMap 注入到 QML 根上下文中以使用属性值进行绑定。
问题是,当我更改 QVariantMap 或什至当我更改 QVariantMap 并为更新的对象调用 setContextProperty 时,或者当我更新(使用 fromValue) contextProperty 返回的对象时,什么也没有发生。所以这看起来像是一次性绑定。
那么,以可观察的方式将 Map 或 Map 注入 QML 上下文中的正确方法是什么,以便映射中的更改项目将反映在 QML 中?
我正在 QML 中创建一个自定义类型,其Column内部有一个GroupBox. 当该类型的用户向其中添加组件时CustomType,它们应该位于Column,而不是GroupBox。如何在不制作额外包装文件的情况下实现这一点?
//CustomType.qml
GroupBox {
Column {
}
}
//Main.qml
CustomType {
CheckBox {//This should be inside the Column of the GroupBox in CustomType
}
}
Run Code Online (Sandbox Code Playgroud) 我是 Qt 和 QML 的新手。我正在使用 QtQuick 2.4。我有TextInput一个在文件中定义了信号的项目,qml如下所示:
import QtQuick 2.4
TextInput {
text: "Text"
cursorVisible: true
signal qmlSignal(string msg)
}
Run Code Online (Sandbox Code Playgroud)
我还有一个与qmlSignal. TextInput我想在用户完成字段输入或关闭 qml 页面以转到应用程序中的另一个页面时触发信号。
实现此所需功能的正确方法是什么?有没有类似的东西onFocusChanged false我可以检测并触发 qml 信号?
我尝试搜索,但无法获得如何在 qml 中执行此操作的示例
我正在尝试使用QtQuick与C++文件中的qml对象进行交互。但遗憾的是暂时没有成功。知道我做错了什么吗?我尝试了两种方法,第一种方法的结果是findChild()返回了nullptr,第二次尝试时我收到Qml 组件未准备好错误。什么是正确的方法呢?
主要的:
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QLatin1String("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
// 1-st attempt how to do it - Nothing Found
QObject *object = engine.rootObjects()[0];
QObject *mrect = object->findChild<QObject*>("mrect");
if (mrect)
qDebug("found");
else
qDebug("Nothing found");
//2-nd attempt - QQmlComponent: Component is not ready
QQmlComponent component(&engine, "Page1Form.ui.qml");
QObject *object2 = component.create();
qDebug() << "Property value:" << QQmlProperty::read(object, "mwidth").toInt();
return app.exec(); …Run Code Online (Sandbox Code Playgroud) 所以我有一个动态变化的饼图。我想在将鼠标悬停在切片上时显示每个切片的值,但我不确定如何在触发 onHovered 时创建工具提示。我用
qt 5.9.1 & 导入 QtQuick.Controls 2.2
更新:我添加了一些代码来解释我如何创建切片。这是代码:
function onUpdateValues(values){
switch(values.type){
case PIE_CHART:
createPieChart(values.data);
break;
...
default:
console.debug("CHART TYPE ERROR");
break;
}
}
}
function createPieChart(data){
pieserieschart.clear();
for (var prop in data) {
var new_slice = pieserieschart.append(prop, data[prop]);
new_slice.tooltip = prop + ": " + data[prop]
//I tried using hovered signal (and without), but it's not doing any difference
new_slice.hovered.connect(function(state) { new_slice.tooltip.visible = state })
//If I replace the above line by the next one, I can …Run Code Online (Sandbox Code Playgroud) 就像一个具有水平流动的 ListView(直到它达到总宽度,然后在下一行继续)。
ListView {
anchors.fill: parent
layoutDirection: Qt.Horizontal
width: container.width; height: implicitHeight
model: ListModel{ id: contactListModel }
delegate: contactComponent
}
Run Code Online (Sandbox Code Playgroud)
上面代码的问题在于它没有考虑宽度的限制。
或者像 GridLayout,但没有定义列数或行数。
Flickable {
anchors.fill: parent
contentHeight: grid.height
contentWidth: container.width
GridLayout {
id: grid
columns: 3
width: container.width; height: implicitHeight
columnSpacing: 0; rowSpacing: 0
Repeater {
model: ListModel{ id: contactListModel }
delegate: contactComponent
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是,如果我没有定义许多列或行,那么无论总宽度如何,它都会继续水平添加项目。而且,间距...
谢谢,
我正在尝试为我的 android apk 设置 applicationwindow {} 大小,所以我希望从 cpp 文件中读取值:
主.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QScreen *screen = QApplication::screens().at(0);
QVariant sz_width = screen->availableSize().width();
QVariant sz_height = screen->availableSize().height();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
Run Code Online (Sandbox Code Playgroud)
然后从 qml 文件中读取它(main.qml):
ApplicationWindow {
id: mainWindow
visible: true
width: sz_width
height: sz_height
}
Run Code Online (Sandbox Code Playgroud)
这是为了稍后在 qml 中轻松操作所有对象大小,因此基本上例如我使用 mainWindow*0.5 的字体大小,因此我可以为每个应用程序分辨率设置适当的字体大小,但只有在我真正设置变量宽度和高度...
也许这个解决方案是“病态的”,但是如果你能用正确的语法帮助我,我想这样做......
谢谢