Qt: Problem with .ui resource files and ConnectResizeEvent

Created on 27 Apr 2018  路  9Comments  路  Source: therecipe/qt

Hi,

I have the following code. mainwindow.ui is a form made by Qt Designer:

core.QCoreApplication_SetAttribute(core.Qt__AA_ShareOpenGLContexts, true)

widgets.NewQApplication(len(os.Args), os.Args)
window := widgets.NewQMainWindow(nil, 0)

loader := uitools.NewQUiLoader(window)
file := core.NewQFile2("./qml/mainwindow.ui")
if ok := file.Open(core.QIODevice__ReadOnly); !ok {
    log.Fatal("could not open the file")
}
defer file.Close()
widget := loader.Load(file, window)

window.SetupUi(widget)
window.ConnectResizeEvent(func(*gui.QResizeEvent) {
    fmt.Println("resize event was called")
})

widget.Show()
widgets.QApplication_Exec()

This runs fine, but the passed closure to the ConnectResizeEvent is not called. However, if you replace the widget.Show() with window.Show(), the closure is called, but it doesn't show any widgets (empty window).

What am I doing wrong here?

Thanks.

Most helpful comment

Okay, looked into this.
The problem is that you can't override the ResizeEvent from the widget (created through the ui file) because it's just a normal virtual function and not a signal: http://doc.qt.io/qt-5/qwidget.html#resizeEvent.

And the widget instance created through the UILoader doesn't inherit the custom class created by the binding, therefore you can't override the function at runtime. (There is a way to let the UILoader use custom classes, but I haven't looked into that yet.)

Also you may want too look into this project: https://github.com/stephenlyu/goqtuic
It's a port of the uic tool and lets you creates go code from your ui files.

And btw, calling SetupUi will have no effect.

All 9 comments

I will look into this.

Okay, looked into this.
The problem is that you can't override the ResizeEvent from the widget (created through the ui file) because it's just a normal virtual function and not a signal: http://doc.qt.io/qt-5/qwidget.html#resizeEvent.

And the widget instance created through the UILoader doesn't inherit the custom class created by the binding, therefore you can't override the function at runtime. (There is a way to let the UILoader use custom classes, but I haven't looked into that yet.)

Also you may want too look into this project: https://github.com/stephenlyu/goqtuic
It's a port of the uic tool and lets you creates go code from your ui files.

And btw, calling SetupUi will have no effect.

Thanks for looking into this.

Ah, forgot to post the example.

This should work:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/gui"
    "github.com/therecipe/qt/uitools"
    "github.com/therecipe/qt/widgets"
)

func main() {
    core.QCoreApplication_SetAttribute(core.Qt__AA_ShareOpenGLContexts, true)

    widgets.NewQApplication(len(os.Args), os.Args)
    window := widgets.NewQMainWindow(nil, 0)

    loader := uitools.NewQUiLoader(window)
    file := core.NewQFile2("./qml/mainwindow.ui")
    if ok := file.Open(core.QIODevice__ReadOnly); !ok {
        log.Fatal("could not open the file")
    }
    defer file.Close()
    widget := loader.Load(file, window)

    window.SetCentralWidget(widget)
    //window.SetupUi(widget)
    window.ConnectResizeEvent(func(*gui.QResizeEvent) {
        fmt.Println("resize event was called")
    })

    window.Show()
    widgets.QApplication_Exec()
}

Thanks for the example. Unfortunately it still doesn't show the contents of the widget.

Do you run it with qtdeploy or go run?
For qtdeploy you will need to use ":/qml/mainwindow.ui" instead.

I do go run.

Mh, it did work for me while modifying this example.

Hmm, you are right. Your main object in the example is a QWidget, mine is QMainWindow.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angiglesias picture angiglesias  路  5Comments

ohenepee picture ohenepee  路  4Comments

adneg picture adneg  路  6Comments

StarAurryon picture StarAurryon  路  5Comments

ismlsmile picture ismlsmile  路  5Comments