Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the components of Dialog Dialog Box with how to customize Dialog Dialog + Qt

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to customize Dialog dialog components in Dialog + Qt". In daily operation, I believe many people have doubts about how to customize Dialog dialog components in CAccord + Dialog. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "Qt + Qt how to customize Dialog dialog components". Next, please follow the editor to study!

But sometimes we need to modify more than one data at a time, using the default modal dialog box does not seem to be enough, at this time we need to create a custom dialog box, this kind of dialog box is also a form, so you can put any common components on it to achieve more complex development requirements.

At present, there are two ways to communicate between the custom dialog box and the main form, one is to communicate through the function, the other is to communicate through the signal, we explain how to achieve cross-form communication on the basis of functional communication.

First, you need to create a custom dialog box. The creation process of the dialog box is as follows

Select Project-> AddNew-> QT-> Qt designer Interface Class-> Select Blank Dialog-> name it Dialog Save

Directly select Dianlog.ui and draw the interface as follows, an edit box, two buttons.

Secondly, we need to add two signals to the Dialog dialog box, namely click and close, and associate the signal to the two slot functions, and the signal should be written as follows.

Then we click on dialog.cpp, which is a dialog class. Two member functions need to be defined in the class. Their functions are as follows:

The first GetValue () is used to get the data in the current edit box and return the data to the parent form.

The second SetValue () is used to receive the incoming parameter and set this parameter to the edit box in its own form.

# include "dialog.h" # include "ui_dialog.h" Dialog::Dialog (QWidget * parent): QDialog (parent), ui (new Ui::Dialog) {ui- > setupUi (this);} / / for MainWindow to get the data QString Dialog::GetValue () {return ui- > lineEdit- > text () in the edit box } / / set the data in the current edit box to MainWindow// https://www.cnblogs.com/lysharkvoid Dialog::SetValue (QString x) {ui- > lineEdit- > setText (x);} Dialog::~Dialog () {delete ui;} void Dialog::on_BtnOk_clicked () {} void Dialog::on_BtnCancel_clicked () {}

For the main function, when the user clicks the on_pushButton_clicked () button, we need to dynamically load the Dialog we created, read the value in the edit box of the main form and set it to the child window, when the user presses QDialog::Accepted, it is to get the value of the child window and set it to the edit box of the parent form, the main function code is as follows.

# include "mainwindow.h" # include "ui_mainwindow.h" # include "dialog.h" # include # include MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); ui- > lineEdit- > setEnabled (false); ui- > lineEdit- > setText ("hello lyshark");} MainWindow::~MainWindow () {delete ui } / / By: execute void MainWindow::on_pushButton_clicked () {/ / create modal dialog box Dialog * ptr = new Dialog (this) after clicking the LyShark// https://www.cnblogs.com/lyshark// button; / / create a dialog box Qt::WindowFlags flags = ptr- > windowFlags () / / need to get the return value ptr- > setWindowFlags (flags | Qt::MSWindowsFixedSizeDialogHint); / / set the dialog box fixed size / / read the MainWindows parameter and set it to Dialog QString item = ui- > lineEdit- > text (); ptr- > SetValue (item); int ref = ptr- > exec () / / display the dialog box if (ref==QDialog::Accepted) / / OK key is pressed, close the dialog box {/ / when BtnOk is pressed, set the data in the dialog box QString the_value = ptr- > GetValue (); std::cout setupUi (this); connect (ui- > pushButton, SIGNAL (clicked ()), this, SLOT (onBtnClick () } Dialog::~Dialog () {delete ui;} / / send a signal to MainWindowvoid Dialog::on_pushButton_clicked () {QString send_data = ui- > lineEdit- > text (); emit sendText (send_data);}

The main body header file mainwindow.h defines the slot function for receiveMsg to accept data.

# ifndef MAINWINDOW_H#define MAINWINDOW_H#include namespace Ui {class MainWindow;} class MainWindow: public QMainWindow {Q_OBJECTpublic: explicit MainWindow (QWidget * parent = nullptr); ~ MainWindow (); / / By: LyShark// https://www.cnblogs.com/lysharkprivate slots: / / define slot function void receiveMsg (QString str); void on_pushButton_clicked (); private: Ui::MainWindow * ui;}; # endif / / MAINWINDOW_H

And implement this slot function in mainwindow.cpp.

# include "mainwindow.h" # include "ui_mainwindow.h" # include "dialog.h" # include / / By: LyShark// https://www.cnblogs.com/lysharkMainWindow::MainWindow(QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); ui- > lineEdit- > setEnabled (false);} / / receive the signal and set it to LineEdit void MainWindow::receiveMsg (QString str) {ui- > lineEdit- > setText (str) } MainWindow::~MainWindow () {delete ui;} void MainWindow::on_pushButton_clicked () {Dialog * subwindow = new Dialog (this); / / use receiveMsg slot function to process connect (subwindow, SIGNAL (sendText (QString)), this, SLOT (receiveMsg (QString)) when receiving sendText signal; subwindow- > show ();}

The code runs basically the same as the function-based version, but the signal version is better in terms of flexibility.

This is the basic content of the custom dialog box, flexible operation of these components, it is easy to implement some useful tabular editors.

This is the end of the study on "how to customize Dialog dialog components by CAccord + Qt". I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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: 255

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report