Qt: Look for Examples slot signal

Created on 6 Jun 2017  路  6Comments  路  Source: therecipe/qt

Hello.
I tray rewrite my program and i have a lot of problems , because all your examples don't work.
More method you used but not declared... This examples is for older version, or oder version?
I am so grateful for your work but now i can't go more alone:(

In Qt in python i used QThread for carate database handler and connect database thread handler like this:
self.connect(self.parent().database1,QtCore.SIGNAL("output(PyQt_PyObject)"),self.return)

in database handler a used self.emit:
self.emit(QtCore.SIGNAL("output(PyQt_PyObject)"),[self.sql_code[0],self.ex(),self.query_status])

I can't find QThread examples.
I don't know how you "connect" go runtimes function.

Most helpful comment

No problem :)

Also this should work:

package main

import (
    "fmt"
    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
    "os"
    "time"
)

func Data_Get(stanvalue chan<- int, data chan<- string, statusthread chan<- bool) {
    //part1

    statusthread <- true
    time.Sleep(1 * time.Second)
    data <- "TreeItem1"
    stanvalue <- 25
    //part2
    time.Sleep(1 * time.Second)
    data <- "TreeItem2"
    stanvalue <- 50
    //part3
    time.Sleep(1 * time.Second)
    data <- "TreeItem3"
    stanvalue <- 75
    //part4
    time.Sleep(1 * time.Second)
    data <- "TreeItem4"
    stanvalue <- 100
    statusthread <- false
}
func Extend(slice []string, element string) []string {
    n := len(slice)
    if n == cap(slice) {
        newSlice := make([]string, len(slice), 2*len(slice)+1)
        copy(newSlice, slice)
        slice = newSlice
    }
    slice = slice[0 : n+1]
    slice[n] = element
    return slice
}

type Pilot struct {
    widgets.QMainWindow

    _ func()            `slot:"infoF"`
    _ func(name string) `slot:"addTreeItem"`
    _ func(value int)   `slot:"setValue"`

    ProgresB *widgets.QProgressBar
    TreeW    *widgets.QTreeWidget
}

func IntPilot() *Pilot {
    return NewPilot(nil, 0)
}

func (p *Pilot) infoF() {
    info := widgets.NewQMessageBox(p)
    p.ProgresB.SetValue(0)
    info.SetWindowTitle("Info!!")
    info.SetText(fmt.Sprintf("Data get Compliet"))
    info.Exec()
}
func (p *Pilot) addTreeItem(name string) {
    var itemtest = widgets.NewQTreeWidgetItem3(p.TreeW, 1)
    itemtest.SetText(0, name)

}
func (p *Pilot) StarProgresBar() {

    stanvalue := make(chan int)
    data := make(chan string)
    statusthread := make(chan bool)
    //all_data can be in pilot windows
    var all_data []string
    go Data_Get(stanvalue, data, statusthread)
    for true {

        select {
        case status_info := <-statusthread:
            if status_info {
                fmt.Println("Start Thread")

            } else {
                fmt.Println("Finish Thread")

                fmt.Println(all_data)
                // How to change this to use the slot signal system in qt? Now it is not correct
                p.InfoF()
                break
            }
        case value := <-stanvalue:
            fmt.Println("progres: ", value)
            // How to change this to use the slot signal system in qt? Now it is not correct
            p.SetValue(value)
        case data_for_databases := <-data:
            //How to change this to use the slot signal system in qt? Now it is not correct
            p.AddTreeItem(data_for_databases)
            all_data = Extend(all_data, data_for_databases)
        }

    }

}

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    window := IntPilot()

    var centralwidget = widgets.NewQWidget(window, 0)
    var layout = widgets.NewQVBoxLayout2(centralwidget)
    var button = widgets.NewQToolButton(nil)
    window.TreeW = widgets.NewQTreeWidget(window)
    window.TreeW.SetHeaderLabel("New Item From Thread")
    button.SetText("Run go runtime or thread")
    //window.SetLayout(layout)
    window.SetWindowTitle("Progres Bar")

    window.ProgresB = widgets.NewQProgressBar(window)
    layout.AddWidget(window.TreeW, 0, core.Qt__AlignCenter)
    layout.AddWidget(window.ProgresB, 0, core.Qt__AlignCenter)
    layout.AddWidget(button, 0, core.Qt__AlignCenter)
    button.ConnectClicked(func(flag bool) {
        window.TreeW.ClearDefault()
        go window.StarProgresBar()

    })
    window.SetCentralWidget(centralwidget)

    window.ConnectInfoF(window.infoF)
    window.ConnectAddTreeItem(window.addTreeItem)
    window.ConnectSetValue(window.ProgresB.SetValue)

    window.Show()
    widgets.QApplication_Exec()

}

The issue I initially faced seems to be unrelated.

All 6 comments

Your last example for issues i change because i have a problem witch build:

package main

