问题描述
我是Qt GUI的新手,最初想配置一个Qt Dock Widget,当按下一个键时显示它,比如说"A",而在按下另一个键时则隐藏,比如说"B".但是经过一些研究,我没有找到任何相关的解决方案.
I'm new to Qt GUI and initially wanted to configure a Qt Dock Widget which is shown when pressed a key, let's say 'A' and is hidden when pressed another key, let's say 'B'. But after researching a bit, I didn't find any relevant solution.
我尝试创建一个切换按钮,该按钮在第一次按下时将显示停靠窗口小部件,在再次按下时将其隐藏.它工作正常,但是有什么办法可以做得更好,或者分配任何键来显示和隐藏底座窗口小部件?
I tried creating a toggle button which when pressed first will show the dock widget and when pressed again will hide it. It's working fine but Is there any way to do it better or assigning any key for showing and hiding dock widget?
t_button = new QPushButton("B1",this);
dockB = new QDockWidget(tr("Panel B"),this);
dockB -> setAllowedAreas(Qt::AllDockWidgetAreas);
addDockWidget(Qt::RightDockWidgetArea,dockB);
dockB -> hide();
connect(t_button,SIGNAL(clicked()),this,SLOT(toggle()));
void MainWindow::toggle()
{
if(!click)
dockB->show();
else
dockB->hide();
click=!click;
}
推荐答案
要将按键绑定到操作, QShortCut 负责.
To bind key presses to actions, QShortCut is responsible.
在某些小部件上,使用'&'字符前面的字符会自动为该字符创建一个助记符(快捷方式),例如"E& xit"将创建快捷键Alt + X(使用&&"显示实际的&"号).该小部件可能会消耗并在给定的快捷方式上执行操作.在X11上,将不显示与号,并在字符下划线.在Windows上,通常不会显示快捷方式,除非用户按下Alt键,但这是用户可以更改的设置.在Mac上,默认情况下禁用快捷方式.调用qt_set_sequence_auto_mnemonic()启用它们.但是,由于助记符快捷方式不符合Aqua的准则,因此Qt不会在带下划线的快捷方式字符上显示
On certain widgets, using '&' in front of a character will automatically create a mnemonic (a shortcut) for that character, e.g. "E&xit" will create the shortcut Alt+X (use '&&' to display an actual ampersand). The widget might consume and perform an action on a given shortcut. On X11 the ampersand will not be shown and the character will be underlined. On Windows, shortcuts are normally not displayed until the user presses the Alt key, but this is a setting the user can change. On Mac, shortcuts are disabled by default. Call qt_set_sequence_auto_mnemonic() to enable them. However, because mnemonic shortcuts do not fit in with Aqua's guidelines, Qt will not show the shortcut character underlined.
对于使用菜单的应用程序,使用QMenu类中提供的便利功能在创建菜单项时将键盘快捷方式分配给菜单项可能更方便.另外,快捷方式可以与QAction类中的其他类型的动作相关联.
For applications that use menus, it may be more convenient to use the convenience functions provided in the QMenu class to assign keyboard shortcuts to menu items as they are created. Alternatively, shortcuts may be associated with other types of actions in the QAction class.
为特定窗口小部件创建快捷方式的最简单方法是使用键序列构造快捷方式.例如:
The simplest way to create a shortcut for a particular widget is to construct the shortcut with a key sequence. For example:
shortcut = new QShortcut(QKeySequence(tr("Ctrl+O", "File|Open")),
parent);
当用户键入给定快捷方式的键序列时,将发出快捷方式的Activated()信号. (在模棱两可的情况下,会发出ActivateAmbiguously()信号.)当快捷方式的父窗口小部件正在接收事件时,Qt的事件循环会监听"该快捷方式.
When the user types the key sequence for a given shortcut, the shortcut's activated() signal is emitted. (In the case of ambiguity, the activatedAmbiguously() signal is emitted.) A shortcut is "listened for" by Qt's event loop when the shortcut's parent widget is receiving events.
小样本testQDockWidgetShortCut.cc
:
#include <QtWidgets>
int main(int argc, char **argv)
{
qDebug() << "Qt Version:" << QT_VERSION_STR;
QApplication app(argc, argv);
// setup GUI
QMainWindow qMainWin;
QDockWidget qDockB("Panel B");
qDockB.setAllowedAreas(Qt::AllDockWidgetAreas);
qMainWin.addDockWidget(Qt::RightDockWidgetArea, &qDockB);
qDockB.hide();
// a window action to show dock on [A]
QAction qCmdShowDockB(&qMainWin);
qCmdShowDockB.setShortcut(QKeySequence("A"));
qMainWin.addAction(&qCmdShowDockB);
// a window action to hide dock on [B]
QAction qCmdHideDockB(&qMainWin);
qCmdHideDockB.setShortcut(QKeySequence("B"));
qMainWin.addAction(&qCmdHideDockB);
// a button to toggle dock B
QPushButton qBtn(
"Show/Hide Panel B\n"
"[A] ... Show Panel B\n"
"[B] ... Hide Panel B\n"
"[Ctrl+B] ... Toggle Panel B");
qBtn.setShortcut(QKeySequence("Ctrl+B"));
qMainWin.setCentralWidget(&qBtn);
qMainWin.show();
// install signal handlers
QAction *pQCmd = qDockB.toggleViewAction();
QObject::connect(&qBtn, &QPushButton::clicked, pQCmd, &QAction::trigger);
QObject::connect(&qCmdShowDockB, &QAction::triggered, &qDockB, &QDockWidget::show);
QObject::connect(&qCmdHideDockB, &QAction::triggered, &qDockB, &QDockWidget::hide);
// runtime loop
return app.exec();
}
最小项目文件testQDockWidgetShortCut.pro
:
SOURCES = testQDockWidgetShortCut.cc
QT += widgets
在 cygwin64 上进行编译和测试:
$ qmake-qt5 testQDockWidgetShortCut.pro
$ make && ./testQDockWidgetShortCut
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQDockWidgetShortCut.o testQDockWidgetShortCut.cc
g++ -o testQDockWidgetShortCut.exe testQDockWidgetShortCut.o -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread
Qt Version: 5.9.4
编译了在VS2017和Qt5.13.0中测试过的内容:
Compiled an tested in VS2017 and Qt5.13.0:
Qt Version: 5.13.0
(在两种情况下,我都测试了所有提供的快捷键:,, + ,然后单击按钮.)
(In both cases, I tested all offered shortcut keys: , , +, as well as clicking the button.)
我想知道OP声称
也许,使用快捷方式"一词可能会更容易或加速键".否则,OP应该已经找到了一些东西,例如
May be, this would have been easier with the term “shortcut” or “accelerator key”. Otherwise, OP should have found something, e.g.
我认为可能是重复的
这篇关于如何正确配置QDockWidget以使其显示/隐藏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!