I'm trying to add a menu to a CMFCToolbar. Following advice I found online, I'm doing it like this:
CMenu m_Menu;
m_Menu.LoadMenu(IDR_MYMENU);
m_Toolbar.ReplaceButton ( ID_DOTHISWHENCLICKED,
CMFCToolBarMenuButton( ID_DOTHISWHENCLICKED,
m_Menu,
10,
nullptr,
FALSE));
So the above gives me a button with a drop-down arrow. When I click the button, it does the action ID_DOTHISWHENCLICKED. When I click the drop-down arrow, I get a menu with one item in it. The item is the title of IDR_MYMENU and this has a sub-menu that is the menu I would like to be displayed. Something like this:
[BUTTON]
My Menu
Submenu Item 1
Submenu Item 2
Submenu Item 3
Obviously what I want to see is:
[BUTTON]
Submenu Item 1
Submenu Item 2
Submenu Item 3
So my question is.... why aren't all of the menu items in IDR_MYMENU in the menu, instead of being in a sub-menu off of it?
Thanks.
This problem is fixed simply by passing in .GetSubMenu(0)->GetSafeHmenu(), instead of the CMenu in question, when creating the menu button. Why this should be so is a complete mystery to me, and one of those MFC'isms that you know if you know.
Not sure whether to delete this question or tick it solved in case anyone else ever has this issue.
Related
How to create a combobox with searchbox at the top? (https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ComboBox?view=winrt-19041)
Combobox with searchbox at the top
As #Luca Lindholm said, you could set IsEditable as true that could make the input auto -complete. If you want to the dropdown will display the matched item dynamically, we suggest use AutoSuggestBox. And this is related tutorial.
I have a dialog list which use Use View dialog for Choices.
I have a row in a table which its appearing depends on the value selected from that dialog list. ( I am using Hide when formula... ) The form has Automatically refresh fields checked.
The problem is that after I select a certain value from the dialog list, and I even had selected Refresh fields on keyword change property, I MUST hit F5 or just TAB ( to go to a next field ) in order to make that row table to appear. I also tried to add uidoc.Refresh at the Exiting/OnChange event(s) of the dialog list.
I have noticed the following:
the refresh works fine for combo box, list box, radio button or check box
the refresh works fine for dialog list where you are using a list of choices / formula.
the refresh doesn't work for dialog list where you are using view dialog for choices ( my case ).
Is there any solution for this issue? I want when I selected a value from the dialog list, immediately the row / line should appear/disappear.
Thank for your time!
I want to create a QTreeWidget where the items are both editable and launchable. I want it to behave like Windows Explorer:
Single click -> selection
Single click on a previously selected item -> open LineEdit to edit the name
Double click -> perform the 'launch'
So I created slots for itemClicked() and itemDoubleClicked(). The first one is the following:
def EditName(self, item, column):
if self.lastclick == item:
self.editItem(item)
self.lastclick = item
The second one just 'launches' the file.
However, this kind of solution doesn't distinguish between a double click and two consecutive clicks, so the QLineEdit still appears after a double click. Is it possible to get rid of the editor forcibly? I tried a hack solution like hiding and showing the item but it didn't work.
You just need to set flags on your QTreeWidgetItem to include the ItemIsEditable option, and set the edit triggers on the QTreeWidget for SelectedClick
def populate( self, tree ):
tree.setEditTriggers(tree.SelectedClicked)
for i in range(10):
item = QTreeWidgetItem(['Testing %02i' % i])
item.setFlags(item.flags() | item.ItemIsEditable)
I have QTreeWidgetItem set in a QTreeWidget with 2 columns. Both cells got a CheckBox set by setCheckState(...).
When the user unchecks the CheckBox in my first column I uncheck the second CheckBox in column 2.
Now, I would like to prevent the user to check this second CheckBox again. Is it possible to remove this CheckBox in column 2 or to disable only this cell?
So far I have just seen that all the flags work on the complete item and a set CheckBox won't disapear.
Btw. The items are not editable and I don't want to use a QTableWidget/-Item.
Update:
The CheckBox will be automatically inserted by Qt when I call setCheckState for the item:
QTreeWidgetItem *item = new QTreeWidgetItem(ui.TreeWidget);
item->setCheckState(0, Qt::Checked);
After the new the item does not own a CheckBox (by Qt default). Calling setCheckState(...) I automatically insert a CheckBox (here in column 0) with the Qt::CheckState I want.
But after I have done it there's no way to remove the CheckBox - so it seems.
Maybe anyone got a solution how I can get rid of this CheckBox at a later time? Any help is much appreciated!
This will do it:
item->setData(0, Qt::CheckStateRole, QVariant()); //No checkbox at all (what you wanted)
any of these others will show the checkbox space
item->setData(0, Qt::CheckStateRole, Qt::Unchecked); //Unchecked checkbox
item->setData(0, Qt::CheckStateRole, Qt::Checked); //Checked checkbox
item->setData(0, Qt::CheckStateRole, Qt::PartiallyChecked); //Partially checked checkbox (gray)
The setCheckState method can not set 'no check state', only Checked, PartiallyChecked or Unckecked.
I have a menu that contains submenus.
eg:
Item1
Item2
Item3
item A
Item B
Item3 has items under it.
At any given time 1, 2, or the items under 3 should be checked. Since I don't have an ID for Item3 I have to use the MF_BYPOSITION indicator when I try to set a check on Item3 to indicate one of its children has a checkmark. Item3 should have a checkmark if A or B are checked. I am able to check items 1 and 2 and A and B - but can't figure out item3.
I have not been able to successfully use either ::CheckMenuItem() or ModifyMenu() to set the check mark.
Can someone point me to an example that does this successfully? The docs seem to indicate it can be done, but I have been unable to do it.
EDIT
This is for a menu that is set as the menu for a dlg box. The menu bar has three items - one of which drops down to what is shown above.
Note also, it is used as a popup for a right click, but I will take any suggestions to work in either case.
I've done this before for popup menus. You will need to access the submenu by position, instead of ID. Using your example above, Item 3 would be at position 2:
CMenu popupMenu;
popupMenu.LoadMenu(IDR_MYMENU);
popupMenu.GetSubMenu(0)->CheckMenuItem(2,MF_BYPOSITION|MF_CHECKED);
.
.
.
popupMenu.GetSubMenu(0)->TrackPopupMenu(...);
However, I haven't done this with items in the menu bar.
EDIT by Tim the OP:
For completeness
To get it to work with the menu item you have to get the hmenu
// MENU_POSITION is the zero based location of the menu you want to use. (file, edit, view, help... etc)
HMENU mainMenu = ::GetMenu(m_hWnd);
HMENU subMenu = GetSubMenu( mainMenu, MENU_POSITION);
SetMenuState(subMenu);
A few moments ago I had a similar problem - a standard MFC menu bar containing at least one submenu, and the need to be able to add a check mark to the submenu parent item, when any of the submenu child items were checked.
The easiest solution (for me) turned out to be as simple as performing the update in the standard OnUpdateMenuItem(CCmdUI* pCmdUI) call. In my case I used ON_UPDATE_COMMAND_UI_RANGE() to feed a bunch of menu IDs into the same update call, but the principal is the same for a single ON_UPDATE_COMMAND_UI() map.
The code I used (edited to be more easily inserted into other people's work) is:
void CMyApp::OnUpdateMenu(CCmdUI* pCmdUI)
{
// Note, a submenu parent (which has no editable ID in the resource editor) has the SAME ID as the first child item
if (pCmdUI->m_nID == ID_FIRST_CHILD_MENU && pCmdUI->m_pSubMenu != NULL) {
// Get the child menu so we can see if any child items are checked
CMenu* pSubMenu = pCmdUI->m_pSubMenu;
BOOL fChildChecked = FALSE;
for (UINT i = 0; !fChildChecked && i < pSubMenu->GetMenuItemCount(); ++i) {
// Do something to decide if this child item should be checked...
UINT nChildID = pSubMenu->GetMenuItemID(i);
fChildChecked = IsThisChildChecked(nChildID);
}
// The POSITION of the current menu item is stored in pCmdUI->m_nIndex
CMenu* pMenu = pCmdUI->m_pMenu;
UINT flags = MF_BYPOSITION;
if (fActiveChild) flags |= MF_CHECKED;
pMenu->CheckMenuItem(pCmdUI->m_nIndex, flags);
}
// Set the enabled state of the menu item as you see fit...
pCmdUI->Enable(TRUE);
}
Et voilĂ the submenu item automagically gains a check mark when any of its child menu items has a check mark.
Hope this helps others looking for similar solutions!
John