问题描述得不好,大家见谅。
我的主界面操作需要 grabKeyboard() 并过滤掉按键的 autorepeat 。
test.cpp
[cpp]
#include <QDebug>
#include <QVBoxLayout>
#include "test.h"
extern int hotkeyGo;
Test::Test(QWidget *parent)
: QWidget(parent)
{
grabKeyboard();
button = new QPushButton(this);
button->setText("press key " + QKeySequence(hotkeyGo).toString(QKeySequence::NativeText) + " to go" );
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(button);
setLayout(layout);
}
void Test::go(){
qDebug("Go");
}
void Test::stop(){
qDebug(" ");
}
void Test::keyPressEvent(QKeyEvent *event){
if (!event->isAutoRepeat()){
if(event->key() == hotkeyGo){
this->go();
}
}
}
void Test::keyReleaseEvent(QKeyEvent *event){
if (!event->isAutoRepeat()){
if(event->key() == hotkeyGo){
this->stop();
}
}
}
[/cpp]
在自行定义热键的界面中,让用户按任意键设置热键,我也用类似的方法即 grabKeyboard 来确定按键:
catchkey.cpp
[cpp]
#include <QLabel>
#include <QGridLayout>
#include "catchkey.h"
CatchKey::CatchKey(QDialog *parent)
: QDialog(parent)
{
grabKeyboard();
QLabel* output = new QLabel(tr("Press a Key"));
QGridLayout* layout = new QGridLayout(this);
layout->addWidget(output);
}
int CatchKey::getKey(){
return this->val;
}
void CatchKey::keyPressEvent(QKeyEvent* event){
this->val = event->key();
this->close();
}
[/cpp]
按键是可以确定了,但是关闭定制热键的界面(即按下 Done)后,在主界面无论按新热键和老热键都没反应。是不是 grabKeyboard 没有回到主界面上?或许需要别的方法来实现?
还有几个问题:
1. 保存热键的变量 hotkeyGo 是 int 类型,用 QKeySequence(hotkeyGo).toString(QKeySequence::NativeText) 可以显示键的名称,比如在按钮文字中,上键显示为“Up”。但是在 qDebug 中,QKeySequence(hotkeyGo).toString(QKeySequence::NativeText).data() 只能显示出“U”。囧。
2. 如果有好几个热键要设置,判断“一个键是否已经在定制其它热键中使用掉了”该怎么实现?给个思路。
以下是我的测试界面和源代码。谢谢。
qt 定制热键的按键难题,有关 grabKeyboard()
- leni
- 帖子: 1989
- 注册时间: 2008-09-28 17:24
qt 定制热键的按键难题,有关 grabKeyboard()
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
- Kandu
- 帖子: 108
- 注册时间: 2008-12-24 12:02
- 联系:
Re: qt 定制热键的按键难题,有关 grabKeyboard()
代码: 全选
diff -Nur keyboard/confighotkey.cpp keyboard_fixes/confighotkey.cpp
--- keyboard/confighotkey.cpp 2013-01-01 20:42:50.000000000 +0800
+++ keyboard_fixes/confighotkey.cpp 2013-01-02 13:01:35.852687312 +0800
@@ -3,8 +3,10 @@
#include <QKeySequence>
#include "confighotkey.h"
+#include "test.h"
extern int hotkeyGo;
+extern Test* default_test;
ConfigHotkey::ConfigHotkey(QWidget *parent)
: QWidget(parent)
@@ -26,4 +28,6 @@
this->getKey->exec();
hotkeyGo = getKey->getKey();
qDebug( "key %i : %s", hotkeyGo, QKeySequence(hotkeyGo).toString(QKeySequence::NativeText).data() );
-}
\ No newline at end of file
+ default_test->grabKeyboard();
+ default_test->resetText();
+}
diff -Nur keyboard/test.cpp keyboard_fixes/test.cpp
--- keyboard/test.cpp 2013-01-01 21:14:18.000000000 +0800
+++ keyboard_fixes/test.cpp 2013-01-02 13:02:09.872687407 +0800
@@ -4,10 +4,12 @@
#include "test.h"
extern int hotkeyGo;
+Test* default_test;
Test::Test(QWidget *parent)
: QWidget(parent)
{
+ default_test= this;
grabKeyboard();
button = new QPushButton(this);
@@ -19,6 +21,9 @@
}
+void Test::resetText(void) {
+ button->setText("press key " + QKeySequence(hotkeyGo).toString(QKeySequence::NativeText) + " to go" );
+}
void Test::go(){
qDebug("Go");
diff -Nur keyboard/test.h keyboard_fixes/test.h
--- keyboard/test.h 2013-01-01 20:04:16.000000000 +0800
+++ keyboard_fixes/test.h 2013-01-02 13:01:58.692687376 +0800
@@ -5,13 +5,13 @@
#include <QPushButton>
#include <QKeyEvent>
-
class Test : public QWidget
{
Q_OBJECT
public:
Test(QWidget *parent = 0);
+ void resetText(void);
private:
QPushButton *button;
上次由 Kandu 在 2015-08-05 12:43,总共编辑 1 次。
- Kandu
- 帖子: 108
- 注册时间: 2008-12-24 12:02
- 联系:
Re: qt 定制热键的按键难题,有关 grabKeyboard()
若有疑問,在 Qt Assistant 仔細看一遍文件 void QWidget::grabKeyboard () XD
- Kandu
- 帖子: 108
- 注册时间: 2008-12-24 12:02
- 联系:
Re: qt 定制热键的按键难题,有关 grabKeyboard()
第一個問題已在 diff 中改好, resetText. 第二個問題的話,用 std::set 或者 QSet 都可以吧。
- leni
- 帖子: 1989
- 注册时间: 2008-09-28 17:24
Re: qt 定制热键的按键难题,有关 grabKeyboard()
ls 真是 Qt 大师啊
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。