当前位置:网站首页>QT designer for QT learning
QT designer for QT learning
2022-07-28 16:43:00 【tyrocjl】
Create a project directory
mkdir Calculator2Enter the project directory , Launch Designer
1) perform “designer” You can enter the designer
2) In the new form interface, select “Dialog Without Buttons”// You can also select other parent windowsFinish the interface design in the designer
1) from "Widget Box" Select control required , Drag and drop above the parent window
LineEdit(3 individual )
Label(1 individual )
PushButton(1 individual )
2) Set the properties of the parent window and each control
–》 The parent window
objectname( Object name ):CalculatorDialog
notes : In the future, the parent window object name will generate the same class name
font( typeface ): Point size (20)
windowTitle( Window title ): Calculator
–》 Left operand
objectname:m_editX
alignment: level (AlignRight)
–》 Right operands
objectname:m_editY
alignment: level (AlignRight)
–》 Show results
objectname:m_editZ
alignment: level (AlignRight)
readOnly: Check √
–》 plus
objectname:m_label
text:"+"
–》 Equal sign
objectname:m_button
enabled: Uncheck √ // Ban
text:"="
3) Resize and position
–》 Method 1: The mouse to drag and drop
–》 Method 2: keyboard ,ctrl/shift+ Direction key
–》 Method 3: Set up geometry, Location (x,y)/ size ( Width , Height )
–》 Method 4: Set the layout // recommend
4) forms -> preview
5) preservation (ctrl+s)
Specify a filename :CalculatorDialog.ui
6) sign out , Finally, we get CalculatorDialog.ui filetake ".ui"(xml) transformation ".h"(C++) notes : This step is not necessary , To understand the principle
uic CalculatorDialog.ui -o ui_CalculatorDialog.hui_CalculatorDialog.h The contents of the document :
class Ui_CalculatorDialog{ public: Graphical control declaration ; void setupUi(){ // Interface initialization } }; namespace Ui{ class CalculatorDialog:public Ui_CalculatorDialog{ }; } Ui::CalculatorDialog<= Equivalent =>Ui_CalculatorDialogUse ui_CalculatorDialog.h Method :
Method 1: Inherit // Reference resources CalculatorDialog2
class XX:public Ui::CalculatorDialog{
// Inherit the code related to the interface and use it directly
};
Method 2: Combine // Reference resources CalculatorDialog2_2
class XX{
Constructors ():ui(new Ui::CalculatorDialog){}
// adopt "ui->" Access and interface related code
// visit ui In the pointer setupUi Also use ui->
// Add destructors to this class
Ui::CalculatorDialog* ui;
};code 、 structure 、 Testing and running
Inheritance method :
CalculatorDialog.h#ifndef __CALCULATORDIALOG_H #define __CALCULATORDIALOG_H // Method 1 : Inherit #include "ui_CalculatorDialog.h" // Inherit the parent window class , The current class is also the parent window class CalculatorDialog :public QDialog,public Ui::CalculatorDialog{ Q_OBJECT //moc public: // Constructors CalculatorDialog(void); public slots:// Custom slot function // The reset button is the slot function in the normal state void enableButton(void); // Slot function for calculating and displaying results void clickButton(void); }; #endif//__CALCULATORDIALOG_HCalculatorDialog.cpp#include "CalculatorDialog.h" // Constructors CalculatorDialog::CalculatorDialog(void){ // Interface initialization setupUi(this); // Set up the digital verifier : You can only enter content in digital form m_editX->setValidator( new QDoubleValidator(this)); m_editY->setValidator( new QDoubleValidator(this)); // Signal and slot function connection // When the left and right operand text changes , Sending signal textChanged connect(m_editX,SIGNAL(textChanged(QString)), this,SLOT(enableButton(void))); connect(m_editY,SIGNAL(textChanged(QString)), this,SLOT(enableButton(void))); // When the button is clicked , Sending signal clicked() connect(m_button,SIGNAL(clicked(void)), this,SLOT(clickButton(void))); } // Custom slot function // The reset button is the slot function in the normal state void CalculatorDialog::enableButton(void){ bool bXOk,bYOk; //text(): Get text content (QString) //toDouble(): take QString transformation double, Convert to // The result of work is saved in parameters m_editX->text().toDouble(&bXOk); m_editY->text().toDouble(&bYOk); // If both the left and right operands have entered valid data , Then the resume button // In the normal available state , Otherwise, set the disabled state m_button->setEnabled(bXOk && bYOk); } // Slot function for calculating and displaying results void CalculatorDialog::clickButton(void){ // The result of the calculation is double res = m_editX->text().toDouble() + m_editY->text().toDouble(); // Convert the result to a string //number(): take double Convert to QString QString str = QString::number(res); // Show calculation results m_editZ->setText(str); }main.cpp#include "CalculatorDialog.h" #include <QApplication> int main(int argc,char** argv){ QApplication app(argc,argv); CalculatorDialog dialog; dialog.show(); return app.exec(); }Combination method :( If you can combine, you won't inherit )
CalculatorDialog.hAdd destructors and private member sub object pointers#ifndef __CALCULATORDIALOG_H #define __CALCULATORDIALOG_H // Method 1 : Combine #include "ui_CalculatorDialog.h" // Inherit the parent window class , The current class is also the parent window class CalculatorDialog:public QDialog{ Q_OBJECT //moc public: // Constructors CalculatorDialog(void); // Destructor ~CalculatorDialog(void); public slots:// Custom slot function // The reset button is the slot function in the normal state void enableButton(void); // Slot function for calculating and displaying results void clickButton(void); private: // adopt "ui->" Access interface related code Ui::CalculatorDialog* ui; }; #endif//__CALCULATORDIALOG_HCalculatorDialog.cpp#include "CalculatorDialog.h" // Constructors CalculatorDialog::CalculatorDialog(void) :ui(new Ui::CalculatorDialog){ // Interface initialization ui->setupUi(this); // Set up the digital verifier : You can only enter content in digital form ui->m_editX->setValidator( new QDoubleValidator(this)); ui->m_editY->setValidator( new QDoubleValidator(this)); // Signal and slot function connection // When the left and right operand text changes , Sending signal textChanged connect(ui->m_editX,SIGNAL(textChanged(QString)), this,SLOT(enableButton(void))); connect(ui->m_editY,SIGNAL(textChanged(QString)), this,SLOT(enableButton(void))); // When the button is clicked , Sending signal clicked() connect(ui->m_button,SIGNAL(clicked(void)), this,SLOT(clickButton(void))); } CalculatorDialog::~CalculatorDialog(void){ delete ui; } // Custom slot function // The reset button is the slot function in the normal state void CalculatorDialog::enableButton(void){ bool bXOk,bYOk; //text(): Get text content (QString) //toDouble(): take QString transformation double, Convert to // The result of work is saved in parameters ui->m_editX->text().toDouble(&bXOk); ui->m_editY->text().toDouble(&bYOk); // If both the left and right operands have entered valid data , Then the resume button // In the normal available state , Otherwise, set the disabled state ui->m_button->setEnabled(bXOk && bYOk); } // Slot function for calculating and displaying results void CalculatorDialog::clickButton(void){ // The result of the calculation is double res = ui->m_editX->text().toDouble() + ui->m_editY->text().toDouble(); // Convert the result to a string //number(): take double Convert to QString QString str = QString::number(res); // Show calculation results ui->m_editZ->setText(str); }main.cppunchanged
边栏推荐
- Redis source code optimization -- binding core
- Sort 4-heap sort and massive TOPK problem
- 排序1-插入排序与希尔排序
- QT packaging
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- Geodetic coordinate system to Martian coordinate system
- 在vs code上配置Hypermesh二次开发环境
- Early in the morning, pay Bora SMS to say that you won the "prize"? Dealing with server mining virus - kthreaddi
- Asp.net large file block upload breakpoint resume demo
- Wechat official account to obtain material list
猜你喜欢

Some suggestions on optimizing HyperMesh script performance

优化Hypermesh脚本性能的几点建议

Sort 4-heap sort and massive TOPK problem

Ansa secondary development - build ansa/meta secondary development environment on pycharm

FX3开发板 及 原理图
![[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)](/img/e8/2044cae63fe2145ce6294cb1fdfaa0.png)
[pointer internal skill cultivation] character pointer + pointer array + array pointer + pointer parameter (I)

Debugging methods of USB products (fx3, ccg3pa)

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

排序2-冒泡排序与快速排序(递归加非递归讲解)

Leetcode daily practice - 160. Cross linked list
随机推荐
PHP gets the applet code, and the applet jumps with parameters
HM二次开发 - Data Names及其使用
LwIP development | socket | TCP | client
1. Simple command line connection to database
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
500million users, four years earlier than wechat... This app, which has been in operation for 15 years, will be permanently discontinued
Wake up after being repeatedly upset by MQ! Hate code out this MQ manual to help the journey of autumn recruitment
Thoughts on solving the pop-up of malicious computer advertisements
有趣的 Kotlin 0x08:What am I
Debugging methods of USB products (fx3, ccg3pa)
Several methods of HyperMesh running script files
Curl returns blank or null without output. Solve the problem
重置grafana登录密码为默认密码
HyperMesh运行脚本文件的几种方法
Pop up layer prompt in the background
LwIP develops | socket | TCP | keepalive heartbeat mechanism
The epidemic dividend disappeared, and the "home fitness" foam dissipated
ANSA二次开发 - 界面开发工具介绍
Use js direct OSS to store files in Alibaba cloud and solve the limitation of large file upload server
Sort 5-count sort