go-qml / qml

QML support for the Go language
Other
1.96k stars 189 forks source link

Can't find the second object in TabView by objectByName #153

Closed CowSmiles closed 9 years ago

CowSmiles commented 9 years ago

Env: Go 1.4.2, Qt 5.4.1 Archlinux x86_64; Go 1.4.2, Qt 5.4.1 Windows x86

ObjectByName works fine when the objects layout parallelly, but in TabView, the second object can't be find. The error string: panic: cannot find descendant with objectName == "button2"。Example is below:

main.go

package main

import (
    "fmt"
    "gopkg.in/qml.v1"
    "io/ioutil"
    "os"
    //"time"
)

type Control struct {
    Root qml.Object
}

var engine *qml.Engine
var window *qml.Window
var ctrl *Control

func main() {
    err := qml.Run(run)
    if err != nil {
        fmt.Fprintf(os.Stderr, err.Error())
        os.Exit(1)
    }
}

func run() error {
    qmlData, err := ioutil.ReadFile("ui.qml")
    engine = qml.NewEngine()

    ctrl = &Control{}
    component, err := engine.LoadString("ui.qml", string(qmlData))
    if err != nil {
        return err
    }
    window = component.CreateWindow(nil)
    window.Show()
    fmt.Println(window.ObjectByName("button1"))
    fmt.Println(window.ObjectByName("button2"))
    fmt.Println(window.ObjectByName("button3"))
    window.Wait()
    return nil
}

ui.qml

import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.3
import QtQuick.Layouts 1.0
import QtQuick.Dialogs 1.2

ApplicationWindow {
    id: root
    visible: true
    visibility: "Maximized"
    title: "Nut"
    width: 1024
    height: 800

    TabView {
        id:frame
        focus:true
        enabled: true

        height: parent.height - 20
        anchors.right: parent.right
        anchors.left: parent.left
        anchors.margins: 10
        Tab {
            title: "button1"
            Button {
                id: button1
                objectName: "button1"
                text: qsTr("Press Me 1")
            }
        }
        Tab {
            title: "button2"
            Button {
                id: button2
                objectName: "button2"
                text: qsTr("Press Me 2")
            }
        }
        Tab {
            title: "button3"
            Button {
                id: button3
                objectName: "button3"
                text: qsTr("Press Me 3")
            }
        }
    }
}
CowSmiles commented 9 years ago

Sorry, it's not a bug. Solution is here: http://stackoverflow.com/questions/22234665/qobjectfindchild-returns-none-without-obvious-reason