douzongmu2543 2017-10-24 13:53
浏览 77

gotk3,带有自定义小部件的笔记本附件,并将其取回

I have this summarized code to display tabs in a notebook in Gtk. Basically, I have created a custom struct with a embedded label, that is added to the notebook. After that, I want to get back that custom widget, but I get a Invalid type assertion. I have read a lot about structs and interfaces, but I can't get it to work.

package main

import "github.com/gotk3/gotk3/gtk"

type NotebookPage struct {
    gtk.Label
}


func main() {

    gtk.Init(nil)

    win, _ := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)

    notebook, _ := gtk.NotebookNew()
    win.Add(notebook)

    content1, _ := gtk.LabelNew("Content 1")
    page1 := NotebookPage{Label: *content1}
    label1, _ := gtk.LabelNew("Label 1")
    notebook.AppendPage(&page1, label1)

    content2, _ := gtk.LabelNew("Content 2")
    page2 := NotebookPage{Label: *content2}
    label2, _ := gtk.LabelNew("Label 2")
    notebook.AppendPage(&page2, label2)

    win.ShowAll()
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    backwidget1, _ := notebook.GetNthPage(0)
    backpage1, _ := backwidget1.(*NotebookPage)

    gtk.Main()

}
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-10-24 15:33
    关注

    Looks like your problem is here on line 35 as the message says:

    backpage1, _ := backwidget1.(*NotebookPage)

    notebook.GetNthPage returns a *Widget, *NotebookPage is not a *Widget, so you're not allowed to cast to it. If the function didn't return a concrete type (if it returned the same IWidget interface), and if it didn't roundtrip through gtk C libraries, you could do this.

    As it is if you want to get your custom widget back you probably need to get at the underlying gtk widget or serialisation (which presumably stores your custom label), extract the label and build a new NotebookPage.

    So you need something like :

    func NewNotebookPage(widget *Widget) {
       return &NotebookPage{Label: widget.GetLabelSomehow()}
    }
    

    It looks like Label is also a widget :) This is painful because it's trying to work with C++ inheritance in Go. You'll have to find out how to unfreeze your label from the widget.C.GtkWidget

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办