[Qt练习]QIuputDialog 和 QMessageBox

来源:互联网 发布:uag手机壳怎么样 知乎 编辑:程序博客网 时间:2024/05/16 10:11


dlg.h

#ifndef MYDLG_H#define MYDLG_H#include <QDialog>class QPushButton;class QLabel;class QString;class Mydlg:public QDialog{    Q_OBJECTpublic:    Mydlg(QWidget * parent = 0);signals:    void setTxt(QString txt);public slots:    void slot_input_txt();private:    QPushButton *btnok;    QLabel * labeltxt;    QString txt;    QWidget * pParent;};#endif // MYDLG_H

dlg.cpp

#include <QtGui>#include <QWidget>#include <QString>#include <QVBoxLayout>#include <QInputDialog>#include <QMessageBox>#include "mydlg.h"Mydlg::Mydlg(QWidget * parent ) : QDialog(parent){    btnok = new QPushButton("ok");    labeltxt = new QLabel("showtext");    QVBoxLayout *layout = new QVBoxLayout;    pParent = parent;    layout->addWidget(btnok);    layout->addWidget(labeltxt);    connect(btnok, SIGNAL(clicked()), this, SLOT(slot_input_txt()));     //连接槽实现点击输入对话框确定按钮将输入的文本显示在标签控件上    connect(this,SIGNAL(setTxt(QString)), labeltxt,SLOT(setText(QString)));    resize(300,200);    setLayout(layout);}void Mydlg::slot_input_txt(){    bool isOk;    txt = QInputDialog::getText(pParent, "Input Dialog",                                "Please input your Text",                                QLineEdit::Normal,"", &isOk);    if(isOk)    {        QMessageBox ::information(pParent, "information",                                  "Your comment is <b><color = red>" + txt + "</b></color>",                                  QMessageBox::Yes|QMessageBox::No, QMessageBox::Yes);        emit(setTxt(txt));    }}


main.cpp

#include <QApplication>#include "mydlg.h"int main(int argc, char**argv){    QApplication app(argc, argv);    Mydlg *dlg = new Mydlg;    dlg->show();    return app.exec();}

 

运行

qinputdlg

点击ok

qinputdlg2

输入 show me text

qinputdlg1

 

点击ok,显示 messagebox

qmessagebox

 

点击"yes"

qinputdlg3


 

原创粉丝点击