• 12488阅读
  • 10回复

如何使用快捷键显示一个隐藏的窗口? [复制链接]

上一主题 下一主题
离线bingogo
 
只看楼主 倒序阅读 楼主  发表于: 2008-10-18
— 本帖被 XChinux 执行加亮操作(2008-11-02) —

在用QT4做一个小程序,
建立一个窗口
并使用快捷键,让它隐藏和显示。

现在的问题是,
用快捷键可以隐藏窗口,
但是再使用快捷键,却显示不了窗口

怎么回事呢?

给点意见吧,先谢谢大家了!
离线bingogo
只看该作者 1楼 发表于: 2008-10-18
PS:我没有使用QAction,而是直接用QShortcut类新建了一个快捷键

看了XChinux版主之前的帖子
--------------------------------------------
Quote:

shortcutContext : Qt::ShortcutContext
This property holds the context for the action's shortcut.
Valid values for this property can be found in Qt::ShortcutContext. The default value is Qt::WindowShortcut.
Access functions:
Qt::ShortcutContext shortcutContext () const
void setShortcutContext ( Qt::ShortcutContext context )

enum Qt::ShortcutContext
For a QEvent::Shortcut event to occur, the shortcut's key sequence must be entered by the user in a context where the shortcut is active. The possible contexts are these:

Constant
Value
Description
Qt::WidgetShortcut
0
The shortcut is only triggered when its parent widget has focus.
Qt::WindowShortcut
1
The shortcut is triggered when its parent widget is a logical subwidget of the active top-level window.
Qt::ApplicationShortcut
2
The shortcut is triggered when the application is active.

-----------------------------------------------------------------------------------------
离线bingogo
只看该作者 2楼 发表于: 2008-10-18
默认窗口是显示的,
我先用isVisible ( )判断窗口是否显示,
用来隐藏窗口的函数是 setVisible(false),
尝试用过hide( ),隐藏正常,可还是没办法显示。

是不是当时键盘输入的focus不在窗口上的原因呢?
但是
-------------------------
void QWidget::setFocus ( Qt::FocusReason reason )
Gives the keyboard input focus to this widget (or its focus proxy) if this widget or one of its parents is the active window. The reason argument will be passed into any focus event sent from this function, it is used to give an explanation of what caused the widget to get focus.

Be aware that if the widget is hidden, it will not accept focus until it is shown.

----------------------------------------
看起来,是行不通的。


如果不是活动窗口,应该是接受不了快捷键的吧?
[ 此贴被bingogo在2008-10-20 16:49重新编辑 ]
离线bingogo
只看该作者 3楼 发表于: 2008-10-20
各位前辈,
怎么样才能让我的隐藏窗口接收到键盘输入的快捷键呢?

-------------
我的操作系统是Fedora 8 ,桌面管理器是GNOME
离线chaoscxm
只看该作者 4楼 发表于: 2008-10-20
你分析的原因是对的。要解决这个方法,可能需要中断的编程,这个我也不清楚,上网查一下吧。
帖子签名(将在每个帖子下方显示)
离线bingogo
只看该作者 5楼 发表于: 2008-10-20
QT的API里好像没有提供绑定系统快捷键的函数

>> enum Qt::ShortcutContext
>> For a QEvent::Shortcut event to occur, the shortcut's key sequence
>> must be entered by the user in a context where the shortcut is active.
>> The possible contexts are these:
>> Constant    Value    Description

>> Qt::WidgetShortcut    0    The shortcut is active when its parent
>> widget has focus.

>> Qt::WindowShortcut    1    The shortcut is active when its parent
>> widget is a logical subwidget of the active top-level window.

>> Qt::ApplicationShortcut    2    The shortcut is active when one of
>> the applications windows are active.

我的程序只有一个窗口,其实也可以建立另一个窗口+按钮让它Show/Hide,
但是我还是想知道
在只有一个窗口(隐藏)的情况下,怎么解决键盘输入的问题呢?
离线bingogo
只看该作者 6楼 发表于: 2008-10-20
引用第4楼chaoscxm于2008-10-20 17:03发表的  :
你分析的原因是对的。要解决这个方法,可能需要中断的编程,这个我也不清楚,上网查一下吧。



谢谢回帖 ……  ……
^ ^
离线sbtree
只看该作者 7楼 发表于: 2008-10-20
估计要重新实现函数bool QApplication::x11EventFilter ( XEvent * )
windows 7 + VC++2008 + Qt4.5.2
离线bingogo
只看该作者 8楼 发表于: 2008-11-08
引用第7楼sbtree于2008-10-20 21:28发表的  :
估计要重新实现函数bool QApplication::x11EventFilter ( XEvent * )


谢谢提示~

我用bool QApplication::x11EventFilter ( XEvent * )
只能捕捉到鼠标的移动事件,不能截获键盘的输入事件。
还是键盘焦点问题~
离线bingogo
只看该作者 9楼 发表于: 2008-11-08
最后还是用Xlib来解决了!

有需要实现类似功能的的朋友,
可以参考(其实很简单)
Simple Java JNI Tutorial: Global Keypress Capture (Hotkey) using X11
----------
http://ubuntuforums.org/showthread.php?t=864566
----------
--主要使用了XGrabKey


在实现过程中,有可能遇到,窗口隐藏后可以成功显示,但不能再次隐藏的问题
原因是,快捷键被冻结,简单的使用XUnGrabKey可能不能解决。
可参考LICQ的源码中关于global accelerator的部分
https://svn.ejohansson.se/repos/licq/trunk/qt-gui/src/licqgui.cpp
使用下列函数
    XAllowEvents(*****);
    XUngrabKeyboard(****);
    XSync(*****);



另:
QT中似乎也有提供(Qwidget??)grabkey的类似功能,
不过我没有试过,尝试过的朋友可以来说说。
之前论坛上还有用XSetInputFocus来实现的,我也没有试过。
离线wxg0130

只看该作者 10楼 发表于: 2009-03-26
我在Windows下面也遇到了同样的问题,在qt3下面还可以正常工作,转到qt4.4.2下面,就出现隐藏的窗口不能使用快捷键了,看了你的分析,还是没有解决,不过还是谢谢,至少提供了思路
Everything is Nothing
快速回复
限100 字节
 
上一个 下一个