QT Menu how to create a new window? - c++

I am working in QT Creator.
I would like to mention that I am beginner in this software. I succeeded to create a menu that has: Edit and Open. I would like to create a new window when " Open " from menu bar is clicked. Till now I succeeded to print a message in QMessageBox when I press Open. What is the code for linking Open clicked with a new window? Is it possible to somehow drag and drop a new window in mainwindow.ui and than link it with Open on action Open_activated()?
THX . Appreciate
P.S I AM WORKING IN UBUNTU/C++ language

This is probably too late to answer but as I am also a beginner and struggled a lot to figure out this, I would like to share a solution that worked for me and according to me this is one of the simplest solution out there for a beginner.
Follow this link. It's possible that in the future that link might not exists, hence I am writing down a detailed version, as detailed as possible because I think for an absolute beginner (like myself) it can be very useful.
Default mainwindow.cpp
Go to "mainwindow.ui" (or whatever .ui one has)
Right-click outside the window and activate "Action Editor" if it wasn't already activated:
Right-click on the window and create "Menu-bar" and then double-clicking on "type here" you can rename it as "Open" or something which will then drop-down another box where you can again type the name you desire, let's say "message".
When created menu item "message" is created corresponding item will apear in the "Action Editor"
Right-click on the "message" in the Action Editor and click "go to slot" which will lead you to the specific slot in the mainwindow.cpp:
Here you can create a small window with whatever message you want to display.
[![Window creating code]11 etc goes here.]12
****don't forget to include "QTextEditor", go to mainwindow.h and #include "
On compiling & executing we will see :

Most of the question is already answered by this previous question. It leaves one part open, namely how that slot is called.
Well, you answered that: the menu entry "Open" is a QAction. You can connect that action to the slot which you just created.

Related

How do I use the "new" Common Item Dialog with wxwidgets?

I want to create a file open dialog with wxwidgets that uses the "new" style of the Common Item Dialog under Win-Vista and newer. Is there any way to achive this? With the wxFileDlg() I get a dialog as shown on the right side but I'd like to get th left dialog...
both dialogs
The dialogs sample included in wxWidgets shows the example of "new style" file dialog if you use e.g. "Dialogs|File operations|Save dialog" menu item (or just press Ctrl+S) and there is nothing special to do. If this doesn't work for you, check that you're
Not using some ancient version of wxWidgets.
Have correct manifest in your application.
Not using any custom controls in your dialog, as those are only supported in old style version.
I struggled with this today so thought I would post here for the next person who has the same issue. If I were to guess, your code was calling either dialog.Center() or dialog.CenterOnParent(). I've posted a lengthy explanation of why this happens here.
From all my time spent on this today, you have to choose whether you want have the old common control dialog and be able to center it, or use the new common item dialog and have it appear in your windows top-left corner.
The good news is that Visual Studio, Word, Excel, Firefox, Chrome, and many others all use the new dialog and they all open at the top-left of the application window.

How to programmatically close wxMenu used as popup menu via `wxWindow::PopupMenu` in wxWidgets?

