基于qt和mysql的学生注册系统(一)
2011-04-01 19:25
357 查看
这学期开了数据库的课,之前也有同学找过我做个类似的东西,但当时不会....
言归正转,
开发环境:ubuntu10.10
ide:qtcreator
数据库:mysql
实现功能:链接数据库,用户登录,数据添加,删除,查询,修改,删除,文件输出。
效果如下:
关于环境的配置在我之前的一篇文章里已经写了,没配置好的可以参照一下。
需要用到的一些qt的知识:
mv架构,qsql类的一些东西。
qt有自己的一套数据库处理的方式,我还是偏向sql语句。
文件组织:
上代码:
先上mainwindow类,主要是页面的布局。
mainwindow.h
mainwindow.cpp
//mainwindow.cpp
#include "mainwindow.h"
QVector<QString> MainWindow::classes;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setGeometry(100,100,1000,768);
setWindowTitle(tr("Student Registration System"));
//状态栏设定
msgLabel=new QLabel;
msgLabel->setMinimumSize(msgLabel->sizeHint());
statusBar()->addWidget(msgLabel);
createActions();
createMenus();
createToolBars();
//createButtons();
tableView= new QTableView(this);
QHBoxLayout *main_lay=new QHBoxLayout;
main_lay->addWidget(tableView);
//main_lay->addLayout(bt_lay);
QWidget *centreWidget=new QWidget(this);
centreWidget->setLayout(main_lay);
setCentralWidget(centreWidget);
model = new QSqlQueryModel(this);
model->setQuery("select * from Student");
tableView->setModel(model);
QVector<QString> cla;
QSqlQuery query;
query.exec("select classNo from Class");
qDebug()<<query.size();
while(query.next())
{
cla.append(query.value(0).toString());
}
classes=cla;
qDebug() << "main:"<<classes.size();
}
MainWindow::~MainWindow()
{
}
void MainWindow::createActions()
{
//about action
aboutAction=new QAction(tr("&About"), this);
aboutAction->setIcon(QIcon(":resources/2.png"));
aboutAction->setStatusTip(tr("OAbout me."));
connect(aboutAction,SIGNAL(triggered()),this,SLOT(slotAbout()));
//help action
helpAction=new QAction(tr("&Help"),this);
helpAction->setStatusTip("Help");
connect(helpAction,SIGNAL(triggered()),this,SLOT(slotHelp()));
//back action
backAction=new QAction(tr("&Back"),this);
backAction->setIcon(QIcon(":resources/7.png"));
backAction->setStatusTip("Back");
connect(backAction,SIGNAL(triggered()),this,SLOT(slotBack()));
//add action
addAction=new QAction(tr("&Add"),this);
addAction->setIcon(QIcon(":resources/1.png"));
addAction->setStatusTip("Add");
connect(addAction,SIGNAL(triggered()),this,SLOT(slotAdd()));
//search action
searchAction=new QAction(tr("&Search"),this);
searchAction->setIcon(QIcon(":resources/3.png"));
searchAction->setStatusTip("search");
connect(searchAction,SIGNAL(triggered()),this,SLOT(slotSearch()));
//delete action
deleteAction=new QAction(tr("&delete"),this);
deleteAction->setIcon(QIcon(":resources/9.png"));
deleteAction->setShortcuts(QKeySequence::Delete);
deleteAction->setStatusTip("delete");
connect(deleteAction,SIGNAL(triggered()),this,SLOT(slotDelete()));
//exit Action
exitAction = new QAction(tr("E&xit"), this);
// exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
///rank Action
rankAction=new QAction(tr("&rank"),this);
rankAction->setIcon(QIcon(":resources/10.png"));
rankAction->setStatusTip("rank");
connect(rankAction,SIGNAL(triggered()),this,SLOT(slotRank()));
//modify Action
modifyAction=new QAction(tr("&modify"),this);
modifyAction->setIcon(QIcon(":resources/11.png"));
modifyAction->setStatusTip("modify");
connect(modifyAction,SIGNAL(triggered()),this,SLOT(slotModify()));
//save Action
saveAction=new QAction(tr("&save File"),this);
saveAction->setStatusTip("Save file");
saveAction->setShortcut(QKeySequence::Save);
saveAction->setIcon(QIcon(":resources/12.png"));
connect(saveAction,SIGNAL(triggered()),this,SLOT(slotSave()));
}
void MainWindow::slotAbout()
{
QMessageBox msg(this);
msg.setWindowTitle("About");
msg.setText(tr("Version:1.0"));
msg.exec();
}
void MainWindow::slotHelp()
{
QMessageBox msg(this);
msg.setWindowTitle("Help");
msg.setText(tr("Email:k283228391@126.com"));
msg.exec();
}
void MainWindow::slotBack()
{
model->setQuery("select * from Student");
tableView->update();
}
void MainWindow::slotDelete()
{
int i=tableView->currentIndex().row();
QVariant j=model->record(i).value(0);
qDebug()<<"currentIndex().data:"<<j;//tableView->indexAt(*p).data();
int b=QMessageBox::question(this,"Attention",tr("Delete row %1?").arg(i+1),QMessageBox::Yes,QMessageBox::Cancel);
qDebug()<<"b:"<<b;//output the value return yes:16384,cancel:4194304
if(b==16384)
{
QSqlQuery queryDel;
queryDel.prepare("delete from Student where Sno=?");
queryDel.addBindValue(j.toString());
queryDel.exec();
model->setQuery("select * from Student");
}
}
void MainWindow::slotSave()
{
this->setWindowOpacity(0.7);
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setAcceptMode(QFileDialog::AcceptSave);
fileDialog->setWindowTitle(tr("Save"));
QString s=QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
fileDialog->setDirectory(s);
fileDialog->setFilter(tr("Txt(*.txt)"));
if(fileDialog->exec() == QDialog::Accepted)
{
QString path = fileDialog->selectedFiles()[0];
if(path.right(3)!="txt")// 修改扩展名
{
path.append(".txt");
}
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
QSqlQuery query;exitAction->setShortcuts(QKeySequence::Quit);
query.exec("select * from Student");
QTextStream stream( &file );
stream << "Student information: "<< "/n";//in ubuntu
stream <<"Number"<<"/t"<<"Name"<<"/t"<<"Sex"<<"/t"<<"Age"<<"/t"<<"Hometown"<<"/t"<<"classNumber"<<"/n";
while(query.next())
{
stream << query.value(0).toInt() <<"/t"<<query.value(1).toString()<</
"/t"<<query.value(2).toString()<<"/t"<<query.value(3).toInt()<<"/t"/
<<query.value(4).toString()<<"/t/t"<<query.value(5).toString()<<"/n";
}
file.close();
}
}
else
{
QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
}
this->setWindowOpacity(1.0);
}
bool MainWindow::slotAdd()
{
this->setWindowOpacity(0.7);
add dlg;
if(dlg.exec()==QDialog::Accepted)
{
dlg.Createsql();
model->setQuery("select * from Student");
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::slotModify()
{
int i=tableView->currentIndex().row();
int k=tableView->currentIndex().column();
QVariant j= model->record(i).value(0);
QVariant h=model->headerData(k,Qt::Vertical);//get the headdata of the column
qDebug()<<"currentIndex().data:"<<h;
this->setWindowOpacity(0.7);
mod dlg;
if(dlg.exec()==QDialog::Accepted)
{
if(dlg.Createsql(h.toInt(),j))
{
qDebug()<<"Yse!";
}
else
{
qDebug()<<"No";
}
model->setQuery("select * from Student");
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::slotRank()
{
//QDialog dlg;
this->setWindowOpacity(0.7);
QStringList list;
bool ok;
list<<tr("")<<tr("No.")<<tr("sex")<<tr("age")<<tr("class");
QString s=QInputDialog::getItem(this,tr("Search Inoformation:"),tr("Choose:"),list,0,false,&ok);
qDebug()<<s;
if(s=="age")
{
model->setQuery("select * from Student order by Sage");
}
else if(s=="No.")
{
model->setQuery("select * from Student order by Sno");
}
else if(s=="class")
{
model->setQuery("select * from Student order by classNo");
}
setWindowOpacity(1.0);
}
void MainWindow::slotSearch()
{
this->setWindowOpacity(0.7);
search sch;
if(sch.exec()==QDialog::Accepted)//if accept,create a sql sentance;
{
QString s;
s=sch.searchSql();
qDebug()<<s;
model->setQuery(s);
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::createToolBars()
{
tool=addToolBar(tr("&File"));
tool->addAction(addAction);
tool->addAction(searchAction);
tool->addSeparator();
tool->addAction(deleteAction);
tool->addAction(backAction);
tool->addAction(rankAction);
tool->addAction(modifyAction);
tool->addAction(saveAction);
}
void MainWindow::createMenus()
{
fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(saveAction);
fileMenu->addAction(exitAction);
helpMenu=menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(helpAction);
aboutMenu=menuBar()->addMenu(tr("&About"));
aboutMenu->addAction(aboutAction);
}
void MainWindow::createButtons()
{
modifyBtn=new QPushButton(tr("modify"));
connect(modifyBtn,SIGNAL(clicked()),this,SLOT(slotModify()));
rankBtn=new QPushButton(tr("rank"));
connect(rankBtn,SIGNAL(clicked()),this,SLOT(slotRank()));
}
言归正转,
开发环境:ubuntu10.10
ide:qtcreator
数据库:mysql
实现功能:链接数据库,用户登录,数据添加,删除,查询,修改,删除,文件输出。
效果如下:
关于环境的配置在我之前的一篇文章里已经写了,没配置好的可以参照一下。
需要用到的一些qt的知识:
mv架构,qsql类的一些东西。
qt有自己的一套数据库处理的方式,我还是偏向sql语句。
文件组织:
上代码:
先上mainwindow类,主要是页面的布局。
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QtGui/QMainWindow> #include <QMenuBar> #include <QPushButton> #include <QWidget> #include <QPainter> #include <QLabel> #include <QScrollArea> #include <QAction> #include <QFileDialog> #include <QMessageBox> #include <QHBoxLayout> #include <QVBoxLayout> #include <QTextEdit> #include <QIcon> #include <QToolBar> #include <QLabel> #include <QStatusBar> #include <QLayout> #include <QPainter> #include <QList> #include <QSqlTableModel> #include <QTableView> #include <QDebug> #include <QSqlQuery> #include <QVariant> #include <QStringList> #include <QInputDialog> #include <QComboBox> #include <QDialogButtonBox> #include <QGridLayout> #include <QFileDialog> #include <QStringList> #include <QFile> #include <QVector> #include <QPoint> #include <QSqlRecord> #include"add.h" #include"mod.h" #include"search.h" #include <QDesktopServices> #include <QSystemTrayIcon> class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); void createMenus();//生成菜单 void createActions();//生成动作 void createToolBars();//生成工具栏 void createButtons();//生成按键 static QVector<QString> classes; private: QGridLayout *grid; QSqlQueryModel *model; QMenu *fileMenu; QMenu *helpMenu; QMenu *aboutMenu; QToolBar *tool; QAction *saveAction; QAction *aboutAction; QAction *exitAction; QAction *helpAction; QAction *searchAction; QAction *deleteAction; QAction *backAction; QAction *addAction; QAction *rankAction; QAction *modifyAction; QLabel *msgLabel;//状态栏显示 QLabel *about;//关于 QPushButton *modifyBtn; QPushButton *rankBtn; QTableView *tableView; QDialogButtonBox *buttonBox; QPoint *p; public slots: void slotAbout();//关于我们 void slotHelp();//帮助 bool slotAdd();//增加 void slotDelete();//删除元组 void slotBack();//返回 void slotSearch();//搜索 void slotModify();//修改 void slotRank();//排序 void slotSave();//储存 }; #endif // MAINWINDOW_H
mainwindow.cpp
//mainwindow.cpp
#include "mainwindow.h"
QVector<QString> MainWindow::classes;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setGeometry(100,100,1000,768);
setWindowTitle(tr("Student Registration System"));
//状态栏设定
msgLabel=new QLabel;
msgLabel->setMinimumSize(msgLabel->sizeHint());
statusBar()->addWidget(msgLabel);
createActions();
createMenus();
createToolBars();
//createButtons();
tableView= new QTableView(this);
QHBoxLayout *main_lay=new QHBoxLayout;
main_lay->addWidget(tableView);
//main_lay->addLayout(bt_lay);
QWidget *centreWidget=new QWidget(this);
centreWidget->setLayout(main_lay);
setCentralWidget(centreWidget);
model = new QSqlQueryModel(this);
model->setQuery("select * from Student");
tableView->setModel(model);
QVector<QString> cla;
QSqlQuery query;
query.exec("select classNo from Class");
qDebug()<<query.size();
while(query.next())
{
cla.append(query.value(0).toString());
}
classes=cla;
qDebug() << "main:"<<classes.size();
}
MainWindow::~MainWindow()
{
}
void MainWindow::createActions()
{
//about action
aboutAction=new QAction(tr("&About"), this);
aboutAction->setIcon(QIcon(":resources/2.png"));
aboutAction->setStatusTip(tr("OAbout me."));
connect(aboutAction,SIGNAL(triggered()),this,SLOT(slotAbout()));
//help action
helpAction=new QAction(tr("&Help"),this);
helpAction->setStatusTip("Help");
connect(helpAction,SIGNAL(triggered()),this,SLOT(slotHelp()));
//back action
backAction=new QAction(tr("&Back"),this);
backAction->setIcon(QIcon(":resources/7.png"));
backAction->setStatusTip("Back");
connect(backAction,SIGNAL(triggered()),this,SLOT(slotBack()));
//add action
addAction=new QAction(tr("&Add"),this);
addAction->setIcon(QIcon(":resources/1.png"));
addAction->setStatusTip("Add");
connect(addAction,SIGNAL(triggered()),this,SLOT(slotAdd()));
//search action
searchAction=new QAction(tr("&Search"),this);
searchAction->setIcon(QIcon(":resources/3.png"));
searchAction->setStatusTip("search");
connect(searchAction,SIGNAL(triggered()),this,SLOT(slotSearch()));
//delete action
deleteAction=new QAction(tr("&delete"),this);
deleteAction->setIcon(QIcon(":resources/9.png"));
deleteAction->setShortcuts(QKeySequence::Delete);
deleteAction->setStatusTip("delete");
connect(deleteAction,SIGNAL(triggered()),this,SLOT(slotDelete()));
//exit Action
exitAction = new QAction(tr("E&xit"), this);
// exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcuts(QKeySequence::Quit);
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
///rank Action
rankAction=new QAction(tr("&rank"),this);
rankAction->setIcon(QIcon(":resources/10.png"));
rankAction->setStatusTip("rank");
connect(rankAction,SIGNAL(triggered()),this,SLOT(slotRank()));
//modify Action
modifyAction=new QAction(tr("&modify"),this);
modifyAction->setIcon(QIcon(":resources/11.png"));
modifyAction->setStatusTip("modify");
connect(modifyAction,SIGNAL(triggered()),this,SLOT(slotModify()));
//save Action
saveAction=new QAction(tr("&save File"),this);
saveAction->setStatusTip("Save file");
saveAction->setShortcut(QKeySequence::Save);
saveAction->setIcon(QIcon(":resources/12.png"));
connect(saveAction,SIGNAL(triggered()),this,SLOT(slotSave()));
}
void MainWindow::slotAbout()
{
QMessageBox msg(this);
msg.setWindowTitle("About");
msg.setText(tr("Version:1.0"));
msg.exec();
}
void MainWindow::slotHelp()
{
QMessageBox msg(this);
msg.setWindowTitle("Help");
msg.setText(tr("Email:k283228391@126.com"));
msg.exec();
}
void MainWindow::slotBack()
{
model->setQuery("select * from Student");
tableView->update();
}
void MainWindow::slotDelete()
{
int i=tableView->currentIndex().row();
QVariant j=model->record(i).value(0);
qDebug()<<"currentIndex().data:"<<j;//tableView->indexAt(*p).data();
int b=QMessageBox::question(this,"Attention",tr("Delete row %1?").arg(i+1),QMessageBox::Yes,QMessageBox::Cancel);
qDebug()<<"b:"<<b;//output the value return yes:16384,cancel:4194304
if(b==16384)
{
QSqlQuery queryDel;
queryDel.prepare("delete from Student where Sno=?");
queryDel.addBindValue(j.toString());
queryDel.exec();
model->setQuery("select * from Student");
}
}
void MainWindow::slotSave()
{
this->setWindowOpacity(0.7);
QFileDialog *fileDialog = new QFileDialog(this);
fileDialog->setAcceptMode(QFileDialog::AcceptSave);
fileDialog->setWindowTitle(tr("Save"));
QString s=QDesktopServices::storageLocation(QDesktopServices::DesktopLocation);
fileDialog->setDirectory(s);
fileDialog->setFilter(tr("Txt(*.txt)"));
if(fileDialog->exec() == QDialog::Accepted)
{
QString path = fileDialog->selectedFiles()[0];
if(path.right(3)!="txt")// 修改扩展名
{
path.append(".txt");
}
QFile file(path);
if (file.open(QIODevice::WriteOnly)) {
QSqlQuery query;exitAction->setShortcuts(QKeySequence::Quit);
query.exec("select * from Student");
QTextStream stream( &file );
stream << "Student information: "<< "/n";//in ubuntu
stream <<"Number"<<"/t"<<"Name"<<"/t"<<"Sex"<<"/t"<<"Age"<<"/t"<<"Hometown"<<"/t"<<"classNumber"<<"/n";
while(query.next())
{
stream << query.value(0).toInt() <<"/t"<<query.value(1).toString()<</
"/t"<<query.value(2).toString()<<"/t"<<query.value(3).toInt()<<"/t"/
<<query.value(4).toString()<<"/t/t"<<query.value(5).toString()<<"/n";
}
file.close();
}
}
else
{
QMessageBox::information(NULL, tr("Path"), tr("You didn't select any files."));
}
this->setWindowOpacity(1.0);
}
bool MainWindow::slotAdd()
{
this->setWindowOpacity(0.7);
add dlg;
if(dlg.exec()==QDialog::Accepted)
{
dlg.Createsql();
model->setQuery("select * from Student");
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::slotModify()
{
int i=tableView->currentIndex().row();
int k=tableView->currentIndex().column();
QVariant j= model->record(i).value(0);
QVariant h=model->headerData(k,Qt::Vertical);//get the headdata of the column
qDebug()<<"currentIndex().data:"<<h;
this->setWindowOpacity(0.7);
mod dlg;
if(dlg.exec()==QDialog::Accepted)
{
if(dlg.Createsql(h.toInt(),j))
{
qDebug()<<"Yse!";
}
else
{
qDebug()<<"No";
}
model->setQuery("select * from Student");
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::slotRank()
{
//QDialog dlg;
this->setWindowOpacity(0.7);
QStringList list;
bool ok;
list<<tr("")<<tr("No.")<<tr("sex")<<tr("age")<<tr("class");
QString s=QInputDialog::getItem(this,tr("Search Inoformation:"),tr("Choose:"),list,0,false,&ok);
qDebug()<<s;
if(s=="age")
{
model->setQuery("select * from Student order by Sage");
}
else if(s=="No.")
{
model->setQuery("select * from Student order by Sno");
}
else if(s=="class")
{
model->setQuery("select * from Student order by classNo");
}
setWindowOpacity(1.0);
}
void MainWindow::slotSearch()
{
this->setWindowOpacity(0.7);
search sch;
if(sch.exec()==QDialog::Accepted)//if accept,create a sql sentance;
{
QString s;
s=sch.searchSql();
qDebug()<<s;
model->setQuery(s);
tableView->update();
}
setWindowOpacity(1.0);
}
void MainWindow::createToolBars()
{
tool=addToolBar(tr("&File"));
tool->addAction(addAction);
tool->addAction(searchAction);
tool->addSeparator();
tool->addAction(deleteAction);
tool->addAction(backAction);
tool->addAction(rankAction);
tool->addAction(modifyAction);
tool->addAction(saveAction);
}
void MainWindow::createMenus()
{
fileMenu=menuBar()->addMenu(tr("&File"));
fileMenu->addAction(saveAction);
fileMenu->addAction(exitAction);
helpMenu=menuBar()->addMenu(tr("&Help"));
helpMenu->addAction(helpAction);
aboutMenu=menuBar()->addMenu(tr("&About"));
aboutMenu->addAction(aboutAction);
}
void MainWindow::createButtons()
{
modifyBtn=new QPushButton(tr("modify"));
connect(modifyBtn,SIGNAL(clicked()),this,SLOT(slotModify()));
rankBtn=new QPushButton(tr("rank"));
connect(rankBtn,SIGNAL(clicked()),this,SLOT(slotRank()));
}
相关文章推荐
- 基于qt和mysql的学生注册系统(二)
- 基于qt和mysql的学生注册系统(一)
- 基于qt和mysql的学生注册系统(二)
- 基于qt和mysql的学生注册系统(二)
- 基于qt和mysql的学生注册系统(一)
- MySql--实战(基于qt的学生管理系统)
- MySql--实战(基于qt的学生管理系统)
- MySql--实战(基于qt的学生管理系统)
- JAVAEE案例: 基于javaweb、mysql的 学生信息管理系统
- 基于servlet/jsp+mysql(DbUtils )的学生系统
- 基于链表的学生信息管理系统和基于MySQL的学生信息管理系统 -C语言
- 基于数据库MySQL的简易学生信息管理系统
- 基于QT的学生信息管理系统开发
- 笨重的mfc还在基于系统控件,熟练的mfc工程师还比不过学习Qt一个月的学生开发效率高(比较精彩,韦易笑)
- 【php】基于php+mysql 简单的登录注册修改密码系统
- Java基于MySQL实现学生管理系统
- 基于RFID,QT,MYSQL开发的医疗电子病历系统
- 【php】基于php+mysql 简单的登录注册修改密码系统
- JDBC应用、控制台连MySQL接数据库实现学生管理系统的登录注册、增删改查
- 基于java+mysql学生管理系统的实现