search
HomeBackend DevelopmentPHP Tutoriallesson3-Qt对话框_PHP教程

lesson3-Qt对话框

一、QDialog类
1、对话框的概念
对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对话框是应用程序和用户交互的平台。
对话框是一个顶层窗口,不能嵌入到其他窗口中。
2、对话框的种类
1)、模式对话框,该应用程序的其他窗口不能被访问,必须等待当前对话框消失,显示模式对话框一般调用它的exec()函数
2)、非模式对话框,该应用程序的其他窗口还能继续被访问,显示非模式对话框一般调用它的show()函数
3、QDialog类的父类是QWidget





二、QDialog的派生类
为了方便开发人员的使用,Qt对一些特殊功能的对话框做了封装,提供一套标准的对话框。这些内建对话框提供静态函数便于使用,通常都是调用系统本地的对话框
1、QFileDialog

使用方法:
1、打开文件对话框,返回选择的文件名
QString str = QFileDialog::getOpenFileName(
父窗口,
对话框名字,
默认选择路径,
文件过滤器);
2、根据名字打开文件,成功返回true,失败返回false
QFile file(str);
file.open(QIODevice::ReadWrite);
3、得到一个输入流
QTextStream in(&file);
4、逐行读出输入流
in.readLine();


2、QColorDialog

使用方法:
1、获取调色板
QPalette palette = textEdit->palette();
2、打开颜色对话框,获取颜色
QColor color = QColorDialog::getColor(
palette.color(QPalette::Text), //对话框初始颜色
this //父窗口
);
3、设置调色板颜色
palette->setColor(
QPalette::Text, //要设置的调色板的部位
color //要设置的颜色
);
4、加载调色板
textEdit->setPalette(palette);

GUI为不同的部位分别设置了颜色标志


3、QFontDialog


使用方法:
1、打开字体对话框,获取字体
bool ok;
QFont font = QFontDialog::getFont(&ok);
如果点击对话框的“确定”按钮,那么ok的值就会变为true;如果点击对话框的“取消”按钮,那么ok的值就会变为false
2、设置字体
textEdit->setFont(font);

4、QInputDialog

使用方法:
打开输入对话框,输入的内容会返回
QString str = QInputDialog::getText(
this, //父窗口
“inputDialog”, //窗口标题
“please input”, //输入框上面的标签文字
QLineEdit::Normal, //编辑框的显示方式
QDir::home(), //编辑框默认的内容
ok //回填bool变量
)

5、QProgressDialog

QProgress::setRange(0,100) //设置进度条范围
QProgress::setValue(50) //设置进度条当前值

三、QMessageBox
Qt提供了几种显示信息的消息框,这些消息框都是模态对话框,平时在软件里面会经常用到
1、QMessageBox::question
一个具有标题和文本的消息询问框,开发人员可以根据具体需要定制按钮的个数和按钮的作用

2、QMessageBox::informat
一个具有标题和提示文本的提示消息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用

3、QMessageBox::warning
一个具有标题和文本信息的警示消息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用

4、QMessageBox::critical
一个具有标题和文本信息的致命信息框,开发人员可以根据具体需要定制按钮的个数和按钮的作用

5、QMessageBox::about
一个具有标题和文本的消息框

6、QMessageBox::aboutQt
显示关于Qt的消息框

7、消息按钮的制订