I have a set of instances of plotting view that's added/removed dynamically according to incoming signals from tcp port from another process.
In each instance, the user can right click to open a popup menu, invoked via wxWindow::PopupMenu.
If the plotting view instance is to got shutted-down dynamically while the popup menu is visible, the view instance window is closed while the popup menu is still floating. Then any GUI action crashes the application.
I've been going through the APIs for wxWidgets to find a way to programmatically close the popup menu in the plotting view destructor, but with no-luck.
I found this forum post suggesting it's an impossible thing to close the popup menu programmatically. But it's too old, so not sure if it's still valid assumption.
Here are the trials that failed till now :
Trying to call SetFocus and SetFocusFromKbd on the plotting view as a way to move focus.
Generating mouse left click event and send it to the popup menu.
Generating a keyboard event and send it to the popup menu.
PlottingView::~PlottingView()
{
cout << "Sending wxMouseEvent to the popup menu" << endl;
wxMouseEvent e(wxEVT_LEFT_UP);
this->GetPopupMenu()->ProcessEvent(e);
wxKeyEvent ke(wxEVT_CHAR);
ke.m_keyCode = WXK_DOWN;
this->GetPopupMenu()->ProcessEvent(ke);
ke.m_keyCode = WXK_RETURN;
this->GetPopupMenu()->ProcessEvent(ke);
// the rest of the destruction
}
So I will appreciate any idea to programmatically close this popup menu.
Platform:
CentOS: 6.7
wxWidgets 2.8.12
G++: 4.3.3
Edit #1
Note: For commenters and answers suggesting upgrading the wxWidgets version, it's a debate in my team for everyday. But the answer is still no.
Most of the trials has failed. But I found a workaround to stop crashing but the popup menu don't close.
The solution was to nullify the following members using their setters, so the menu callback won't access them.
this->GetPopupMenu()->SetInvokingWindow(NULL);
this->GetPopupMenu()->SetEventHandler(NULL);
The best is probably to delay destroying the underlying window until PopupMenu() returns. As it is, your program logic is very convoluted because you're dispatching the event which results in closing of the window from inside PopupMenu() function and this just can't end well, even if you could use wxUIActionSimulator to close the menu (but you definitely should consider upgrading your 15 year old wxWidgets version in any case).

Accessing a right click context menu from an external application

After performing some Google-fu and searching Stack Overflow I've been unable to find a way to access the right click menu of an item and read data or select an item from it. I've looked up methods for SendMessage and PostMessage
What I've done so far:
This is an example of post here that would have been exactly what I want if it didn't use Qt and would work on an external application. Everything I've been able to find is about creating a right click menu when I just want to view an external application's right click menu.
I've tried getting the handle of the context menu using Spy++ but the menu just disappears as soon as I select the "Find Window" option in Spy++ (which is to be expected).
The only way I could think of doing this is using mouse_event to display the box but then I don't know where to go from there. I feel like this would also be very inefficient.
I'm working on some legacy code so I don't have a lot of choice in what I can use, if it's possible I'd like to not use libraries that aren't included in Visual Studio 2008.
What I'm trying to do:
I'm looking for a way to access the right click context menu in an external application and read the data in it and then select an item. I feel like this should be really simple but my research skills are subpar. If anyone can point me in the right direction I would really appreciate it.
Thank you for reading!
The only way you can get hold of a context menu is to make the application display it. It doesn't exist otherwise. (The other question you give as an example is regarding the Windows Shell menu which is designed to be useable)
A window gets sent WM_CONTEXTMENU when the user right clicks, so you could use SendMessage() to invoke the menu, or failing that just send a right click.
After a bit of googling, I think context menus have a classname of #32768, so you could use FindWindowEx to find a child window handle with the matching classname.
Once you have the window handle, you can use the MN_GETHMENU message to get hold of the menu handle.

Add a new menu item in the Taskbar menu of all open applications in Windows 7

I want to know whether it's possible to add a menu item to the taskbar right-click menu of all open applications in Windows 7. I specifically want to know whether it's possible to add the new item directly above the "Close Window" (or "Close All Windows") menu item.
Any help is much appreciated.
Thanks.
No. Imagine if that were possible: Everybody would abuse it by adding their app to the menu of every other app.

Add a menu resource to a dialog box

I was wondering if it is possible to add a menu resource to a dialog box. It would be easier to "design" my applications layout in multiple dialog boxes rather than the standard window, however, I will need to be able to add a menu. I would preferably be able to do this through a resource defined menu, however, I have no objections to doing it programmatically.
Right now my application is just starting like this:
DialogBox(hInst,MAKEINTRESOURCE(IDD_DIALOG_UPDATE),NULL,(DLGPROC)updateTitle);
in the main function.
Thanks for your help!
Dennis M.
Call SetMenu(hDlg, hMenu) during WM_INITDIALOG.
As Krishty commented on the accepted answer, it is easier and safer to just specify the menu ID in the designer. In the dialog resource properties, scroll down and there is a Menu option. Choose your already created menu from there. You won't see it in the designer, but when you run your app it will be there.