import (
    "fmt"
    "os"

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

type hStruct struct {
    *core.QObject
    InMain        func()       `signal:"inMain"`
    ConnectInMain func(func()) `slot:"ConnectinMain"`
}

func NewHStruct() *hStruct {
    this := &hStruct{QObject: core.NewQObject(nil)}
    return this
}

func main() {
    core.NewQCoreApplication(len(os.Args), os.Args)

    fmt.Println("current thread (main):", core.QThread_CurrentThread().Pointer())

    go func() { fmt.Println("current thread (go routine):", core.QThread_CurrentThread().Pointer()) }()

    h := NewHStruct()
    h.ConnectInMain(func() { fmt.Println("current thread (hstruct):", core.QThread_CurrentThread().Pointer()) })

    h.InMain()
    go h.InMain()

    core.QCoreApplication_Exec()
}

but i get a panic when I run program after.

This is my example problem:

package main

import (
    "fmt"
    "os"
    "time"
    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
)

func Data_Get(stanvalue chan<- int, data chan<- string, statusthread chan<- bool) {
    //part1

    statusthread <- true
    time.Sleep(1 * time.Second)
    data <- "TreeItem1"
    stanvalue <- 25
    //part2
    time.Sleep(1 * time.Second)
    data <- "TreeItem2"
    stanvalue <- 50
    //part3
    time.Sleep(1 * time.Second)
    data <- "TreeItem3"
    stanvalue <- 75
    //part4
    time.Sleep(1 * time.Second)
    data <- "TreeItem4"
    stanvalue <- 100
    statusthread <- false
}
func Extend(slice []string, element string) []string {
    n := len(slice)
    if n == cap(slice) {
        newSlice := make([]string, len(slice), 2*len(slice)+1)
        copy(newSlice, slice)
        slice = newSlice
    }
    slice = slice[0 : n+1]
    slice[n] = element
    return slice
}

type Pilot struct {
    *widgets.QMainWindow
    ProgresB *widgets.QProgressBar
    TreeW    *widgets.QTreeWidget
}

func IntPilot() *Pilot {
    this := &Pilot{QMainWindow: widgets.NewQMainWindow(nil, 0)}
    return this
}

func (p *Pilot) InfoF() {
    info := widgets.NewQMessageBox(p)
    p.ProgresB.SetValue(0)
    info.SetWindowTitle("Info!!")
    info.SetText(fmt.Sprintf("Data get Compliet"))
    info.Exec()
}
func (p *Pilot) AddTreeItem(name string) {
    var itemtest = widgets.NewQTreeWidgetItem3(p.TreeW, 1)
    itemtest.SetText(0, name)

}
func (p *Pilot) StarProgresBar() {

    stanvalue := make(chan int)
    data := make(chan string)
    statusthread := make(chan bool)
    //all_data can be in pilot windows
    var all_data []string
    go Data_Get(stanvalue, data, statusthread)
    for true {

        select {
        case status_info := <-statusthread:
            if status_info {
                fmt.Println("Start Thread")

            } else {
                fmt.Println("Finish Thread")

                fmt.Println(all_data)
                // How to change this to use the slot signal system in qt? Now it is not correct
                p.InfoF()
                break
            }
        case value := <-stanvalue:
            fmt.Println("progres: ", value)
            // How to change this to use the slot signal system in qt? Now it is not correct
            p.ProgresB.SetValue(value)
        case data_for_databases := <-data:
            //How to change this to use the slot signal system in qt? Now it is not correct
            p.AddTreeItem(data_for_databases)
            all_data = Extend(all_data, data_for_databases)
        }

    }

}

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    window := IntPilot()

    var centralwidget = widgets.NewQWidget(window, 0)
    var layout = widgets.NewQVBoxLayout2(centralwidget)
    var button = widgets.NewQToolButton(nil)
    window.TreeW = widgets.NewQTreeWidget(window)
    window.TreeW.SetHeaderLabel("New Item From Thread")
    button.SetText("Run go runtime or thread")
    //window.SetLayout(layout)
    window.SetWindowTitle("Progres Bar")

    window.ProgresB = widgets.NewQProgressBar(window)
    layout.AddWidget(window.TreeW, 0, core.Qt__AlignCenter)
    layout.AddWidget(window.ProgresB, 0, core.Qt__AlignCenter)
    layout.AddWidget(button, 0, core.Qt__AlignCenter)
    button.ConnectClicked(func(flag bool) {
        window.TreeW.ClearDefault()
        go window.StarProgresBar()

    })
    window.SetCentralWidget(centralwidget)

    window.Show()
    widgets.QApplication_Exec()

}

Hey

Yeah, unlike in Python you will need to use a program called qtmoc first to generate the signals/slots/properties functions for you custom classes.

package main

