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

一、基础

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_OBJECT

private 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; //删除对话框对象
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值