Qt: How to hide the main window after minisize and show after clicking systray.

Created on 4 May 2019  路  2Comments  路  Source: therecipe/qt

I try to use widget->hide(), but it did not work.

widget.ConnectChangeEvent(func(event *core.QEvent) {
if event.Type() == core.QEvent__WindowStateChange &&
widget.WindowState() == core.Qt__WindowMinimized {
fmt.Println("mini_size event")
widget.Hide()
}
})

Most helpful comment

Hey

It seems like you need to set the window state to core.Qt__WindowNoState first for this to work.
(at least on macOS)
But it rather ugly on macOS, because the app is minimized and then shown again and then hidden.

For this to properly work, you would need to override the nativeEvent (b) or work around this by disabling the minimize button and using the close button instead (c).

package main

import (
    //"unsafe"

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

func main() {
    app := widgets.NewQApplication(0, nil)

    w := widgets.NewQMainWindow(nil, 0)

    //a) this is ugly on macOS, but it seems to work
    /*
        w.ConnectChangeEvent(func(event *core.QEvent) {
            if event.Type() == core.QEvent__WindowStateChange {
                if w.WindowState() == core.Qt__WindowMinimized {
                    println("mini_size event")
                    w.SetWindowState(core.Qt__WindowNoState)
                    w.Hide()
                }
            }
        })
    */

    //b)
    /*
        w.ConnectNativeEvent(func(eventType *core.QByteArray, message unsafe.Pointer, result *int) bool {
            //catch the minimize event for every OS
            return w.NativeEventDefault(eventType, message, result)
        })
    */

    //c)
    //make the window a dialog to hide the minimize button
    //and don't exit the app if the last window is closed/hidden
    //but then you will need to provide some other way to close your application
    w.SetWindowFlags(core.Qt__Dialog)
    app.SetQuitOnLastWindowClosed(false)

    w.Show()

    //systray
    sys := widgets.NewQSystemTrayIcon(nil)
    sys.SetIcon(w.Style().StandardIcon(widgets.QStyle__SP_MessageBoxCritical, nil, nil))
    sys.ConnectActivated(func(reason widgets.QSystemTrayIcon__ActivationReason) {
        if reason == widgets.QSystemTrayIcon__Trigger {
            w.Show()
        }
    })
    menu := widgets.NewQMenu(nil)
    exit := menu.AddAction("Exit")
    exit.ConnectTriggered(func(bool) { app.Exit(0) })
    sys.SetContextMenu(menu)
    sys.Show()

    widgets.QApplication_Exec()
}

All 2 comments

Hey

It seems like you need to set the window state to core.Qt__WindowNoState first for this to work.
(at least on macOS)
But it rather ugly on macOS, because the app is minimized and then shown again and then hidden.

For this to properly work, you would need to override the nativeEvent (b) or work around this by disabling the minimize button and using the close button instead (c).

package main

import (
    //"unsafe"

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

func main() {
    app := widgets.NewQApplication(0, nil)

    w := widgets.NewQMainWindow(nil, 0)

    //a) this is ugly on macOS, but it seems to work
    /*
        w.ConnectChangeEvent(func(event *core.QEvent) {
            if event.Type() == core.QEvent__WindowStateChange {
                if w.WindowState() == core.Qt__WindowMinimized {
                    println("mini_size event")
                    w.SetWindowState(core.Qt__WindowNoState)
                    w.Hide()
                }
            }
        })
    */

    //b)
    /*
        w.ConnectNativeEvent(func(eventType *core.QByteArray, message unsafe.Pointer, result *int) bool {
            //catch the minimize event for every OS
            return w.NativeEventDefault(eventType, message, result)
        })
    */

    //c)
    //make the window a dialog to hide the minimize button
    //and don't exit the app if the last window is closed/hidden
    //but then you will need to provide some other way to close your application
    w.SetWindowFlags(core.Qt__Dialog)
    app.SetQuitOnLastWindowClosed(false)

    w.Show()

    //systray
    sys := widgets.NewQSystemTrayIcon(nil)
    sys.SetIcon(w.Style().StandardIcon(widgets.QStyle__SP_MessageBoxCritical, nil, nil))
    sys.ConnectActivated(func(reason widgets.QSystemTrayIcon__ActivationReason) {
        if reason == widgets.QSystemTrayIcon__Trigger {
            w.Show()
        }
    })
    menu := widgets.NewQMenu(nil)
    exit := menu.AddAction("Exit")
    exit.ConnectTriggered(func(bool) { app.Exit(0) })
    sys.SetContextMenu(menu)
    sys.Show()

    widgets.QApplication_Exec()
}

thanks, the 'a' works fine for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

adneg picture adneg  路  6Comments

iMaxopoly picture iMaxopoly  路  3Comments

woodyjon picture woodyjon  路  5Comments

tjma2001 picture tjma2001  路  3Comments

fabstu picture fabstu  路  4Comments