问题
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
回答1:
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.
回答2:
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 :
来源:https://stackoverflow.com/questions/6743970/qt-menu-how-to-create-a-new-window