当前位置:   article > 正文

lv21 QT对话框3

lv21 QT对话框3

1 内置对话框

标准对话框样式

内置对话框基类        

  1. QColorDialog,
  2. QErrorMessage
  3. QFileDialog
  4. QFontDialog
  5. QInputDialog
  6. QMessageBox
  7. QProgressDialog

QDialog Class帮助文档 

示例:各按钮激发对话框实现基类提供的各效果

第一步:实现组件布局()

第二步:实现信号与槽函数

 文件静态成员方法 

打印实现使用qdebug

代码:

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QPushButton>
  5. #include <QTextEdit>
  6. #include<QColorDialog>
  7. #include<QErrorMessage>
  8. #include<QFileDialog>
  9. #include<QFontDialog>
  10. #include<QInputDialog>
  11. #include<QMessageBox>
  12. #include<QProgressDialog>
  13. #include <QDebug>
  14. class Widget : public QWidget
  15. {
  16. Q_OBJECT
  17. public:
  18. Widget(QWidget *parent = 0);
  19. ~Widget();
  20. public slots:
  21. void setcolorf()
  22. {
  23. QColor c = QColorDialog::getColor();
  24. te->setTextColor(c);
  25. }
  26. void showerr()
  27. {
  28. QErrorMessage *msg = QErrorMessage::qtHandler();
  29. msg->showMessage("EEEEEEEE");
  30. }
  31. void getfile()
  32. {
  33. QString filename = QFileDialog::getOpenFileName();
  34. qDebug()<<filename; //qt中打印方法
  35. te->setText(filename);
  36. }
  37. void setfont()
  38. {
  39. bool ok;
  40. QFont myfont = QFontDialog::getFont(&ok);
  41. if(ok)
  42. te->setFont(myfont);
  43. }
  44. void getstr()
  45. {
  46. QString str = QInputDialog::getText(this, "xxxx","yyyyy");
  47. te->setText(str);
  48. }
  49. void showmsg()
  50. {
  51. QMessageBox::information(this, "vvvv", "hello", "AAA");
  52. }
  53. void showprogress()
  54. {
  55. QProgressDialog p;
  56. p.setValue(50);
  57. p.exec();
  58. }
  59. private:
  60. QPushButton *btcolor;
  61. QPushButton *bterrm;
  62. QPushButton *btfile;
  63. QPushButton *btfont;
  64. QPushButton *btinput;
  65. QPushButton *btmsg;
  66. QPushButton *btprg;
  67. QTextEdit *te;
  68. };
  69. #endif // WIDGET_H
  1. #include "widget.h"
  2. #include "QVBoxLayout"
  3. #include "QHBoxLayout"
  4. Widget::Widget(QWidget *parent)
  5. : QWidget(parent)
  6. {
  7. btcolor = new QPushButton("setcolor");
  8. bterrm = new QPushButton("errmsg");
  9. btfile = new QPushButton("getfile");
  10. btfont = new QPushButton("setfont");
  11. btinput = new QPushButton("getstr");
  12. btmsg = new QPushButton("msg");
  13. btprg = new QPushButton("progress");
  14. te = new QTextEdit;
  15. QVBoxLayout *vbox = new QVBoxLayout;
  16. vbox->addWidget(btcolor);
  17. vbox->addWidget(bterrm);
  18. vbox->addWidget(btfile);
  19. vbox->addWidget(btfont);
  20. vbox->addWidget(btinput);
  21. vbox->addWidget(btmsg);
  22. vbox->addWidget(btprg);
  23. QHBoxLayout *mainbox = new QHBoxLayout;
  24. mainbox->addLayout(vbox);
  25. mainbox->addWidget(te);
  26. this->setLayout(mainbox);
  27. connect(btcolor, SIGNAL(clicked(bool)), this, SLOT(setcolorf()));
  28. connect(bterrm, SIGNAL(clicked(bool)), this, SLOT(showerr()));
  29. connect(btfile, SIGNAL(clicked(bool)), this, SLOT(getfile()));
  30. connect(btfont, SIGNAL(clicked(bool)), this, SLOT(setfont()));
  31. connect(btinput, SIGNAL(clicked(bool)), this, SLOT(getstr()));
  32. connect(btmsg, SIGNAL(clicked(bool)), this, SLOT(showmsg()));
  33. connect(btprg, SIGNAL(clicked(bool)), this, SLOT(showprogress()));
  34. }
  35. Widget::~Widget()
  36. {
  37. }

2 自定义对话框 

现象:模态显示,前面不关后面不关,小框可以卡其后面的态

有时候对于弹出对话框可以点确定关闭,可以点X全部关闭,那么这时候需要实现自定义对话框 

 添加新文件,添加C++

文件添加 姓名,基类

生成myQdialog的cpp和.h文件,需要用到信号与槽,Q_OBJEC

myqdialog.h

  1. #ifndef MYQDIALOG_H
  2. #define MYQDIALOG_H
  3. #include <QDialog>
  4. #include <QLineEdit>
  5. #include <QPushButton>
  6. class myQDialog : public QDialog
  7. {
  8. Q_OBJECT
  9. public:
  10. myQDialog();
  11. public slots:
  12. void ok_pushed()
  13. {
  14. stat = true;
  15. close();
  16. }
  17. public:
  18. static int getstat()
  19. {
  20. myQDialog a;
  21. a.exec();
  22. return a.stat;
  23. }
  24. private:
  25. QLineEdit *le;
  26. QPushButton *pb;
  27. int stat;
  28. };
  29. #endif // MYQDIALOG_H

myqdialog.cpp

  1. #include "myqdialog.h"
  2. #include <QVBoxLayout>
  3. myQDialog::myQDialog()
  4. {
  5. le = new QLineEdit("aaaaaa");
  6. pb = new QPushButton("OK");
  7. QVBoxLayout *vbox = new QVBoxLayout;
  8. vbox->addWidget(le);
  9. vbox->addWidget(pb);
  10. setLayout(vbox);
  11. stat = false;
  12. connect(pb, SIGNAL(clicked(bool)), this, SLOT(ok_pushed()));
  13. }

widget.h

  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. class Widget : public QWidget
  5. {
  6. Q_OBJECT
  7. public:
  8. Widget(QWidget *parent = 0);
  9. ~Widget();
  10. };
  11. #endif // WIDGET_H

 widget.cpp

  1. #include "widget.h"
  2. #include <QWidget>
  3. #include <QDialog>
  4. #include "myqdialog.h"
  5. Widget::Widget(QWidget *parent)
  6. : QWidget(parent)
  7. {
  8. #if 0
  9. myQDialog a;
  10. a.setFixedSize(100, 100);
  11. a.exec();
  12. if(!a.stat)
  13. exit(0);
  14. #endif
  15. int s = myQDialog::getstat();
  16. if(!s)
  17. exit(0);
  18. }
  19. Widget::~Widget()
  20. {
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/167732?site
推荐阅读
相关标签
  

闽ICP备14008679号