使用别名和 C++ 集成在 .qml 中实现 .ui 文件的逻辑
Implementing logic of .ui file in .qml using alias and C++ integration
我正在尝试 connect the C++ class with QML 但我无法在单独的 qml 文件中实现逻辑。当我直接在 ui.qml 文件中添加功能时,它可以工作,但我收到警告:
Functions are not supported in a Qt Quick UI form
我试过 using alias 但由于某种原因它不起作用。这是代码:
原因表。ui.qml:
import QtQuick 2.4
//import io.qt.UserDataProvider 1.0
MenuPage {
id: reasonsPage
property alias myReasons: myReasons
title: qsTrId("reasons")
Description {
id: reasonsText
text: qsTrId("reasons-text")
anchors.bottomMargin: 150
}
//UserDataProvider{id:dataProvider}
MenuTextArea {
id: myReasons
text: qsTrId("aa")
anchors.horizontalCenter: reasonsText.horizontalCenter
anchors.top: reasonsText.bottom
width: reasonsText.width
//this actually works despite the warnings
//onEditingFinished: dataProvider.saveInput("myReasons", text)
}
ReasonsForm.qml:
import QtQuick 2.4
import io.qt.UserDataProvider 1.0
ReasonsForm {
UserDataProvider{id:dataProvider}
myReasons{
onEditingFinished: dataProvider.saveInput("myReasons", text)
}
}
编译器没有抱怨任何事情,但根本没有调用 dataProvider
中的方法。非常感谢任何帮助。
编辑:我找到了一个解决方法,但它仍然没有解释为什么代码不起作用。使其在没有 qml 文件的情况下工作的方法是添加
Connections {
target: myReasons
onEditingFinished: dataProvider.saveInput("myReasons", myReasons.text)
}
到 ui 文件。
不过,我想将逻辑与 ui 分开。
编辑二:我是不是误会了什么?似乎别名对我来说根本不起作用。这是一个什么都不做的简单示例(按钮什么也不做):
这是在 QML 中使用 C++ 的完整示例。
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import Proxy 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Proxy {
id : proxy
}
Component.onCompleted: {
var test = "Hello C++ from QML";
proxy.call( test );
}
}
proxy.h
#ifndef _proxy_h_
#define _proxy_h_
#include <QObject>
#include <QVariant>
class Proxy : public QObject
{
Q_OBJECT
public :
Proxy();
virtual ~Proxy() {}
Q_INVOKABLE void call( QVariant );
};
#endif
proxy.cpp
#include "proxy.h"
#include <QGuiApplication>
#include <QQmlEngine>
Proxy::Proxy() {}
void Proxy::call( QVariant in )
{
qInfo("C++ : %s", in.toString().toStdString().c_str());
}
static void registerProxy()
{
qmlRegisterType<Proxy>("Proxy", 1, 0, "Proxy");
}
Q_COREAPP_STARTUP_FUNCTION(registerProxy);
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(test_proxy LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc" proxy/proxy.h proxy/proxy.cpp )
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
我终于修好了。问题是我假设当有 ui.qml 和 .qml 文件时,它们已经连接(nameForm.ui.qml 和 name.qml)。需要做的是使用 .qml 文件来显示元素并将 .ui.qml 放在里面所以我需要做的就是 display/include ReasonsForm.qml
文件,其中我也想要 ui 文件的内容,因为 ui 已经像这样显示在 qml 中:
ReasonsForm {...
我正在显示 ReasonsForm.ui.qml
假设这是图形界面,应该用作在 main.qml 或堆栈布局中显示的主要元素,而 ReasonsForm.qml
只是附加元素逻辑。然而首先需要显示的是qml。
我正在尝试 connect the C++ class with QML 但我无法在单独的 qml 文件中实现逻辑。当我直接在 ui.qml 文件中添加功能时,它可以工作,但我收到警告:
Functions are not supported in a Qt Quick UI form
我试过 using alias 但由于某种原因它不起作用。这是代码:
原因表。ui.qml:
import QtQuick 2.4
//import io.qt.UserDataProvider 1.0
MenuPage {
id: reasonsPage
property alias myReasons: myReasons
title: qsTrId("reasons")
Description {
id: reasonsText
text: qsTrId("reasons-text")
anchors.bottomMargin: 150
}
//UserDataProvider{id:dataProvider}
MenuTextArea {
id: myReasons
text: qsTrId("aa")
anchors.horizontalCenter: reasonsText.horizontalCenter
anchors.top: reasonsText.bottom
width: reasonsText.width
//this actually works despite the warnings
//onEditingFinished: dataProvider.saveInput("myReasons", text)
}
ReasonsForm.qml:
import QtQuick 2.4
import io.qt.UserDataProvider 1.0
ReasonsForm {
UserDataProvider{id:dataProvider}
myReasons{
onEditingFinished: dataProvider.saveInput("myReasons", text)
}
}
编译器没有抱怨任何事情,但根本没有调用 dataProvider
中的方法。非常感谢任何帮助。
编辑:我找到了一个解决方法,但它仍然没有解释为什么代码不起作用。使其在没有 qml 文件的情况下工作的方法是添加
Connections {
target: myReasons
onEditingFinished: dataProvider.saveInput("myReasons", myReasons.text)
}
到 ui 文件。 不过,我想将逻辑与 ui 分开。
编辑二:我是不是误会了什么?似乎别名对我来说根本不起作用。这是一个什么都不做的简单示例(按钮什么也不做):
这是在 QML 中使用 C++ 的完整示例。 main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import Proxy 1.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Proxy {
id : proxy
}
Component.onCompleted: {
var test = "Hello C++ from QML";
proxy.call( test );
}
}
proxy.h
#ifndef _proxy_h_
#define _proxy_h_
#include <QObject>
#include <QVariant>
class Proxy : public QObject
{
Q_OBJECT
public :
Proxy();
virtual ~Proxy() {}
Q_INVOKABLE void call( QVariant );
};
#endif
proxy.cpp
#include "proxy.h"
#include <QGuiApplication>
#include <QQmlEngine>
Proxy::Proxy() {}
void Proxy::call( QVariant in )
{
qInfo("C++ : %s", in.toString().toStdString().c_str());
}
static void registerProxy()
{
qmlRegisterType<Proxy>("Proxy", 1, 0, "Proxy");
}
Q_COREAPP_STARTUP_FUNCTION(registerProxy);
CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(test_proxy LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp" "qml.qrc" proxy/proxy.h proxy/proxy.cpp )
target_compile_definitions(${PROJECT_NAME} PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Quick)
我终于修好了。问题是我假设当有 ui.qml 和 .qml 文件时,它们已经连接(nameForm.ui.qml 和 name.qml)。需要做的是使用 .qml 文件来显示元素并将 .ui.qml 放在里面所以我需要做的就是 display/include ReasonsForm.qml
文件,其中我也想要 ui 文件的内容,因为 ui 已经像这样显示在 qml 中:
ReasonsForm {...
我正在显示 ReasonsForm.ui.qml
假设这是图形界面,应该用作在 main.qml 或堆栈布局中显示的主要元素,而 ReasonsForm.qml
只是附加元素逻辑。然而首先需要显示的是qml。