qt 定制热键的按键难题,有关 grabKeyboard()
发表于 : 2013-01-02 4:29
问题描述得不好,大家见谅。
我的主界面操作需要 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. 如果有好几个热键要设置,判断“一个键是否已经在定制其它热键中使用掉了”该怎么实现?给个思路。
以下是我的测试界面和源代码。谢谢。
我的主界面操作需要 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. 如果有好几个热键要设置,判断“一个键是否已经在定制其它热键中使用掉了”该怎么实现?给个思路。
以下是我的测试界面和源代码。谢谢。