//logindlg.cpp #include "logindlg.h" #include <QGridLayout> #include <QHBoxLayout> #include <QPalette> loginDlg::loginDlg(QWidget *parent) : QDialog(parent) { /*默认没有点击登录*/ islogin=false; this->setWindowTitle(tr("登录")); label1=new QLabel(tr("用户ID:")); edit1=new QLineEdit(); label2=new QLabel(tr("密码:")); edit2=new QLineEdit(); /*将QLineEdit设置成密码框*/ edit2->setEchoMode(QLineEdit::Password); label3=new QLabel(tr("数据库名称:")); edit3=new QLineEdit(); label4=new QLabel(tr("服务器IP:")); edit4=new QLineEdit(); btn1=new QPushButton(tr("登录")); connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_click())); btn2=new QPushButton(tr("取消")); connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_click())); QHBoxLayout *lay2=new QHBoxLayout(); lay2->addWidget(btn1); lay2->addWidget(btn2); QGridLayout *lay1=new QGridLayout(this); lay1->addWidget(label1,0,0); lay1->addWidget(edit1,0,1); lay1->addWidget(label2,1,0); lay1->addWidget(edit2,1,1); lay1->addWidget(label3,2,0); lay1->addWidget(edit3,2,1); lay1->addWidget(label4,3,0); lay1->addWidget(edit4,3,1); lay1->addLayout(lay2,4,0,1,2); /*设置第0列占位比为1*/ lay1->setColumnStretch(0,1); /*设置第1列占位比为1*/ lay1->setColumnStretch(1,1); /*设置边距*/ lay1->setMargin(15); /*设置控件间的间距*/ lay1->setSpacing(10); /*设置窗口大小不可以随意改变*/ lay1->setSizeConstraint(QLayout::SetFixedSize); /*设置图片填充满对话框背景*/ this->setAutoFillBackground(true); /*设置对话框背景图片*/ QPalette palette1; palette1.setBrush(QPalette::Background,QBrush(QPixmap("12.jpg"))); this->setPalette(palette1); } void loginDlg::btn1_click() { /*用户点击登录*/ islogin=true; //获取用户输入信息 username=edit1->text(); userpass=edit2->text(); dbname=edit3->text(); ipaddr=edit4->text(); /*在登录方法只获取信息,在主函数中处理这些信息,这是为了安全*/ this->close(); /*不关闭对话框,Dialog的exec()方法会永远阻塞*/ } void loginDlg::btn2_click() { /*用户点击取消*/ islogin=false; this->close(); }
//main.cpp #include "mainwindow.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.resize(700,500); w.show(); return a.exec(); }
//scriptdlg.cpp #include "scriptdlg.h" #include <QHBoxLayout> #include <QVBoxLayout> ScriptDlg::ScriptDlg(QWidget *parent) : QDialog(parent) { isexec=false; this->setWindowTitle("执行SQL"); label1=new QLabel(tr("请输入SQL")); tedit1=new QTextEdit(); btn1=new QPushButton(tr("执行")); connect(btn1,SIGNAL(clicked()),this,SLOT(btn1_click())); btn2=new QPushButton(tr("取消")); connect(btn2,SIGNAL(clicked()),this,SLOT(btn2_click())); QHBoxLayout *lay1=new QHBoxLayout(); lay1->addWidget(btn1); lay1->addWidget(btn2); QVBoxLayout *lay2=new QVBoxLayout(); lay2->addWidget(tedit1); lay2->addLayout(lay1); QHBoxLayout *lay3=new QHBoxLayout(this); lay3->addWidget(label1); lay3->addLayout(lay2); /*设置边距*/ lay3->setMargin(15); /*设置间距*/ lay3->setSpacing(10); } void ScriptDlg::btn1_click() { isexec=true; //获取用户输入 this->strsql=tedit1->toPlainText(); this->close(); } void ScriptDlg::btn2_click() { this->close(); }