四、QDialog实例
1、头文件
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#ifndef BUILDINDIALOG_H<br /> </li><li>#define BUILDINDIALOG_H<br /></li><li><br /></li><li>#include <QtGui><br /></li><li><br /></li><li>class buildInDialog : public QDialog<br /></li><li>{<br /></li><li>Q_OBJECT<br /></li><li>public:<br /></li><li>buildInDialog();<br /></li><li>private:<br /></li><li>QPushButton *fileBtn;<br /></li><li>QPushButton *colorBtn;<br /></li><li>QPushButton *fontBtn;<br /></li><li>QPushButton *saveBtn;<br /></li><li>QPushButton *closeBtn;<br /></li><li><br /></li><li>QTextEdit *textEdit;<br /></li><li>private slots:<br /></li><li>void fileSlot();<br /></li><li>void colorSlot();<br /></li><li>void fontSlot();<br /></li><li>void saveSlot();<br /></li><li>void closeSlot();<br /></li><li><br /></li><li>};<br /></li><li><br /></li><li><br /></li><li><br /></li><li>#endif</li></ol>
2、实现文件
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#include "buildInDialog.h"<br /> </li><li><br /></li><li>buildInDialog::buildInDialog()<br /></li><li>{<br /></li><li>fileBtn = new QPushButton("open");<br /></li><li>colorBtn = new QPushButton("color");<br /></li><li>fontBtn = new QPushButton("font");<br /></li><li>saveBtn = new QPushButton("save");<br /></li><li>closeBtn = new QPushButton("close");<br /></li><li><br /></li><li>textEdit = new QTextEdit();<br /></li><li><br /></li><li><br /></li><li>//布局<br /></li><li>QVBoxLayout *vLay = new QVBoxLayout();<br /></li><li>QHBoxLayout *hLay = new QHBoxLayout();<br /></li><li>vLay->addWidget(fileBtn);<br /></li><li>vLay->addWidget(colorBtn);<br /></li><li>vLay->addWidget(fontBtn);<br /></li><li>vLay->addWidget(saveBtn);<br /></li><li>vLay->addWidget(closeBtn);<br /></li><li><br /></li><li>hLay->addWidget(textEdit);<br /></li><li>hLay->addLayout(vLay);<br /></li><li><br /></li><li>setLayout(hLay);<br /></li><li><br /></li><li>connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));<br /></li><li>connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));<br /></li><li>connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));<br /></li><li>connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));<br /></li><li>connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));<br /></li><li>}<br /></li><li><br /></li><li>void buildInDialog::fileSlot()<br /></li><li>{<br /></li><li>//获取文件名字<br /></li><li>QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");<br /></li><li><br /></li><li>//打开文件<br /></li><li>QFile file(str);<br /></li><li>if(!file.open(QIODevice::ReadWrite))<br /></li><li>return;<br /></li><li>//得到输入流<br /></li><li>QTextStream in(&file);<br /></li><li>//读取数据<br /></li><li>while(!in.atEnd())<br /></li><li>{<br /></li><li>QString st = in.readLine();<br /></li><li>textEdit->append(st);<br /></li><li>}<br /></li><li>}<br /></li><li><br /></li><li>void buildInDialog::colorSlot()<br /></li><li>{<br /></li><li>//获取条色板<br /></li><li>QPalette palette = textEdit->palette();<br /></li><li>//打开对话框,获取颜色<br /></li><li>QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);<br /></li><li><br /></li><li>if(color.isValid())<br /></li><li>{<br /></li><li>//将颜色放到条色板<br /></li><li>palette.setColor(QPalette::Window, color);<br /></li><li>//加载调色板<br /></li><li>textEdit->setPalette(palette);<br /></li><li>}<br /></li><li><br /></li><li>}<br /></li><li><br /></li><li>void buildInDialog::fontSlot()<br /></li><li>{<br /></li><li>bool ok;<br /></li><li>QFont font = QFontDialog::getFont(&ok);<br /></li><li>if(ok)<br /></li><li>textEdit->setFont(font);<br /></li><li>}<br /></li><li><br /></li><li>void buildInDialog::saveSlot()<br /></li><li>{<br /></li><li>bool ok;<br /></li><li>//获取输入的信息<br /></li><li>QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);<br /></li><li><br /></li><li>//根据输入的名字打开文件<br /></li><li>QFile file(str);<br /></li><li>file.open(QIODevice::WriteOnly);<br /></li><li>//获取输出流<br /></li><li>QTextStream out(&file);<br /></li><li>//将textEdit的内容写入到out<br /></li><li>out<<textEdit->toPlainText()<<"\n";<br /></li><li>}<br /></li><li><br /></li><li>void buildInDialog::closeSlot()<br /></li><li>{<br /></li><li>QProgressDialog *progress = new QProgressDialog();<br /></li><li>progress->setRange(0, 100);<br /></li><li>for(int i=0; i<=100; i+=10)<br /></li><li>{<br /></li><li>qApp->processEvents();<br /></li><li>progress->setValue(i);<br /></li><li>sleep(1);<br /></li><li>}<br /></li><li>} </li></ol>
3、主函数
<ol style="margin:0 1px 0 0px;padding-left:40px;" start="1" class="dp-css"><li>#include "buildInDialog.h"<br /> </li><li>#include <QApplication><br /></li><li><br /></li><li>int main(int argc, char *argv[])<br /></li><li>{<br /></li><li>//设置编码,防止汉字出现乱码<br /></li><li>QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));<br /></li><li>QApplication app(argc, argv);<br /></li><li><br /></li><li>buildInDialog dialog;<br /></li><li>dialog.show();<br /></li><li><br /></li><li>return app.exec();<br /></li><li>} </li></ol>




www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068089.htmlTechArticlelesson3-Qt对话框 一、QDialog类 1、对话框的概念 对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Describe different HTTP caching headers (e.g., Cache-Control, ETag, Last-Modified).Apr 17, 2025 am 12:22 AM

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1?Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP: An Introduction to the Server-Side Scripting LanguagePHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP and the Web: Exploring its Long-Term ImpactPHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

Why Use PHP? Advantages and Benefits ExplainedWhy Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft