gpt4 book ai didi

qt - 尝试将 shell 命令的实时/实时输出作为字符串数据从函数返回到主函数

转载 作者:IT王子 更新时间:2023-10-29 01:57:35 24 4
gpt4 key购买 nike

不确定如何解释这个,因为我在 GO 中使用 QT 绑定(bind)所以我粘贴了一个迷你版的程序。我正在尝试从 run() 函数返回一个实时流到 QT 窗口。我尝试了很多方法......最后一个有 channel (不成功)将实时输出传递给 main() 函数以便我的 QT Slot 可以更新窗口的最佳方法是什么?

主要包

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

func run(input string) string {

stream := make(chan string)

fmt.Printf("You Clicked The Push Button %s\n", input)

cmdName := "/usr/bin/nikto"
cmdArgs := []string{"-host", input}

cmd := exec.Command(cmdName, cmdArgs...)
cmdReader, err := cmd.StdoutPipe()
if err != nil {
fmt.Fprintln(os.Stderr, "Error creating StdoutPipe for Cmd", err)
os.Exit(1)
}

scanner := bufio.NewScanner(cmdReader)
go func() {
for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
stream <- scanner.Text()
//stream = fmt.Sprintf("%s\n", scanner.Text())
}
}()

err = cmd.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
os.Exit(1)
}

err = cmd.Wait()
if err != nil {
fmt.Fprintln(os.Stderr, "Error waiting for Cmd", err)
os.Exit(1)
}

input = <-stream
return string(input)
//return go getOutput(scanner)

}

func main() {
// Create application
app := widgets.NewQApplication(len(os.Args), os.Args)

// Create main window
window := widgets.NewQMainWindow(nil, 0)
window.SetWindowTitle("nikto front end")
window.SetMinimumSize2(400, 400)

// Create layout

Layout := widgets.NewQVBoxLayout()

TopLayout := widgets.NewQHBoxLayout()
//topright := widgets.NewQHBoxLauout()

RLayout := widgets.NewQVBoxLayout()
LLayout := widgets.NewQVBoxLayout()

Layout.AddLayout(TopLayout, 0)
Layout.AddLayout(RLayout, 1)
Layout.AddLayout(LLayout, 0)

//Create main widget
mainWidget := widgets.NewQWidget(nil, 0)
mainWidget.SetLayout(Layout)

// Create left widget
LQWidget := widgets.NewQWidget(nil, 0)
LQWidget.SetLayout(LLayout)

// Create right widget

RQWidget := widgets.NewQWidget(nil, 0)
RQWidget.SetLayout(RLayout)

// Create label
urlLabel := widgets.NewQLabel(nil, 0)
urlLabel.SetText("Target: ")
TopLayout.AddWidget(urlLabel, 0, 0)

// Create a line edit
input := widgets.NewQLineEdit(nil)
input.SetPlaceholderText("Enter target like http://127.0.0.1")
TopLayout.AddWidget(input, 0, 0)

// Create checkboxes
checkBox1 := widgets.NewQCheckBox2("Default", nil)
//checkBox1.SetWindowTitle("Check Box")
LLayout.AddWidget(checkBox1, 0, 0)

checkBox2 := widgets.NewQCheckBox2("SSL mode", nil)
//checkBox2.SetWindowTitle("Check Box")
LLayout.AddWidget(checkBox2, 0, 0)

checkBox3 := widgets.NewQCheckBox2("no 404", nil)
//checkBox3.SetWindowTitle("Check Box")
LLayout.AddWidget(checkBox3, 0, 0)

output := widgets.NewQTextEdit(nil)
LLayout.AddWidget(output, 0, 0)

// Create a button and add it to the layout
button1 := widgets.NewQPushButton2("2. click me", nil)
Layout.AddWidget(button1, 0, 0)

button1.ConnectClicked(func(checked bool) {
output.Append(run(input.Text()))
})

// Set main widget as the central widget of the window
window.SetCentralWidget(mainWidget)

// Show the window
window.Show()

// Execute app
app.Exec()
}

最佳答案

run 返回 channel ,然后在 main 中读取它(不要从 run 中的 channel 读取):

package main

import (
"bufio"
"fmt"
"os/exec"
)

func main() {
// ...

button1.ConnectClicked(func(checked bool) {
stream := run(input.Text())
go func() {
for line := range stream {
output.Append(line)
}
}()
})

// ...
}

func run(host string) <-chan string {

var cmd *exec.Cmd
cmdReader, err := cmd.StdoutPipe()
if err != nil {
// ...
}

scanner := bufio.NewScanner(cmdReader)
stream := make(chan string)

go func() {
defer close(stream)

for scanner.Scan() {
fmt.Printf("%s\n", scanner.Text())
stream <- scanner.Text()
}
}()

go func() {
if err := cmd.Run(); err != nil {
// ...
}
}()

return stream
}

关于qt - 尝试将 shell 命令的实时/实时输出作为字符串数据从函数返回到主函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com