Golang中学习therecipe/qt遇到问题求助
Golang中学习therecipe/qt遇到问题求助 我正在尝试学习如何在Go中使用Qt。我正在学习一本书,书中使用了C++示例,我正尝试将其翻译成Go。目前我在QKeySequences这里卡住了。
有人能给我指明正确的方向吗?我的电脑运行的是ubuntu 18.04。
谢谢
4 回复
供记录:您使用的是 https://therecipe.github.io/qt/
能否展示一下您遇到问题的代码片段?
更多关于Golang中学习therecipe/qt遇到问题求助的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
MainWindow::MainWindow()
{
setWindowTitle("SRM System");
setFixedSize(500, 500);
QPixmap newIcon("new.png");
QPixmap openIcon("open.png");
QPixmap closeIcon("close.png");
// 设置文件菜单
fileMenu = menuBar()->addMenu("&File");
quitAction = new QAction(closeIcon, "Quit", this);
quitAction->setShortcuts(QKeySequence::Quit);
newAction = new QAction(newIcon, "&New", this);
newAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_C));
openAction = new QAction(openIcon, "&New", this);
openAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_O));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addSeparator();
fileMenu->addAction(quitAction);
helpMenu = menuBar()->addMenu("Help");
aboutAction = new QAction("About", this);
aboutAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_H));
helpMenu->addAction(aboutAction);
// 设置信号与槽
connect(quitAction, &QAction::triggered, this, &QApplication::quit);
}
在Go中使用therecipe/qt处理QKeySequence时,需要注意Qt的C++ API与Go绑定的差异。以下是一个将常见Qt快捷键转换为Go代码的示例:
package main
import (
"os"
"github.com/therecipe/qt/widgets"
"github.com/therecipe/qt/gui"
)
func main() {
app := widgets.NewQApplication(len(os.Args), os.Args)
// 创建主窗口
window := widgets.NewQMainWindow(nil, 0)
window.SetWindowTitle("Qt KeySequence示例")
window.SetMinimumSize2(400, 300)
// 创建菜单栏
menuBar := window.MenuBar()
fileMenu := menuBar.AddMenu2("文件(&F)")
// 创建动作并设置快捷键
newAction := widgets.NewQAction2("新建(&N)", window)
// 设置快捷键 - 使用QKeySequence的标准快捷键
newAction.SetShortcut(gui.NewQKeySequence2("Ctrl+N", gui.QKeySequence__NativeText))
openAction := widgets.NewQAction2("打开(&O)", window)
openAction.SetShortcut(gui.QKeySequence__Open)
saveAction := widgets.NewQAction2("保存(&S)", window)
saveAction.SetShortcut(gui.QKeySequence__Save)
quitAction := widgets.NewQAction2("退出(&Q)", window)
quitAction.SetShortcut(gui.QKeySequence__Quit)
// 将动作添加到菜单
fileMenu.AddActions([]*widgets.QAction{newAction, openAction, saveAction})
fileMenu.AddSeparator()
fileMenu.AddAction(quitAction)
// 连接信号
quitAction.ConnectTriggered(func(bool) {
app.Quit()
})
window.Show()
app.Exec()
}
对于自定义快捷键,可以使用以下方式:
// 自定义快捷键
customAction := widgets.NewQAction2("自定义动作", window)
// 方式1:使用字符串
customAction.SetShortcut(gui.NewQKeySequence2("Ctrl+Shift+T", gui.QKeySequence__NativeText))
// 方式2:使用键组合
customAction.SetShortcut(gui.NewQKeySequenceFromString("Alt+F4"))
// 方式3:使用标准快捷键枚举
copyAction := widgets.NewQAction2("复制", window)
copyAction.SetShortcut(gui.QKeySequence__Copy)
cutAction := widgets.NewQAction2("剪切", window)
cutAction.SetShortcut(gui.QKeySequence__Cut)
pasteAction := widgets.NewQAction2("粘贴", window)
pasteAction.SetShortcut(gui.QKeySequence__Paste)
如果需要处理键盘事件,可以这样实现:
// 在自定义widget中处理键盘事件
type CustomWidget struct {
*widgets.QWidget
}
func NewCustomWidget() *CustomWidget {
widget := &CustomWidget{widgets.NewQWidget(nil, 0)}
widget.SetFocusPolicy(qt.Core.Qt__StrongFocus)
return widget
}
func (w *CustomWidget) KeyPressEvent(event *gui.QKeyEvent) {
// 检查快捷键组合
if event.Modifiers() == qt.Core.Qt__ControlModifier && event.Key() == qt.Core.Qt__Key_S {
// 处理Ctrl+S
println("保存操作被触发")
} else if event.Key() == qt.Core.Qt__Key_Escape {
// 处理ESC键
println("退出操作被触发")
}
w.QWidget.KeyPressEvent(event)
}
在Ubuntu 18.04上,确保已安装必要的Qt开发依赖:
sudo apt-get update
sudo apt-get install qt5-default libqt5svg5-dev
对于therecipe/qt,还需要设置环境变量:
export QT_PKG_CONFIG=true
编译时使用正确的标签:
go build -tags=qt5
这些示例展示了在Go中如何使用therecipe/qt处理QKeySequence,包括标准快捷键、自定义快捷键和键盘事件处理。