import (
    "fmt"
    "os"

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

type hStruct struct {
    core.QObject        //<- don't use a * here or qtmoc will ignore this struct
    _            func() `signal:"inMain"` //<- qtmoc will generate ConnectInMain, DisconnectInMain and InMain
}

func main() {
    core.NewQCoreApplication(len(os.Args), os.Args)

    fmt.Println("current thread (main):", core.QThread_CurrentThread().Pointer())

    go func() { fmt.Println("current thread (go routine):", core.QThread_CurrentThread().Pointer()) }()

    h := NewHStruct(nil) //<- will be generated by qtmoc
    h.ConnectInMain(func() { fmt.Println("current thread (hstruct):", core.QThread_CurrentThread().Pointer()) })

    h.InMain()
    go h.InMain()

    core.QCoreApplication_Exec()
}

This should work if you run qtmoc && go build -o test && ./test inside your project dir.

You can also find some additional infos about qtmoc here: https://github.com/therecipe/qt#tools

I also looked into your second example, but there seems to be a bug that affects the subclassing of QMainWindow. I will look into that and report back.

Yes your example work. Thank you for the direction.. I'm so happy now. Sorry for my stupidity. I should read about all qttools.

No problem :)

Also this should work:

package main

import (
    "fmt"
    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
    "os"
    "time"
)

func Data_Get(stanvalue chan<- int, data chan<- string, statusthread chan<- bool) {
    //part1

    statusthread <- true
    time.Sleep(1 * time.Second)
    data <- "TreeItem1"
    stanvalue <- 25
    //part2
    time.Sleep(1 * time.Second)
    data <- "TreeItem2"
    stanvalue <- 50
    //part3
    time.Sleep(1 * time.Second)
    data <- "TreeItem3"
    stanvalue <- 75
    //part4
    time.Sleep(1 * time.Second)
    data <- "TreeItem4"
    stanvalue <- 100
    statusthread <- false
}
func Extend(slice []string, element string) []string {
    n := len(slice)
    if n == cap(slice) {
        newSlice := make([]string, len(slice), 2*len(slice)+1)
        copy(newSlice, slice)
        slice = newSlice
    }
    slice = slice[0 : n+1]
    slice[n] = element
    return slice
}

type Pilot struct {
    widgets.QMainWindow

    _ func()            `slot:"infoF"`
    _ func(name string) `slot:"addTreeItem"`
    _ func(value int)   `slot:"setValue"`

    ProgresB *widgets.QProgressBar
    TreeW    *widgets.QTreeWidget
}

func IntPilot() *Pilot {
    return NewPilot(nil, 0)
}

func (p *Pilot) infoF() {
    info := widgets.NewQMessageBox(p)
    p.ProgresB.SetValue(0)
    info.SetWindowTitle("Info!!")
    info.SetText(fmt.Sprintf("Data get Compliet"))
    info.Exec()
}
func (p *Pilot) addTreeItem(name string) {
    var itemtest = widgets.NewQTreeWidgetItem3(p.TreeW, 1)
    itemtest.SetText(0, name)

}
func (p *Pilot) StarProgresBar() {

    stanvalue := make(chan int)
    data := make(chan string)
    statusthread := make(chan bool)
    //all_data can be in pilot windows
    var all_data []string
    go Data_Get(stanvalue, data, statusthread)
    for true {

        select {
        case status_info := <-statusthread:
            if status_info {
                fmt.Println("Start Thread")

            } else {
                fmt.Println("Finish Thread")

                fmt.Println(all_data)
                // How to change this to use the slot signal system in qt? Now it is not correct
                p.InfoF()
                break
            }
        case value := <-stanvalue:
            fmt.Println("progres: ", value)
            // How to change this to use the slot signal system in qt? Now it is not correct
            p.SetValue(value)
        case data_for_databases := <-data:
            //How to change this to use the slot signal system in qt? Now it is not correct
            p.AddTreeItem(data_for_databases)
            all_data = Extend(all_data, data_for_databases)
        }

    }

}

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    window := IntPilot()

    var centralwidget = widgets.NewQWidget(window, 0)
    var layout = widgets.NewQVBoxLayout2(centralwidget)
    var button = widgets.NewQToolButton(nil)
    window.TreeW = widgets.NewQTreeWidget(window)
    window.TreeW.SetHeaderLabel("New Item From Thread")
    button.SetText("Run go runtime or thread")
    //window.SetLayout(layout)
    window.SetWindowTitle("Progres Bar")

    window.ProgresB = widgets.NewQProgressBar(window)
    layout.AddWidget(window.TreeW, 0, core.Qt__AlignCenter)
    layout.AddWidget(window.ProgresB, 0, core.Qt__AlignCenter)
    layout.AddWidget(button, 0, core.Qt__AlignCenter)
    button.ConnectClicked(func(flag bool) {
        window.TreeW.ClearDefault()
        go window.StarProgresBar()

    })
    window.SetCentralWidget(centralwidget)

    window.ConnectInfoF(window.infoF)
    window.ConnectAddTreeItem(window.addTreeItem)
    window.ConnectSetValue(window.ProgresB.SetValue)

    window.Show()
    widgets.QApplication_Exec()

}

The issue I initially faced seems to be unrelated.

Thank you very much again. This example satisfies me.... And i close issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amlwwalker picture amlwwalker  路  3Comments

tjma2001 picture tjma2001  路  3Comments

woodyjon picture woodyjon  路  5Comments

StarAurryon picture StarAurryon  路  5Comments

Litarvan picture Litarvan  路  3Comments