基于qt和mysql的学生注册系统(一)

这学期开了数据库的课,之前也有同学找过我做个类似的东西,但当时不会....

言归正转,

开发环境:ubuntu10.10

ide:qtcreator

数据库:mysql

实现功能:链接数据库,用户登录,数据添加,删除,查询,修改,删除,文件输出。

效果如下:

基于qt和mysql的学生注册系统(一)_第1张图片

基于qt和mysql的学生注册系统(一)_第2张图片

基于qt和mysql的学生注册系统(一)_第3张图片

 

 

 

 

 

 

 

 

 

 

 

关于环境的配置在我之前的一篇文章里已经写了,没配置好的可以参照一下。

需要用到的一些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:[email protected]"));
    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()));
}


你可能感兴趣的:(mysql,delete,action,qt,Signal)