Solved Open dialog with menu item
-
Edit: Several working lines of code are shown in, what I think is, post 11. I have working code so declared this thread solved. Thank you.
Novice alert. Please be tolerant of typos as I cannot copy paste to here.
I have created a widget, and configured it thusly:QWidget *central_widget = new QWidget( this ); setCentralWidget( central_widget ) QGridLayout *sc_grid_layout = new QGridlayout( central_widget ); // code to fill in the grid not shown. QMenuBar *menu_bar = new QMenuBar( central_widget ); setMenuBar( menu_bar ); QMenu *controls_option = new QMenu( “Controls”, menu_bar ); menu_bar->addMenu( controls_option )
That much does work.
When the user clicks the menu item “Controls” the intent is to display a dialog that has about 10 controls, line edits, radio buttons, etc. I don’t want a drop down menu. Do not stop at Go to collect $200. Just go directly to displaying the dialog. How can that be done? -
@BKBK
You need to add an action to your menubar see here for details -
@BKBK You can add a QToolBar instead of QMenuBar and then add QToolButtons to that toolbar.
-
I had not come across QToolBar so this is more involved that at first appears, as usual.
In the QT4 Assistant for QMenuBar it states early on: … a list of pull-down menu items. QToolBar states: Toolbar buttons are added by adding actions, …
So,…, I suspect both will work but need to try each and see what happens.
Thank you for taking the time to post. -
@kenchan said in Open dialog with menu item:
@BKBK
You need to add an action to your menubar see here for detailsThanks for that link. I'm having a bit of difficulty in understanding how class Action fits in but am working that link to understand it. Thanks for the reply.
-
In the QT4 Assistant for QMenuBar it states
Purely a heads-up: if you are a "novice", do you have legacy code to support which means you need to use Qt 4? Otherwise we are on Qt 5, and that's much shinier/newer/better....
-
JonB, Yes, we do have legacy code to support. Quite a lot and all but one QT Linux user has moved on so I am pretty much on my own. Most is QT3 but we do have QT 3 and Qt 4 installed and I am trying to force the issue to get to 4 then 5. Is this stuff obviously QT3 or 4? Yes, I can see many changes from 3 to 4, and presume many more for 5.
I am not able to understand the use of QAction.
The class: C_Strip_Chart has some labels and line edit widgets to show some of the parameters of a strip chart. It has been created with a menu bar that displays the text: "Controls" When the user clicks "Controls" another widget is to be displayed that provides the ability to change the parameters of the strip chart.
The constructor for C_Strip_Chart instantiates
C_Sc_User_Control * sc_user_control.
It inherits QWidget and when the right code code is implemented can be displayed. Back to the constructor for C_Strip_Chart: it contains these lines:
QMenu *menu_bar = new QMenuBar( central_widget ); SetMenuBar( menu_bar ) QMenu *strip_chart_menu = new QMenu( “Controls”, menu_bar ) menu_bar->addMenu( strip_chart_menu ); // That gets a menu bar displayed containing “Controls” Now the part I don’t understand. QAction * open_user_control = new QAction( strip_chart_menu ); Connect( open_user_control, SIGNAL( triggered() ), sc_user_control, SLOT( show() ) );
I am tempted to write more about the intent, but suspect I am too far off the mark so simply ask:
What must be different such that when the user clicks “Controls” the widget sc_user_controls is displayed?Please try to be tolerant of typos. I cannot copy paste from my work computer to my internet computer.
-
@BKBK
The above seems (to me) to do what would be expected. Clicking on Controls will call theshow()
slot ofsc_user_control
. One would imagine that should show whatever it is. If you know how to use a debugger/appropriateqDebug()
statements you should verify that is getting called, and take it from there. -
@BKBK said in Open dialog with menu item:
C_Sc_User_Control * sc_user_control.
Just to be 100% sure, it does ?
C_Sc_User_Control * sc_user_control=new C_Sc_User_Control(); -
@BKBK
I thought you are supposed to add the action to the menu at some point unless it is an action group, which I don't think it is, is it?strip_chart_menu->addAction(open_user_control);
-
First, my solution may not be the best so feel free to comment.
Here are some extracted code fragments that do the job.
This is the constructor for the widget that contains the strip chart and some explanatory values such at time and the limits of what is displayed.C_Strip_chart::C_Strip_chart( QWidget *parent ) :QmainWindow( parent ) { // Create the widget the provides control of the strip chart. It provides the ability to select // the parameter plotted, set the time span, etc. The strip chart is a diagnostic tool. There is // the need to make it small while other things are observed. Therefore the controls for the // strip chart are displayed independently of the plotted point. Sc = Strip Chart. sc_user_control = new C_SC_User_Control( ); // note: no parent. sc_user_control->hide(); …. QMenuBar * menu_bar = new QMenuBar( central_widget ); SetMenuBar( menu_bar ) // Line numbers are for easy reference below 1 QMenu *show_controls = menu_bar->( “Controls” ); 2 Show_controls->addAction( “Show”, this, SLOT( show_the_controls() )); 3 Show_controls->addAction( “Hide”, this, SLOT( hide_the_controls() )); 4 Show_controls->addAction( “Show 4”, sc_user_control, SLOT( show() )); 5 Show_controls->addAction( “Hide 4”, sc_user_control, SLOT( hide() )); // these were tested and found to work. Discussed below 6 menu_bar->addAction( “Direct Show”, this, SLOT( show_the_controls() )); 7 menu_bar->addAction( “Direct Hide”, this, SLOT( hide_the_controls() )); 8 menu_bar->addAction( “Direct Show 2”, sc_user_control, SLOT( show() )); 9 menu_bar->addAction( “Direct Hide 2”, sc_user_control, SLOT( hide() ));
The original versions and variations compiled but when ran there was a startup message that the slot could not be found. The buttons did not show and hide the user control widget. I cannot get back to them to show my errors, but concluded that a slot in a different widget could not be called directly with an addAction() operation. We thought we had to create a local function to make that call in the other class / widget.
Hence, lines 2 and three. The function show_the_controls() is part of class C_Strip_chart and within them they call the show() of the user controls class.
I wanted a menu item that directly showed and hid the controls widget rather than opening a menu list to do that. So I wrote lines 6 and 7.
At that point I wrote line 8 and 9 to repeat my error and demonstrate that the local function had to be called. However, I was a bit surprised when they worked. That means that I had errors in my original calls that I cannot duplicate right now, and that my earlier conclusion was wrong. (That a direct call to another widget’s function could not be done.) Then I added lines 4 and 5 which work.
So, now this shows several ways of implementing the menu control so desired. Lines 4 and 5 follow what I suspect is the commonly used format, build a drop down menu and those items perform the operations desired. Lines 8 and 9 demonstrate that a menu item can directly initiate an action rather than just showing a menu list.
This has consumed a couple of days of my time.
I send my thanks to each that took the time to read and post.