How to correctly configure QDockWidget for it to show/hide?

丶灬走出姿态 提交于 2019-12-11 06:47:40

问题


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;
}

回答1:


To bind key presses to actions, QShortCut is responsible.

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.

A small sample 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();
}

A minimal project file testQDockWidgetShortCut.pro:

SOURCES = testQDockWidgetShortCut.cc

QT += widgets

Compiled and tested on 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

Compiled an tested in VS2017 and Qt5.13.0:

Qt Version: 5.13.0

(In both cases, I tested all offered shortcut keys: A, B, Ctrl+B, as well as clicking the button.)


I wonder that OP claims

But after researching a bit, I didn't find any relevant solution.

May be, this would have been easier with the term “shortcut” or “accelerator key”. Otherwise, OP should have found something, e.g.

SO: How to set 3-key sequence shortcut with two key modifiers in Qt?

which I considered as possible duplicate.



来源:https://stackoverflow.com/questions/57157746/how-to-correctly-configure-qdockwidget-for-it-to-show-hide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!