如何正确配置QDockWidget 到show/hide?
How to correctly configure QDockWidget for it to show/hide?
我是 Qt GUI 的新手,最初想配置一个 Qt Dock Widget,它在按下一个键时显示,比方说 'A',在按下另一个键时隐藏,比方说 'B'.但是经过一番研究,我没有找到任何相关的解决方案。
我尝试创建一个切换按钮,第一次按下时会显示停靠栏小部件,再次按下时会隐藏它。它工作正常,但有什么方法可以做得更好或分配任何键来显示和隐藏停靠栏小部件?
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 负责。
The QShortcut class provides a way of connecting keyboard shortcuts to Qt's signals and slots mechanism, so that objects can be informed when a shortcut is executed. The shortcut can be set up to contain all the key presses necessary to describe a keyboard shortcut, including the states of modifier keys such as Shift, Ctrl, and Alt.
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.
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);
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 中编译测试:
Qt Version: 5.13.0
(在这两种情况下,我测试了所有提供的快捷键:A、B、Ctrl+B,以及点击按钮。)
我想知道 OP 声称
But after researching a bit, I didn't find any relevant solution.
也许,使用术语“快捷键”或“加速键”会更容易。否则,OP 应该已经找到了一些东西,例如
我认为这可能是重复的。
我是 Qt GUI 的新手,最初想配置一个 Qt Dock Widget,它在按下一个键时显示,比方说 'A',在按下另一个键时隐藏,比方说 'B'.但是经过一番研究,我没有找到任何相关的解决方案。
我尝试创建一个切换按钮,第一次按下时会显示停靠栏小部件,再次按下时会隐藏它。它工作正常,但有什么方法可以做得更好或分配任何键来显示和隐藏停靠栏小部件?
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 负责。
The QShortcut class provides a way of connecting keyboard shortcuts to Qt's signals and slots mechanism, so that objects can be informed when a shortcut is executed. The shortcut can be set up to contain all the key presses necessary to describe a keyboard shortcut, including the states of modifier keys such as Shift, Ctrl, and Alt.
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.
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);
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 中编译测试:
Qt Version: 5.13.0
(在这两种情况下,我测试了所有提供的快捷键:A、B、Ctrl+B,以及点击按钮。)
我想知道 OP 声称
But after researching a bit, I didn't find any relevant solution.
也许,使用术语“快捷键”或“加速键”会更容易。否则,OP 应该已经找到了一些东西,例如
我认为这可能是重复的。