QT5(14)对话框扩展;获取对话框值;exec和show;在对话框显示图片

来源:互联网 发布:中小型网络规划与设计 编辑:程序博客网 时间:2024/06/06 01:12

一、基础

1、 扩展对话框基础QDialog类,我们可以在扩展对话框中实现自定义控件
2、 对话框的exec();//阻塞的,一直到用户关闭对话框,程序才会继续往下执行;show(); 非阻塞的,对话框运行一闪而过就关闭了。

3、获取对话框中数据有两种办法:一种在扩展对话框中写返回类;另一种把变量地址传到扩展对话框对象中。

二、代码

//头文件#ifndef SHOWVERYCODE_H#define SHOWVERYCODE_H#include <QPixmap>#include <QLabel>#include <QLineEdit>#include <QWidget>#include <QVBoxLayout>#include <QDialog>#include <QPushButton>class ShowVerycode : public QDialog   基础自QDialog类{    Q_OBJECTprivate slots:    void getCode();private:    QLabel *codeImg;    QLineEdit *codeString;    QWidget *baseWidget;    QVBoxLayout *mainLayout;    QString codeText;public:    QPushButton *confirm;    ShowVerycode(QPixmap);    QString returnCode();};#endif // SHOWVERYCODE_H
//cpp文件#include "showverycode.h"#include <QVBoxLayout>#include <QLabel>#include <QLineEdit>#include <QPushButton>ShowVerycode::ShowVerycode(QPixmap pi){    setWindowTitle(tr("输入验证码"));    codeImg = new QLabel();    codeString = new QLineEdit();    baseWidget = new QWidget();    confirm = new QPushButton();    codeImg -> setPixmap(pi);    confirm -> setText("确认");//  布局类通过添加layout或widget完成布局,最后通过setLayout将整个布局类添加到窗口上。通过show()显示出来。//  窗口类也可以加载layout。    mainLayout = new QVBoxLayout();    mainLayout -> addWidget(codeImg);    mainLayout -> addWidget(codeString);    mainLayout -> addWidget(confirm);    mainLayout -> setSizeConstraint(QLayout::SetFixedSize);    mainLayout -> setSpacing(20);    connect(confirm,SIGNAL(clicked(bool)),this,SLOT(getCode()));    setLayout(mainLayout);}void ShowVerycode::getCode(){    codeText = codeString->text();    close();   //关闭对话框}QString ShowVerycode::returnCode(){    return codeText;}
//调用类    QPixmap pixmap;    pixmap.loadFromData(duImage.first);    // 对话框首先阻塞运行,当用户确认输入后首先获取输入框值,再关闭对话框,返回获取到的值,再删除对话框。    // 关于获取对话框的返回值,一种是调用对话框返回函数;另一种直接传递个地址变量。    ShowVerycode *dialogImg = new ShowVerycode(pixmap);//    connect(dialogImg->confirm, SIGNAL(clicked(bool)), dialogImg, SLOT(close())); //关闭对话框    dialogImg -> exec();//阻塞的,一直到用户关闭对话框,程序才会继续往下执行     //dialogImg.show(); 非阻塞的,对话框运行一闪而过就关闭了    QString codeDO = dialogImg -> returnCode();    while(codeDO.isEmpty()){        codeDO = dialogImg -> returnCode();    }    delete dialogImg; //删除对话框对象
0 0