OnClicked 属性将数据发送回 Go,但不会调用插槽

问题描述

因此,我尝试在 Qml 中编写一个简单的登录页面用户在两个文本字段中写入数据,一旦按下登录按钮,Go Slot 就会将一些数据发送到另一个函数进行身份验证。 唯一的问题是,当按下登录按钮时,不会调用插槽,代码在创建 UI 的主循环之外继续运行,并以 SIGSEV 退出程序。

基本上过去了:

// Execute app
gui.QGuiApplication_Exec()

退出MakeUI()

出现此错误

Fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x80 addr=0x0 pc=0x265a014]

这是我的 Go 代码

package UI

import (
    "fmt"
    _ "github.com/heyuan110/gorepertory/logger"
    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/gui"
    "github.com/therecipe/qt/qml"
    "github.com/therecipe/qt/quickcontrols2"
    "os"
)
type QmlBridge struct {
    core.QObject
    //_ func(username string,password string) bool `signal:sendToQml`
    _ func(username string,password string )bool `slot:sendToGo`
}

func MakeUI() {
    //var QmlBridgeVar *QmlBridge
    var QmlBridgeVar = NewQmlBridge(nil)

    // Create application
    app := gui.NewQGuiApplication(len(os.Args),os.Args)

    // Enable high DPI scaling
    app.SetAttribute(core.Qt__AA_EnableHighDpiScaling,true)

    // Use the material style for qml
    quickcontrols2.QQuickStyle_SetStyle("material")

    // Create a QML application engine
    engine := qml.NewQQmlApplicationEngine(nil)
    engine.RootContext().SetContextProperty("QmlBridgeVar",QmlBridgeVar)
    //QmlBridgeVar.ConnectSendToQml(func(username string,password string){
    //
    //})
    QmlBridgeVar.ConnectSendToGo(func(username string,password string){
        fmt.Println(username,password)
    })
    // Load the main qml file
    engine.Load(core.NewQUrl3("qml/main.qml",0))
    // Execute app
    gui.QGuiApplication_Exec()
}

这是我对应的 Qml:

import QtQuick 2.0
import QtQuick.Controls 2.1
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3

applicationwindow {
    visible: true
    title: "Bouncer"
    property int margin: 11
    minimumWidth: 600
    minimumHeight: 450

    ColumnLayout {
        id: mainLayout
        anchors.fill: parent
        anchors.margins: margin
        GroupBox {
            id: rowBox
            title: "Login"
            Layout.fillWidth: true

            RowLayout {
                id: rowLayout
                anchors.fill: parent
                TextField {
                    id: usernameField
                    placeholderText: "Username"
                    Layout.fillWidth: true
                }
                TextField {
                    id: passwrdField
                    placeholderText: "Password"
                    Layout.fillWidth: true
                }
                Button {
                    id: loginButton
                    text: "Login"
                    MouseArea{
                        id: loginMouseArea
                        anchors.fill: parent
                        onClicked: QmlBridgeVar.sendToGo(usernameField.text,passwrdField.text)
                    }
                }
            }
        }
    }
}

我倾向于相信我的 Qt 绑定可能会遇到一些问题?虽然我不完全确定。如果有人能给我宝贵的反馈,我将不胜感激。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)