miqt-uic: fix setObjectName for qt6

This commit is contained in:
mappu 2025-05-04 14:16:39 +12:00
parent d802e345fe
commit 1498f05fa5

View File

@ -211,15 +211,29 @@ func renderProperties(properties []UiProperty, ret *strings.Builder, targetName,
return nil return nil
} }
func generateWidget(w UiWidget, parentName string, parentClass string) (string, error) { func generateSetObjectName(target string, objectName string, useQt6 bool) string {
if useQt6 {
// return `ui.` + target + `.SetObjectName(*qt.NewQAnyStringView3(` + strconv.Quote(objectName) + "))\n"
spn := target + "__objectName"
ret := spn + `:= qt.NewQAnyStringView3(` + strconv.Quote(objectName) + ")\n"
ret += `ui.` + target + ".SetObjectName(*" + spn + ")\n"
ret += spn + ".Delete() // setter copied value\n"
return ret
} else {
return `ui.` + target + `.SetObjectName(` + strconv.Quote(objectName) + ")\n"
}
}
func generateWidget(w UiWidget, parentName string, parentClass string, useQt6 bool) (string, error) {
ret := strings.Builder{} ret := strings.Builder{}
ctor := "New" + w.Class ctor := "New" + w.Class
ret.WriteString(` ret.WriteString(`ui.` + w.Name + ` = qt.` + ctor + `(` + qwidgetName(parentName, parentClass) + ")\n")
ui.` + w.Name + ` = qt.` + ctor + `(` + qwidgetName(parentName, parentClass) + `)
ui.` + w.Name + `.SetObjectName(` + strconv.Quote(w.Name) + `) ret.WriteString(generateSetObjectName(w.Name, w.Name, useQt6))
`)
if RootWindowName == "" { if RootWindowName == "" {
RootWindowName = `ui.` + w.Name RootWindowName = `ui.` + w.Name
} }
@ -257,10 +271,8 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
if w.Layout != nil { if w.Layout != nil {
ctor := "New" + w.Layout.Class ctor := "New" + w.Layout.Class
ret.WriteString(` ret.WriteString(`ui.` + w.Layout.Name + ` = qt.` + ctor + `(` + qwidgetName("ui."+w.Name, w.Class) + ")\n")
ui.` + w.Layout.Name + ` = qt.` + ctor + `(` + qwidgetName("ui."+w.Name, w.Class) + `) ret.WriteString(generateSetObjectName(w.Layout.Name, w.Layout.Name, useQt6))
ui.` + w.Layout.Name + `.SetObjectName(` + strconv.Quote(w.Layout.Name) + `)
`)
// Layout->Properties // Layout->Properties
@ -284,7 +296,7 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
// Layout items have the parent as the real QWidget parent and are // Layout items have the parent as the real QWidget parent and are
// separately assigned to the layout afterwards // separately assigned to the layout afterwards
nest, err := generateWidget(*child.Widget, `ui.`+w.Name, w.Class) nest, err := generateWidget(*child.Widget, `ui.`+w.Name, w.Class, useQt6)
if err != nil { if err != nil {
return "", fmt.Errorf(w.Name+"/Layout/Item[%d]: %w", i, err) return "", fmt.Errorf(w.Name+"/Layout/Item[%d]: %w", i, err)
} }
@ -354,10 +366,8 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
// Actions // Actions
for _, a := range w.Actions { for _, a := range w.Actions {
ret.WriteString(` ret.WriteString(`ui.` + a.Name + ` = qt.NewQAction(` + parentName + ")\n")
ui.` + a.Name + ` = qt.NewQAction(` + parentName + `) ret.WriteString(generateSetObjectName(a.Name, a.Name, useQt6))
ui.` + a.Name + `.SetObjectName(` + strconv.Quote(a.Name) + `)
`)
// QActions are translated in the parent window's context // QActions are translated in the parent window's context
if prop, ok := propertyByName(a.Properties, "text"); ok { if prop, ok := propertyByName(a.Properties, "text"); ok {
@ -414,7 +424,7 @@ func generateWidget(w UiWidget, parentName string, parentClass string) (string,
) )
for i, child := range w.Widgets { for i, child := range w.Widgets {
nest, err := generateWidget(child, `ui.`+w.Name, w.Class) nest, err := generateWidget(child, `ui.`+w.Name, w.Class, useQt6)
if err != nil { if err != nil {
return "", fmt.Errorf(w.Name+"/Widgets[%d]: %w", i, err) return "", fmt.Errorf(w.Name+"/Widgets[%d]: %w", i, err)
} }
@ -543,7 +553,7 @@ func New` + u.Class + `Ui() *` + u.Class + `Ui {
ui := &` + u.Class + `Ui{} ui := &` + u.Class + `Ui{}
`) `)
nest, err := generateWidget(u.Widget, "", "") nest, err := generateWidget(u.Widget, "", "", useQt6)
if err != nil { if err != nil {
return nil, err return nil, err
} }