当前位置:Gxlcms > PHP教程 > lesson3-Qt对话框_PHP教程

lesson3-Qt对话框_PHP教程

时间:2021-07-01 10:21:17 帮助过:9人阅读

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、头文件
  1. #ifndef BUILDINDIALOG_H
  2. #define BUILDINDIALOG_H

  3. #include

  4. class buildInDialog : public QDialog
  5. {
  6. Q_OBJECT
  7. public:
  8. buildInDialog();
  9. private:
  10. QPushButton *fileBtn;
  11. QPushButton *colorBtn;
  12. QPushButton *fontBtn;
  13. QPushButton *saveBtn;
  14. QPushButton *closeBtn;

  15. QTextEdit *textEdit;
  16. private slots:
  17. void fileSlot();
  18. void colorSlot();
  19. void fontSlot();
  20. void saveSlot();
  21. void closeSlot();

  22. };



  23. #endif
2、实现文件
  1. #include "buildInDialog.h"

  2. buildInDialog::buildInDialog()
  3. {
  4. fileBtn = new QPushButton("open");
  5. colorBtn = new QPushButton("color");
  6. fontBtn = new QPushButton("font");
  7. saveBtn = new QPushButton("save");
  8. closeBtn = new QPushButton("close");

  9. textEdit = new QTextEdit();


  10. //布局
  11. QVBoxLayout *vLay = new QVBoxLayout();
  12. QHBoxLayout *hLay = new QHBoxLayout();
  13. vLay->addWidget(fileBtn);
  14. vLay->addWidget(colorBtn);
  15. vLay->addWidget(fontBtn);
  16. vLay->addWidget(saveBtn);
  17. vLay->addWidget(closeBtn);

  18. hLay->addWidget(textEdit);
  19. hLay->addLayout(vLay);

  20. setLayout(hLay);

  21. connect(fileBtn, SIGNAL(clicked()), this, SLOT(fileSlot()));
  22. connect(colorBtn, SIGNAL(clicked()), this, SLOT(colorSlot()));
  23. connect(fontBtn, SIGNAL(clicked()), this, SLOT(fontSlot()));
  24. connect(saveBtn, SIGNAL(clicked()), this, SLOT(saveSlot()));
  25. connect(closeBtn, SIGNAL(clicked()), this, SLOT(closeSlot()));
  26. }

  27. void buildInDialog::fileSlot()
  28. {
  29. //获取文件名字
  30. QString str = QFileDialog::getOpenFileName(this, "打开文件", "/", "All File(*.*)");

  31. //打开文件
  32. QFile file(str);
  33. if(!file.open(QIODevice::ReadWrite))
  34. return;
  35. //得到输入流
  36. QTextStream in(&file);
  37. //读取数据
  38. while(!in.atEnd())
  39. {
  40. QString st = in.readLine();
  41. textEdit->append(st);
  42. }
  43. }

  44. void buildInDialog::colorSlot()
  45. {
  46. //获取条色板
  47. QPalette palette = textEdit->palette();
  48. //打开对话框,获取颜色
  49. QColor color = QColorDialog::getColor(palette.color(QPalette::Text), this);

  50. if(color.isValid())
  51. {
  52. //将颜色放到条色板
  53. palette.setColor(QPalette::Window, color);
  54. //加载调色板
  55. textEdit->setPalette(palette);
  56. }

  57. }

  58. void buildInDialog::fontSlot()
  59. {
  60. bool ok;
  61. QFont font = QFontDialog::getFont(&ok);
  62. if(ok)
  63. textEdit->setFont(font);
  64. }

  65. void buildInDialog::saveSlot()
  66. {
  67. bool ok;
  68. //获取输入的信息
  69. QString str = QInputDialog::getText(this, "输入对话框", "请输入名字", QLineEdit::Normal, "wj", &ok);

  70. //根据输入的名字打开文件
  71. QFile file(str);
  72. file.open(QIODevice::WriteOnly);
  73. //获取
输出流
  • QTextStream out(&file);
  • //将textEdit的内容写入到out
  • out<toPlainText()<<"\n";
  • }

  • void buildInDialog::closeSlot()
  • {
  • QProgressDialog *progress = new QProgressDialog();
  • progress->setRange(0, 100);
  • for(int i=0; i<=100; i+=10)
  • {
  • qApp->processEvents();
  • progress->setValue(i);
  • sleep(1);
  • }
  • }
  • 3、主函数
    1. #include "buildInDialog.h"
    2. #include

    3. int main(int argc, char *argv[])
    4. {
    5. //设置编码,防止汉字出现乱码
    6. QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf-8"));
    7. QApplication app(argc, argv);

    8. buildInDialog dialog;
    9. dialog.show();

    10. return app.exec();
    11. }




    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1068089.htmlTechArticlelesson3-Qt对话框 一、QDialog类 1、对话框的概念 对话框在各种软件中都会使用到,一般用来给用户提示信息或者接收用户反馈的信息,因此对...

    人气教程排行