Table of Contents
lesson3-Qt对话框
Home php教程 php手册 lesson3-Qt对话框

lesson3-Qt对话框

Jun 13, 2016 am 08:51 AM
android

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>
Copy after login
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>
Copy after login
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>
Copy after login




Statement of this Website
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

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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship iQOO Z9 Turbo Plus: Reservations begin for the potentially beefed-up series flagship Sep 10, 2024 am 06:45 AM

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T

See all articles