Qt menu bar events - c++

I've added menus and menu items in Qt creator using the "Design" option. I then rt-clicked the associated action below and clicked on "Go to slot". This takes me to the code that will be executed when I click on that menu item. Perfect - almost. I'd like to distinguish between clicking and shift-clicking on the menu item. It looks like I can determine whether the event was modified with the shift button by doing something like this:
if (event->modifiers() & Qt::ShiftModifier) {
...
}
The problem is that no event is passed in for a menu click action.
Can I do what I'm trying to do somehow? Do I need to figure out how to pass an event in - or is there some other way to test that the shift key is pressed?
Thanks!

In the slot which is being called you can use QGuiApplication::keyboardModifiers() to get the current keyboard modifiers:
void MainWindow::on_action_triggered() {
if (QApplication::keyboardModifiers() & Qt::ShiftModifier) {
// Shift key is down.
}
}

Related

Call button click function from grandchild

I'm creating my first C++ wxWidgets application. I'm trying to create some kind of split button where the options are displayed in a grid. I have a custom button class which, when right-clicked on, opens a custom wxPopupTransientWindow that contains other buttons.
When I click on the buttons in the popup, I want to simulate a left click on the main button. I'm trying to achieve this through events, but I'm kinda confused.
void expandButton::mouseReleased(wxMouseEvent& evt)
{
if (pressed) {
pressed = false;
paintNow();
wxWindow* mBtn = this->GetGrandParent();
mBtn->SetLabel(this->GetLabel());
mBtn->Refresh();
wxCommandEvent event(wxEVT_BUTTON);
event.SetId(GetId());
event.SetEventObject(mBtn);
mBtn-> //make it process the event somehow?
wxPopupTransientWindow* popup = wxDynamicCast(this->GetParent(), wxPopupTransientWindow);
popup->Dismiss();
}
}
What is the best way to do this?
You should do mBtn->ProcessWindowEvent() which is a shorter synonym for mBtn->GetEventHandler()->ProcessEvent() already mentioned in the comments.
Note that, generally speaking, you're not supposed to create wxEVT_BUTTON events from your own code. In this particular case and with current (and all past) version(s) of wxWidgets it will work, but a cleaner, and guaranteed to also work with the future versions, solution would be define your own custom event and generate it instead.

Disable some context menu items if no items are checked

I have a tree view (CTreeView) that will show me a pop-up menu after I right-click my mouse on it.
In my context menu there are only 3 items (i.e A, B, C) for selection and my tree view displays a long list of ordered foods designed with check-boxes. I would like to disable menu items A and B if no ordered foods are checked and enable them when any is.
I create CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) //CFoodView inherits CTreeView
and CFoodView::OnUpdateItemB(CCmdUI* pCmdUI) to handle their states like so
CFoodView::OnUpdateItemB(CCmdUI* pCmdUI)
{
if TreeView has no items
{
pCmdUI->Enable(FALSE);
}
else
{
*Search* the tree to get selected items
if None is checked
{
pCmdUI->Enable(FALSE);
}
else there are checked items
pCmdUI->Enable(TRUE);
}
}
Method CFoodView::OnUpdateItemA(CCmdUI* pCmdUI) is the same.
I think this isn't a correct way to handle this GUI feature.
Well, you did not submit all important information. How did you create menu item handlers?
Assuming you insert handlers the proper way, still did not provide any information how you are invoking popup menu.
If all you did was properly done it is the proper way of handling update menu.
The most common mistake is designating view itself as the window that handles popup updates and commands. In order to use MFC menu update mechanism, you have to pass a pointer to the main window not to the tree view:
CWnd *pMainWnd = AfxGetMainWnd();
ASSERT(pMainWnd != nullptr);
pSubMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, pMainWnd);
If this will not work reexamine the way you create handler and/or the place you invoke the TrackPopupMenu function.

QSystemTrayIcon activated signal: DoubleClick without Trigger

I want to show context menu on left click and run app on double click.
For this i have next code:
...
connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason)));
...
void MyTray::slotActivated(ActivationReason reason)
{
if(reason==QSystemTrayIcon::DoubleClick)
startApp();
else
if(reason==QSystemTrayIcon::Trigger
|| reason==QSystemTrayIcon::MiddleClick)
contextMenu()->popup(QCursor::pos());
}
It works, but for double click case i got two slot calls - one for Trigger and only then for DoubleClick. As result context menu shown and hidding in a moment.
Is there is a way to avoid this?
Rather than using slotActivated, you need to handle the mouse events.
Whilst these aren't directly available in QSystemTrayIcon, it does allow you to install an event filter and handle the mouse events from there.

MFC: CMFCToolBar SetButtonStyle not wirking with style TBBS_PRESSED?

byIs there are bug in control ? or I am doing something wrong ?
In .h
CMFCToolBar m_wndToolBar;
in message map
ON_COMMAND(ID_MYID, &CMainFrame::OnToolBar)
void CMainFrame::OnToolBar()
{
int nIndex = m_wndToolBar.CommandToIndex(ID_MYID);
UINT nState = m_wndToolBar.GetButtonStyle(nIndex);
if(nState & TBBS_PRESSED)
nState &= ~TBBS_PRESSED;
else
nState |= TBBS_PRESSED;
m_wndToolBar.SetButtonStyle(nIndex,nState);
m_wndToolBar.InvalidateButton(nIndex);
}
By clicking on button I need to set button pressed, and when user clicked again, button become unpressed.
Nothing happens by clicking on button :(
Just create an ON_UPDATE_COMMAND handler for the specific item.
Use pCmdUI->SetCheck to Signal the down or Up state.
The MFC updates tool bars and menus never directly. They ask the Framework to update the state of the Buttons and menu items.
Your description indicates that you want the button to have the behavior of a "check box". If that is correct, make sure you specify TBBS_CHECKBOX for the button style. You should not need to manually handle rendering the check box state each time the button is pressed.

Javafx choicebox/dropdown looses focus on keyboard down arrow and moves to next item

In a JavaFX application, I have two textboxes and three choicebox on a screen.
They are all placed in vertical manner.
On navigating by keyboard TAB when focus reaches fist choicebox,if i click on keyboard Down arrow, then instead of opening the items for that choicebox, focus moves to next choicebox and displays items from it.
I tried to manually override this by creating a keypress event method on first choicebox, still the focus, moves to next choicebox.
Any solution?
Put event filter in the parent component that contains those controls.
/**
* prevent move focus on pressing UP/DOWN
*/
pnlRadioButton.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.UP || event.getCode() == KeyCode.DOWN) {
event.consume();
}
}
});