In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the method of realizing the custom dialog box by CAccord Craft + QT". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought slowly and deeply, and come to study and learn "what is the method of realizing the custom dialog box by CAccord Craft + QT".
There are many kinds of dialogs, such as general dialogs, custom dialogs, modal dialogs, modeless dialogs, etc., among which the general dialogs include QFileDialog file dialogs, QColorDialog color dialogs, QFontDialog font dialogs, QInputDialog input dialogs and so on. The custom dialogs are mainly simple pages that realize their own layout, which is simpler than the form dialogs, except dialogs. Multi-form design is also the most commonly used, such as multi-form embedding, MID forms, etc. The following is a summary of the code for each form.
Create a custom form
1. First of all, use two controls, TableView is mainly table processing, TreeView is mainly a tree box, the two can be bound through the QItemSelectionModel model to achieve menu linkage.
# mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H#include # include QT_BEGIN_NAMESPACEnamespace Ui {class MainWindow;} QT_END_NAMESPACEclass MainWindow: public QMainWindow {Q_OBJECTpublic: MainWindow (QWidget * parent = nullptr); ~ MainWindow (); private: Ui::MainWindow * ui; QStandardItemModel * model; / / define data model QItemSelectionModel * selection; / / define Item selection model} # endif / / MAINWINDOW_H# mainwindow.cpp#include "mainwindow.h" # include "ui_mainwindow.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); model = new QStandardItemModel; selection = new QItemSelectionModel (model); / / Associated to tableView ui- > tableView- > setModel (model); ui- > tableView- > setSelectionModel (selection); / Associated to treeView ui- > treeView- > setModel (model) Ui- > treeView- > setSelectionModel (selection); / / add header QStringList HeaderList; HeaderList setRowCount (rows);} / / finally delete the release dialog handle delete ptr;}
After configuration, we also need to bind two slot functions, first come to the dialog form, manually add two slot functions, when the button is clicked, send a signal, the main form can receive post-processing.
The effect is as follows: this is a modal dialog box, which means that you cannot operate the main form again when you open it, and you can continue to operate the main form only when the modal dialog box is closed.
Then write a modeless dialog box, this time create a Dialog named DialogHead and draw the interface.
Dialoghead.h
# ifndef DIALOGHEAD_H#define DIALOGHEAD_H#include # include namespace Ui {class DialogHead;} class DialogHead: public QDialog {Q_OBJECTpublic: explicit DialogHead (QWidget * parent = nullptr); ~ DialogHead (); void setHeaderList (QStringList& headers); / / set header data QStringList headerList (); / / return header data private: QStringListModel * model; / / define data model Ui::DialogHead * ui;} # endif / / DIALOGHEAD_H
Dialoghead.cpp
# include "dialoghead.h" # include "ui_dialoghead.h" DialogHead::DialogHead (QWidget * parent): QDialog (parent), ui (new Ui::DialogHead) {ui- > setupUi (this); model = new QStringListModel; ui- > listView- > setModel (model);} DialogHead::~DialogHead () {delete ui;} / / set the data void DialogHead::setHeaderList (QStringList & headers) {model- > setStringList (headers) in the current listView } / / returns the current header QStringList DialogHead::headerList () {return model- > stringList ();}
Mainwindow.cpp
/ / dialog box sets header data void MainWindow::on_pushButton_2_clicked () {DialogHead * ptr = new DialogHead (this); Qt::WindowFlags flags = ptr- > windowFlags (); ptr- > setWindowFlags (flags | Qt::MSWindowsFixedSizeDialogHint); / / if the number of header columns changes, reinitialize if (ptr- > headerList (). Count ()! = model- > columnCount ()) {QStringList strList / / get the existing header title for (int item0 int ref icolumnCount (); iSum +) {strList.append (model- > headerData). ToString ();} / used to initialize dialog box display ptr- > setHeaderList (strList);} int ref = ptr- > exec () / / call the pop-up window if (ref==QDialog::Accepted) {QStringList strList=ptr- > headerList (); / / get the modified StringList model- > setHorizontalHeaderLabels (strList) on the dialog box; / / set the header title of the model} delete ptr;}
Finally, bind the signal to Cao on Dialog.
The previous interaction is realized by using the traditional cross-reference, which is more complex to realize. we can also use the signal and Cao to realize, associate the signal with Cao, send the signal during the operation, and the slot function accepts and corresponds.
Realize multi-form design
Multiple forms are divided into body cards and MID forms, which are also the most commonly used.
1. The first is the form card, here in the creation should choose the Widget form, this form is the cleanest without any controls, so we choose it.
Formdoc.h
# ifndef FORMDOC_H#define FORMDOC_H#include namespace Ui {class FormDoc;} class FormDoc: public QWidget {Q_OBJECTpublic: explicit FormDoc (QWidget * parent = nullptr); ~ FormDoc (); private: Ui::FormDoc * ui;}; # endif / / FORMDOC_H
Formdoc.cpp
# include "formdoc.h" # include "ui_formdoc.h" # include "mainwindow.h" # include # include FormDoc::FormDoc (QWidget * parent): QWidget (parent), ui (new Ui::FormDoc) {ui- > setupUi (this); QVBoxLayout * Layout = new QVBoxLayout (); Layout- > setContentsMargins; Layout- > setSpacing (2); this- > setLayout (Layout); MainWindow* parWind = (MainWindow*) parentWidget () / / get parent window pointer QString ref = parWind- > GetTableNumber (); / / get selected tab index std::cout tabWidget- > setVisible (false); ui- > tabWidget- > clear (); / / clear all pages ui- > tabWidget- > tabsClosable (); / / Page has a close button that can be closed} MainWindow::~MainWindow () {delete ui } / / define a function to get the current Table name QString MainWindow::GetTableNumber () {QString ref = QString (ui- > tabWidget- > currentIndex ()); return ref;} void MainWindow::on_pushButton_clicked () {FormDoc * ptr = new FormDoc (this); / / New tab ptr- > setAttribute (Qt::WA_DeleteOnClose) / / automatically destroy int cur = ui- > tabWidget- > addTab when closed (ptr,QString::asprintf ("192.168.1% d", ui- > tabWidget- > count ()); ui- > tabWidget- > setTabIcon (cur,QIcon (": / image/1.ico")); ui- > tabWidget- > setCurrentIndex (cur); ui- > tabWidget- > setVisible (true) } / / execute void MainWindow::on_tabWidget_tabCloseRequested (int index) {if (indextabWidget- > widget (index); aForm- > close ();} / / disable void MainWindow::on_tabWidget_currentChanged (int index) {Q_UNUSED (index) by default on pages without Tab; bool en=ui- > tabWidget- > count () > 0; ui- > tabWidget- > setVisible (en);}
To achieve a beautiful dialog box, the first two dialogs can only pop up once, and the last dialog box can pop up many times. The main code is as follows.
# include "mainwindow.h" # include "ui_mainwindow.h" # include # include "formmain.h" # include "formoption.h" # include "formcharts.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); ui- > tabWidget- > setVisible (false); ui- > tabWidget- > clear (); / / clear all pages ui- > tabWidget- > tabsClosable () / / Page has a close button that can be closed ui- > tabWidget- > setTabPosition (QTabWidget::North); / / set tab orientation ui- > tabWidget- > setIconSize (QSize (50,25)); / / set overall icon size / / ui- > tabWidget- > setTabShape (QTabWidget::Triangular); / / set tab shape ui- > tabWidget- > setMovable (true) / / set whether the tab can be dragged ui- > tabWidget- > usesScrollButtons (); / / Tab scroll ui- > toolBar- > setMovable (false);} MainWindow::~MainWindow () {delete ui;} / / Home menu creation void MainWindow::on_actionMain_triggered () {int tab_count = ui- > tabWidget- > count (); int option_count = 0; for (int x = 0; x
< tab_count; x++) { // 获取出每个菜单的标题 QString tab_name = ui->TabWidget- > tabText (x); if (tab_name = = "Home menu") option_count = option_count + 1;} if (option_count
< 1) { FormMain *ptr = new FormMain(this); // 新建选项卡 ptr->SetAttribute (Qt::WA_DeleteOnClose); / / automatically destroy int cur=ui- > tabWidget- > addTab (ptr,QString::asprintf (Home menu)) when closed; ui- > tabWidget- > setTabIcon (cur,QIcon (": / image/1.ico")); ui- > tabWidget- > setCurrentIndex (cur); ui- > tabWidget- > setVisible (true) }} / / create the system setup menu void MainWindow::on_actionOption_triggered () {int tab_count = ui- > tabWidget- > count (); int option_count = 0; for (int x = 0; x
< tab_count; x++) { // 获取出每个菜单的标题 QString tab_name = ui->TabWidget- > tabText (x); if (tab_name = = "system setup") option_count = option_count + 1;} / / determine whether there is only one menu on the home page, and determine the number of tags to identify if (option_count)
< 1) { FormOption *ptr = new FormOption(this); ptr->SetAttribute (Qt::WA_DeleteOnClose); int cur = ui- > tabWidget- > addTab (ptr,QString::asprintf (system setup)); ui- > tabWidget- > setTabIcon (cur,QIcon (": / image/2.ico")); ui- > tabWidget- > setCurrentIndex (cur); ui- > tabWidget- > setVisible (true); popup void MainWindow::on_actionCharts_triggered () {FormCharts * ptr = new FormCharts (this) of the drawing page Ptr- > setAttribute (Qt::WA_DeleteOnClose); int cur = ui- > tabWidget- > addTab (ptr,QString::asprintf ("graphic rendering"); ui- > tabWidget- > setTabIcon (cur,QIcon (": / image/3.ico")); ui- > tabWidget- > setCurrentIndex (cur); ui- > tabWidget- > setVisible (true); void MainWindow::on_tabWidget_tabCloseRequested (int index) {if (indextabWidget- > widget (index); aForm- > close ();}
Thank you for your reading, the above is the content of "what is the method of realizing custom dialog box with Cpicket + QT". After the study of this article, I believe you have a more profound understanding of what is the method of realizing custom dialog box with Cpicket + QT. The specific use of this problem also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.