gpt4 book ai didi

c++ - 如何从 C++ 访问特定 QML 控件的事件

转载 作者:行者123 更新时间:2023-11-28 00:38:33 29 4
gpt4 key购买 nike

有没有一种方法可以从 C++ 访问 QML 控件(例如按钮)的信号(例如 clicked())。假设我有那个特定控件的内存地址。我只想从 C++ 代码模拟点击事件。

最佳答案

简单。您只需在具有 QObject 基础的 C++ 对象中创建一个插槽,确保其注册为 QML 类型,然后在 QML 文档中“实例化”它,并使用 connect 通过 QML 将所需信号连接到 C++ 对象() 并从 C++ 端处理逻辑。

例子:

我有一个矩形,我想从中获取 onWidthChanged 信号并在我的类 ShapeTracker 中使用它,它跟踪形状何时发生变化或其他什么

在 main.cpp 中:

#include "shapetracker.h"

int main(int argc, char *argv[])
{

QGuiApplication app(argc, argv);



QQmlApplicationEngine engine;



/* this is where you register ShapeTracker as a QML type that can be
accessed through the QML engine even though its a C++ QObject */

qmlRegisterType<ShapeTracker>("my_cpp_classes", 1, 0, "ShapeTracker");




engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}

然后在

主.qml

import QtQuick 2.6



/* import the C++ class you registered into this QML document */

import my_cpp_classes 1.0


Window {
visible: true
Item {


/* set up a parent item that has a signal which is exposed to
the ShapeTracker object and the Rectangle you want to track */
id: myRootItem
signal rectangleChanged(var newWidth)



/* Heres our special Rectangle from QML that will send a signal when
its width changes */
Rectangle {
id: specialRectangle
width: 250


/* send the signal rectangleChanged(width) from our parent item
root item whenever width changes
*/
onWidthChanged: function() { rectangleChanged(width); }

}


/* Special Button that when clicked enlarges the Rectangle but
10px */
Button {
id: mySpecialButton
onClicked: { click_handler(mouse); }

function click_handler(mouse): {
if (specialRectangle.width < 500)
specialRectangle.width += 10;
}
}



/* Heres the actual ShapeTracker instance, which exists
as a QObject inside of the QML context, but has its methods
which are declared in C++, and exposed to the QML engine */
ShapeTracker {
id: myShapeTracker


/* similar to a constructor, but more like a callback */
Component.onCompleted: {


/* connect our signal from the parent root Item called
"rectangleChanged" to the ShapeTracker's Slot called
"myRectangleChangeHandler" */
myRootItem.rectangleChanged.connect(myShapeTracker.myRectangleChangeHandler);


/* connect send_mouse_click to the click_handler of
the button */
myShapeTracker.send_mouse_click.connect(mySpecialButton.click_handler)

}

}

}
}

在 shapetracker.h 中,您只需添加一个名为 myRectangleChangeHandler 的新插槽,只要它通过 QML 发送并通过 C++ 处理,它就会收到该信号

class ShapeTracker : public QObject {
Q_OBJECT
public:
ShapeTracker(QObject *parent = 0 );

signal:

void send_mouse_click(QMouseEvent *event);


public slots:
void myRectangleChangeHandler(QVariant newWidth) {
/* Perform a mouse click on our QML Object mySpecialButton
using QQuickItem::mousePressEvent and sending it via
signal back to QML */


QMouseEvent myEvent(QEvent::MouseButtonPress, QPointF(1,1), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);

QMouseEvent* pressEvent = QQuickItem::mousePressEvent(&myEvent);

emit send_mouse_click(pressEvent);


}

};


总而言之,

您将 C++ QObject 公开给 QML,然后使用

  object.signal.connect(cppObject.desired_slot)  

为了连接它们——所有额外的东西都是为了一个功能示例,以防以后有人需要它

实际上,您甚至不需要这个功能,因为在 onClick 事件中发生的任何事情都可以很容易地放入任何其他属性中,例如

Rectangle {
id: rect
signal customClick(var var1)
onCustomClick : { console.log(var1); }
}

Item {
rect.customClick(1);
}

关于c++ - 如何从 C++ 访问特定 QML 控件的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19958211/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com