QxtGlobalShortcut 在控制台应用程序中导致 SIGSEGV
我正在编写一个控制台应用程序,允许您使用 ECMAScript 来移动鼠标,并定义热键等。鼠标的东西可以工作,现在我正在尝试添加定义热键的功能,按下该热键将调用 QtScript处理函数。
问题是,当我尝试使用 QxtGlobalShortcut 创建热键时,我收到 SIGSEGV 并且一切都崩溃了。
这是代码:
int Keyboard::hot_key(QString key) {
QxtGlobalShortcut shortcut;
shortcut.setShortcut(QKeySequence(key));
shortcut.setEnabled(true);
connect(&shortcut, SIGNAL(activated()), this, SLOT(hot_key_pressed()));
return 0;
}
这一行QxtGlobalShortcut快捷方式;
,如果我删除除该行之外的整个函数,它仍然会抛出段错误。
调试器说:
1 ZNK17QxtGlobalShortcut8shortcutEv C:\Qxt\lib\QxtGui.dll 0 0x6f6f14a6
2 ZN17QxtGlobalShortcutC1EP7QObject C:\Qxt\lib\QxtGui.dll 0 0x6f6f14f7
3 Keyboard::hot_key keyboard.cpp 16 0x403c0c
4 Keyboard::qt_metacall moc_keyboard.cpp 74 0x404740
5 QMetaObject::metacall qmetaobject.cpp 237 0x8f5ff8
考虑到在我看来 Windows 热键并不那么容易,我真的很想使用 Qxt,但到目前为止我只是碰壁。如果有人能给我指出正确的方向,或者推荐一些其他可能对我有帮助的库,或者关于热键的教程,那就太棒了。
提前致谢!
I am writing a console application that allows you to use ECMAScript to move the mouse about, and define hot keys etc. The mouse stuff works, and now I am trying to add in the ability to define hot keys that when pressed will call a QtScript handler function.
The problem is, when I try to use QxtGlobalShortcut to create the hot key, I get a SIGSEGV and everything crashes.
Here is the code:
int Keyboard::hot_key(QString key) {
QxtGlobalShortcut shortcut;
shortcut.setShortcut(QKeySequence(key));
shortcut.setEnabled(true);
connect(&shortcut, SIGNAL(activated()), this, SLOT(hot_key_pressed()));
return 0;
}
This line QxtGlobalShortcut shortcut;
, if I gut the entire function except for that line, it still throws a seg fault.
And the debugger says:
1 ZNK17QxtGlobalShortcut8shortcutEv C:\Qxt\lib\QxtGui.dll 0 0x6f6f14a6
2 ZN17QxtGlobalShortcutC1EP7QObject C:\Qxt\lib\QxtGui.dll 0 0x6f6f14f7
3 Keyboard::hot_key keyboard.cpp 16 0x403c0c
4 Keyboard::qt_metacall moc_keyboard.cpp 74 0x404740
5 QMetaObject::metacall qmetaobject.cpp 237 0x8f5ff8
Considering how not so easy, in my opinion, Windows hotkeys are, I'd really like to use Qxt, but so far I am just hitting a wall. If anyone could point me in the right direction, or even recommend some other library that might be able to help me, or a tutorial on hotkeys that would be awesome.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
shortcut
对象是在堆栈上构造的。一旦您离开该函数,它就会被删除,因此即使您的代码没有在该函数中死亡,它也必然会失败。您应该使用以下方式创建该对象:
如果您关心不泄漏该对象,请将该指针存储为类的成员,并在其析构函数中
删除
它。至于崩溃,如果您使用的 Qxt 版本早于 6.0 并且尚未初始化
QxtApplication
,则可能会发生这种情况。Your
shortcut
object is constructed on the stack. It will get deleted as soon you leave that function, so your code is bound to fail even if it did not die in that function.You should be creating that object with:
If you care about not leaking that, store that pointer as a member of your class and
delete
it in it's destructor.As for the crash, that could happen if you are using a version of Qxt older than 6.0 and haven't initialized a
QxtApplication
yet.