In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use C++ 's Qt MdiArea multi-form components". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use C++ 's Qt MdiArea multi-forms components.
MDI forms control is similar to canvas, the control only has the function of displaying the form, can not generate the form, so we need to manually add a custom Dialog dialog box in the project, and customize the dialog box to some extent.
This Dialog dialog box we only add two functions, one Dialog::currentFileName () to get the form title, the other Dialog::SetData (QString data) setting data to the edit box, the code implementation is as follows.
# include "dialog.h" # include "ui_dialog.h" Dialog::Dialog (QWidget * parent): QDialog (parent), ui (new Ui::Dialog) {ui- > setupUi (this); this- > setWindowTitle ("New Doc"); / / window title this- > setAttribute (Qt::WA_DeleteOnClose); / / automatically delete this- > setFixedSize (200100) when closed / / set form size / / this- > setWindowIcon (QIcon (": / image/1.ico"));} Dialog::~Dialog () {delete ui;} / / get form title / / By: LySharkQString Dialog::currentFileName () {QString title = this- > windowTitle (); return title } / / set edit box content / / https://www.cnblogs.com/lysharkvoid Dialog::SetData (QString data) {ui- > lineEdit- > setText (data);}
Then we start to draw the main interface of the program, add the corresponding menu bar in toolBar, and put the mdiArea container component in the main form.
The top menu bar in the form, we need to manually define the names of the functions they have, etc.
When the program starts, the program calls MainWindow to initialize the form with the following initialization code:
# include "mainwindow.h" # include "ui_mainwindow.h" # include "dialog.h" # include # include / / clear all dialogs / / https://www.cnblogs.com/lysharkvoid MainWindow::closeEvent (QCloseEvent * event) {ui- > mdiArea- > closeAllSubWindows (); event- > accept ();} / / By: LySharkMainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this) This- > setCentralWidget (ui- > mdiArea); / / this- > setWindowState (Qt::WindowMaximized); / / window maximization ui- > mainToolBar- > setToolButtonStyle (Qt::ToolButtonTextUnderIcon); ui- > mdiArea- > setViewMode (QMdiArea::SubWindowView); / / child window mode} MainWindow::~MainWindow () {delete ui;}
The code works as follows:
The user executes the MainWindow::on_actionOpen_triggered () event when the form is created, and the MainWindow::on_actionClose_triggered () event when the form is closed.
/ / create a new form void MainWindow::on_actionOpen_triggered () {Dialog * formDoc = new Dialog (this); / / ui- > mdiArea- > addSubWindow (formDoc); / / add the document window to MDI formDoc- > show (); / / display} / / close all void MainWindow::on_actionClose_triggered () {ui- > mdiArea- > closeAllSubWindows () in a separate window; / / close all child windows}
The code works as follows:
When the user clicks MDI mode, we execute the following code to merge all existing forms into a form component similar to TabWidget.
/ / switch to MID mode void MainWindow::on_actionMID_triggered (bool checked) {/ / Tab multi-page display mode if (checked) {ui- > mdiArea- > setViewMode (QMdiArea::TabbedView); / / Tab multi-page display mode ui- > mdiArea- > setTabsClosable (true); / / pages can be closed ui- > actionLine- > setEnabled (false); ui- > actionTile- > setEnabled (false) } / / subwindow mode else {ui- > mdiArea- > setViewMode (QMdiArea::SubWindowView); / / subwindow mode ui- > actionLine- > setEnabled (true); ui- > actionTile- > setEnabled (true);}}
The code works as follows:
Window cascading mode is to arrange the forms side by side, we only need to call ui- > mdiArea- > cascadeSubWindows (); method.
/ / cascade mode void MainWindow::on_actionLine_triggered () {ui- > mdiArea- > cascadeSubWindows ();}
The code works as follows:
The tiling mode also uses ui- > mdiArea- > tileSubWindows (); the transformation can be achieved.
/ / Tile mode void MainWindow::on_actionTile_triggered () {ui- > mdiArea- > tileSubWindows ();}
The code works as follows:
The last function is that the main form sends data to the sub-form, which requires two functions to implement.
On_mdiArea_subWindowActivated implementation sets the name of the main form to itself
On_actionSendMsg_triggered implements the main form to send messages to the child window.
/ / get the form title when the sub-form is opened / / By: LySharkvoid MainWindow::on_mdiArea_subWindowActivated (QMdiSubWindow * arg1) {Q_UNUSED (arg1); / / if the number of child windows is zero, leave statusBar empty if (ui- > mdiArea- > subWindowList (). Count () = 0) {ui- > statusBar- > clearMessage () } else {/ / if not 0, display the file name of the main window Dialog * formDoc=static_cast (ui- > mdiArea- > activeSubWindow ()-> widget ()); ui- > statusBar- > showMessage (formDoc- > currentFileName ());}} / / A pair of selected forms send data / / https://www.cnblogs.com/lysharkvoid MainWindow::on_actionSendMsg_triggered () {/ / get the current MDI child window Dialog * formDoc first / / if opened, get the active form if (ui- > mdiArea- > subWindowList (). Count () > 0) {formDoc= (Dialog*) ui- > mdiArea- > activeSubWindow ()-> widget (); / / A pair of active form settings data formDoc- > SetData ("hello lyshark");}}
The code works as follows:
At this point, I believe you have a deeper understanding of "how to use C++ 's Qt MdiArea multi-forms components". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.