Please help with actual C++ code
-
@JonB OK, here is my take on this.
I understand that I am using QMenu method "addAction".
"addAction" takes parameters as any function/ method.
When I pass text the result is - new menu of text gets created. . That is probably explained in doc, but it is weird how text creates new menu.But that is OK and I am using that and it does the job.Now I want to use QAction method "addCheckable" as a parameter to "addAction". That has no relation to "text"....
So I need to create QAction object.
Am I correct so far ? -
Menus are containers. Actions are elements in that container.
menu->addAction("hello")
creates an action with text "hello" and adds it to a menu.
menu->addMenu("hello")
creates a new menu, then creates an action that opens this menu with text "hello" and adds that action to the original menu.
menu->menuAction()
returns an action that opens this menu.Maybe this example helps:
QMenu* menu = menuBar->addMenu("Action that opens menu"); QAction* actionThatOpensMenu = menu->menuAction(); QAction* actionInMenu = menu->addAction("Action in menu"); QAction* checkableActionInMenu = menu->addAction("Checkable action in menu"); checkableActionInMenu->setCheckable(true); QMenu* subMenu = menu->addMenu("Action that opens sub menu"); QAction* actionThatOpensSubMenu = subMenu->menuAction(); QAction* actionInSubMenu = subMenu->addAction("Action in sub menu"); QAction* checkableActionInSubMenu = subMenu->addAction("Checkable action in sub menu"); checkableActionInSubMenu->setCheckable(true);
-
@Chris-Kawa Thanks Chris - I am willing to redo my clumsy code...
Give me some time to digest your post.
Yes it has been a struggle to keep QMenu and QAction working with each other.I did implement last suggestion with mixed result - I get "check box" as a last entry in submenu without any text .
I am posting my current messy code FOR ENTERTAINMENT purpose ONLY.
Let me take a detailed look at the code you posted BEFORE any more suggestions , OK ?
QAction* tempAction = new QAction(); // text , this); tempAction->setCheckable(true); //subMenu[index]->addAction(tempAction); for (index_sub = 0; index_sub < subSize; ++index_sub) { // inner loop { // process block { // add subsub menu text = " index_sub " ; text += QString::number(index_sub); qDebug()<<text; subMenu[index]->addAction(list_array[index][ index_sub] + " #" + QString::number( index_sub)); //tempAction->setCheckable(true); subMenu[index]->addAction(tempAction);
.
-
@AnneRanch OK, I changed bunch of code and got Chris suggestion working as expected . It adds single entry to main menus , so I need to work on that.
Thanks Chris.
-
@AnneRanch
Here is my current , working , main loop code...
{ // // main menu process block #ifdef ARRAY text = " main menu process block "; qDebug() << text; #endif // start with action // attach to existing menu new_mainAction[index] = m_ui->menuWindow_cpntrol->menuAction(); // C new_checkableActionInMenu[index] = m_ui->menuWindow_cpntrol->addAction(list[index] + " # " + QString::number(index)); new_checkableActionInMenu[index]->setCheckable(true); // TOK //checkableActionInMenu[index]->setCheckable(false); // TOK } // main menu process block
I cannot help to say - there is a real difference in contributing to solution with few lines of REAL issue description and ....
Unfortunately...
Thanks a million Chris I owe you a beer.
Real Czech Pilsner or ?? -
@AnneRanch
I am stuck again
here is the working snippet// attach to existing menu subMenu_mainAction[index] = m_ui->menuWindow_cpntrol->menuAction(); // C subMenu_checkableActionInMenu[index] = m_ui->menuWindow_cpntrol->addAction(list[index] + " # " + QString::number(index)); subMenu_checkableActionInMenu[index]->setCheckable(true); // here need subMenu **menu** ??
If I understand the workings of adding subMenu ,
I start with the existing "widget " - QMenu .
subMenu_mainAction[index] = m_ui->menuWindow_cpntrol->menuAction();
To add subsubMenu - don't I need the added subMenu menu?
All i have are "actions" and no real subMenu.
-
QMenu* menu = menuBar->AddMenu("menu"); QMenu* subMenu = menu->AddMenu("sub menu"); QMenu* subSubMenu = subMenu->AddMenu("sub sub menu"); QMenu* subSubSubMenu = subSubMenu->AddMenu("sub sub sub menu"); QMenu* subSubSubSubMenu = subSubSubMenu->AddMenu("sub sub sub sub menu"); ...
Menus all the way down :)
-
This post is deleted!
-
This post is deleted!
-
I would try to help, but I've no idea what you're trying to achieve. Code you posted makes no sense unfortunately.
Maybe you could draw a layout you want and I can help achieve it in code?
-
This post is deleted!
-
@AnneRanch said in Please help with actual C++ code:
I have been told I cannot have "check box : in main menu
You can have a checkbox on an action in any menu, "main" or not. These are created via
QMenu::addAction()
.However, if you instead add an item which is a submenu (a > that you click on to display a "slide-out" menu) you cannot have a checkbox on that item. These are created via
QMenu::addMenu()
. And if you think about it, what would be the point of having a checkbox on a menu item which just displays a submenu anyway? -
This post is deleted!
-
This post is deleted!
-
reading this generally,
@AnneRanch said in Please help with actual C++ code:
The widow just do part of the interface and then I will oopen anither widow to continue... eventually all windows will comlete the required task. So when it is all done I can close it and it would be nice to reopen ALL of then at once when required...hence I thought it could be done in main window...
The whole idea to have uncluttered interaction as required -- one window At the time...I have the strong suspicion you're trying to implement a GUI idea using existing components which are not actually meant to be used like that at all.
Specifically, QMenu and its related classes are NOT supposed to be used like windows. You're not going to get a good user experience using this kind of windows.If you share your GUI idea you might get suggestions on how to approach the app.
I suspect a QML component will give you a LOT more flexibility that will allow you to get a good user experience.
-
@TomZ Thanks, however... I am just extending "windows
...tile...cascade..." menu using mdiArea (example). I have it essentially working, include "connect". The "problem is " - it can be started with QMenu -that makes submenu extension easy - but adding "checkable " to sub menu is currently failing. Or I can start with QAction then adding depended sub menu fails. Right now I feel starting with "cascading menus " is cleaner ..in case I want to add sususbsub....
The "user" -me - has several depended tasks to accomplish - setup serial connection, then setup dependent Bluetooth connection, configure remote Bluetooth device and then attach it to "serial connection" etc.,key remote device - amateur radio transmitter, listen to "meteor scatter" pings on Internet ...
The basic idea is to do all this in separate forms , I got that going, to keep monitor and me , focused on task... -
This post is deleted!
-
@AnneRanch said in Please help with actual C++ code:
subMenu[index]->addAction(list_array[index][ 2] + " #" + QString::number( index_sub));
...returns a
QAction *
. You need to store this pointer to connect to the action'striggered
signal. Then you can skip the whole acrobatics about gettingindex
inside a slot.Do you understand that?
-
@AnneRanch
but I am unable to add "check box" to any of them
QAction *action = submenu[index]->addAction("Some text"); action->setCheckable(true);
adds an action with text
Some text
and sets it to have a checkbox. -
This post is deleted!