Qt Creator - Add keyboard shortcuts to Menu entries - c++

I created the shortcut events, such as:
new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this, SLOT(close()));
But now I would like to show "Ctrl+Q" in the menu entry here:
How do I do that? I don't seem to find a way to do that in Qt Creator.

You can set the shortcut keys in the QtDesigner in the 'Action Editor' (Tab at the bottom, the 'Signal/Slot Editor' tab is selected by default).
Here all defined QActions are listed. A double click on a field in the 'shortcut' column will open the wanted dialog.
This will add the shortcut to the QAction (create triggered events) and set it as visibile text, e.g. in the menu entry (only visible in the application, not in QtDesigner).
In the 'Property Editor' are more options for the 'shortcuts' (e.g. make them translatable).

Generally you would use QAction class for this, so you would have a QMenu to which you will add actions, in your case CLOSE. Then you can use SetShortcut to add "CTRL + Q" in menu.
pNewAction->setShortcut(QString(strAccel.c_str()));
where pNewAction is of type QAction.

Related

How to set signal to a button of the GtkColorChooserDialog composite widget?

I am using Glade to create GUI. I have the default GtkColorChooserDialog.
How to set signals to the Select and Cancel button clicked event.
These two buttons are grouped in one GtkButtonBox and I can only choose the box, but not the buttons themselves.
If I can't select the button then how to assign action to the clicked signal?

Change between Qt4 and Qt5 with conflicting access keys and Shortcut keys

There is a change between QT4 and QT5 when handling Menubar menus access keys and QActions shortcuts that have conflicting key combinations.
In Qt4 when a MenuBar contains a menu with an ampersand like &File it can be opened by tapping on Alt and then tapping F. If a QAction is created using the same hotkey combination like Alt+F then the QAction ‘s signal will be fired if Alt and F are pushed simultaneously or alt is held down and F is pushed.
This is similar functionality to Visual Studios and other MS programs.
In Qt5.4 and Qt5.5 when a MenuBar contains a menu with an ampersand like &File it can be opened by tapping on Alt and then tapping F. If a QAction is created using the same hotkey combination like Alt+F then the QAction ‘s signal will be NOT fired. Instead nothing happens and a warning message is sent to the application output.
QAction::eventFilter: Ambiguous shortcut overload: Alt+F
This issue can be duplicated using a QTcreator example like menus and changing the setshortcut for newAct to Alt+F or another shortkey that is the same as a menubar.
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAct);
void MainWindow::createActions()
{
newAct = new QAction(tr("&New"), this);
newAct->setShortcut(tr("Alt+F")); // This will work in QT4 but not in QT5 and will send a message QAction::eventFilter: Ambiguous shortcut overload: Alt+F
newAct->setStatusTip(tr("Create a new file"));
connect(newAct, SIGNAL(triggered()), this, SLOT(newFile()));
My question is what is the correct functionality for windows QT4 or Qt5. If QT5 is correct is there a way to setup Qt so it acts like QT4 instead.
TL;DR: Don't hate your users. Don't do that.
Application-global key shortcuts must be unambiguous. Qt5's behavior is correct. Qt4 failed to detect this, and would let you horribly mislead the user. When a user sees an underline keyboard shortcut for the menu or a menu item, they have every right to believe that the shortcut is application global and won't depend on the context. What you observed in Qt 4 was "allowed", but very bad from usability perspective and had no place in polished, professional applications.
To get the behavior you're after, you must make the action unambiguous: you must remove the shortcut from the menu action. That will work correctly: the menu loses the shortcut, and your application-global shortcut takes over that key sequence.
Alas, I'm very much doubtful that it is a good idea to take over a very common menu shortcut - one that is present in most Windows applications that have menus. If I was your user, I would not like you for taking over Alt-F.

Custom menu actions in Qt Designer

I'm currently trying to use Qt Designer to build a GUI and I would like to customize slots of my menu actions.
E-g: I'd like the user to press a menu action and it'd show a widget if it is hidden or hide it if it is already visible.
Basically, what I want to do is execute some code of mine and not the default actions such as show() or hide().
So I'm wondering if I should create a subclass of QMenuBar, add custom slots to it, then create a plugin to use it inside Qt Designer or if I should create a subclass for QMenu or QAction ? Or maybe it isn't the right way to do that ?
I'm working under Visual Studio and I'm only using Qt Designer, not Qt creator.
I'm new to GUI and Qt programming and I'm a bit lost here.
Thanks in advance :)
You have basically 2 options:
Implement the custom logic in the Mainwindow sublcass.
For this, you simply add the slots required for your handling in the class, and make them available in Qt Designer. You can do this:
either in the Signal/Slot editor and click "Modify" and then click on the + Symbol. By this you make new slots available in QtDesigner;
or when your slot is called on_(senderName)_(signalName), Qt autowiring will automatically connect the signals, and you don't have to do this in code or desinger.
Create a QMenuBar subclass and implement the custom logic there.
Your case tell Qt Designer to select your specific subclass as replacement for the default QMenuBar by right-clicking on it, and select "Promote to...". In the new dialog, you can specify your custom class that will be used as replacement in actual code, but in design time a QMenuBar is used. With this mehtod, you don't have to write a separate plugin to make your class available in Qt Designer.
Note that with the second option, your custom logic will only be called when the actions are triggered through the menu bar, not by shortcuts or tool buttons
Create a slot in your class:
onMenuActionTriggered()
Use the connect() to react on action's signal:
connect(ui.myAction, SIGNAL(triggered()), this, SLOT(onMenuActionTriggered()));
In your slot you can do whatever you want.
Another solution (not my favourite one, but possible) is to use the auto-connect functionality, which means, by declaring a slot 'on_myAction_triggered()' (where myAction is the name of your QAction) you don't need to use the connect() since it is automatically connected by Qt
The menu bar is automatically added to any new form derived from QMainWindow (the default when creating a gui application, but you can create new main windows using file->new file or project... and selecting Qt->Qt Designer Form Class ).
To add options to it you simply click in the area labeled "Type here" and write your option text. When you do so an action will appear in a list in the lower part of Qt Designer. Right click on that action and select "go to slot". It will pop up a dialog with "triggered()" already selected for you. Simply click "Ok" and Qt Creator will take care of all the details and transport you to the body of the slot function.

Qt Designer: How to remove slot from designer?

Adding slot for a signal of a widget is easy: right click on it and select "go to slot". But how can I remove the slot created for a button (like on_pushButton_clicked) from the Qt designer.
You have to remove the declaration of <class_name>::on_pushButton_clicked() from header file.
You can press View->Signal/Slot editor, then, a new window will be shown, simply remove the slots by pressings the minus button.

Creating popup menu in Qt for QTableView

I have a QTableView in the main UI of my program. I'd like to show popup menu when user right clicks on the cells of the table and take appropriate action when an option is selected from the menu.
I am using Qt Creator 1 (Qt version 4.5). How can I do that?
Check out the customContextMenuRequested signal to get the event, and use a QMenu for the menu itself. Use QTableView::indexAt to find out what, if any, cell was clicked based on the coordinates given to the signal and take the appropriate action when a menu item is clicked.