Qt之消息对话框
widget.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QLineEdit> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); public slots: void Jiemian_buju_Init(); void showdanger(); void showinform(); void showquestion(); private: QLineEdit * qLedit1; QLineEdit* qLedit2; }; #endif // WIDGET_H
widget.cpp:
#include "widget.h" #include<QMessageBox> #include<QPushButton> #include<QVBoxLayout> #include<QLineEdit> #include<QGridLayout> #include<QLabel> #include<QDebug> Widget::Widget(QWidget *parent):QWidget(parent) { Jiemian_buju_Init(); } Widget::~Widget() { } void Widget::Jiemian_buju_Init() { QGridLayout *qg=new QGridLayout(this); QLabel *qL1=new QLabel("用户名:"); QLabel *qL2=new QLabel("密码:"); qLedit1=new QLineEdit; qLedit2=new QLineEdit; qLedit2->setEchoMode(QLineEdit::Password); QPushButton *qb1=new QPushButton("确认:"); QPushButton *qb2=new QPushButton("取消:"); qg->addWidget(qL1,0,0,1,1); qg->addWidget(qL2,1,0,1,1); qg->addWidget(qLedit1,0,1,1,1); qg->addWidget(qLedit2,1,1,1,1); qg->addWidget(qb1,2,0,1,1); qg->addWidget(qb2,2,1,1,1); this->setLayout(qg); connect(qb1,SIGNAL(clicked()),this,SLOT(showdanger())); connect(qb2,SIGNAL(clicked()),this,SLOT(showquestion())); } void Widget::showdanger() { if((qLedit1->text()=="admin")&&(qLedit2->text()=="123456")) { showinform(); } else { QMessageBox::warning(this,"warning","密码错误,登录失败!"); } } void Widget::showinform() { QMessageBox::information(this,"information","登录成功!"); } void Widget::showquestion() { int s=QMessageBox::question(this,"question","确认退出吗?",QMessageBox::Open|QMessageBox::Save); qDebug()<<s<<endl; if(s==2048) { close(); } }
main.cpp:
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }
效果图: