Qt 實現簡易的學生信息管理文檔

頭文件:

mianwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTableWidget>
#include <QTreeWidget>
#include <QComboBox>
#include <QLineEdit>
#include <QStringList>
#include <QTableWidgetItem>
#include <QTreeWidgetItem>
#include <QAction>


#include "student.h"
#include "teacher.h"
#include "collage.h"
#include "university.h"
#include "newcollagedialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private:
    void DataInit();
    void TreeInit();
    void creatAction();
private:
    Ui::MainWindow *ui;
    University *u;
    QAction *m_add;
    QAction *m_del;
private slots:
    void onItemClicked(QTreeWidgetItem*item, int index);
    void customContextMenuRequested(QPoint pos);
    void OnCollageAdd();
};

#endif // MAINWINDOW_H


Person.h

#ifndef PERSON_H
#define PERSON_H

#include <iostream>
#include <string>

class Person
{
public:
    Person();
    ~Person();
    Person(const Person &other);
    Person& operator= (const Person &other);
    Person(const int Age,const int Height,const std::string Name);
    virtual void ShowInfo();
public:
    void setAge(const int &Age);
    void setHeight(const int &Height);
    void setName(const std::string &Name);
    const int& Age()const;
    const int& Height()const;
    const std::string &Name()const;
private:
    int age;
    int height;
    std::string name;
};

#endif // PERSON_H
Student.h

#ifndef STUDENT_H
#define STUDENT_H
#include "person.h"
#include <iostream>
using namespace std;

class Student:public Person
{
public:
    Student();
    ~Student();

public:
    Student(const string Sid,const int sAge,
            const int sHeight,string sName);

    friend std::ostream &operator <<(ostream &os,const Student &s);
    void ShowInfo()const;
    Student& operator= (const Student &s);
    bool CompareSid(const string &sid);
    string &StudentSid();
    bool setStudentName(const string &name);
private:
    string SID;
};

#endif // STUDENT_H
Teacher.h

#ifndef TEACHER_H
#define TEACHER_H
#include "person.h"

class Teacher:public Person
{
public:
    Teacher();
    ~Teacher();
    void ShowInfo();
    std::string &TeacherID();
    Teacher(const std::string tid,const int age,
            const int height,const std::string name);
private:
    std::string TID;
};

#endif // TEACHER_H
Collage.h

#ifndef COLLAGE_H
#define COLLAGE_H
#include "student.h"
#include "teacher.h"
#include <list>

class Collage:public Teacher,public Student
{
public:
    Collage();
    ~Collage();
    Collage(string Cid, string Cname);
public:
    bool StudentAdd(Student* s);
    void StudentDel(const string &sid);
    Student *StudentInfo(const string &sid)const;
    void ShowStudentList()const;
    bool CompareCid(const string &CID)const;
    string &CollageCid();
    bool TeacherAdd(Teacher* t);
    void StudentRemove(const string &tid);
    Teacher *TeacherInfo(const string &tid);
public:
    const string &getCid()const;
    const string &getCname()const;
    const void ShowInfo()const;
    std::list<Student*>& sList();
private:
    string cid;
    string cname;
    list<Student *> s_list;
    list<Teacher *> t_list;
};

#endif // COLLAGE_H
University.h

#ifndef UNIVERSITY_H
#define UNIVERSITY_H
#include "collage.h"

class University:public Collage
{
public:
    University();
    University(const string &uid,
               const string &uname,
               const string &uaddr);

    static University *CreatUniversity(const string &uid,
                      const string &uname,
                      const string &uaddr)
    {
        if(nullptr == unty)
        {
            unty = new University(uid,uname,uaddr);
        }
        return unty;
    }

    ~University();
public:
    const void ShowInfo()const;
    bool CollageAdd(Collage *c);
    void CollageDel(const string &cid);
    Collage *CollageInfo(const string &id) const;
    const string& uName()const;
    list<Collage*>& cList();
private:
    string m_uid;
    string m_uname;
    string m_uaddr;
    list<Collage *> m_list;
    static University *unty;
};

#endif // UNIVERSITY_H
源文件:

person.cpp

#include "person.h"
#include "student.h"

Person::Person()
{
}

Person::~Person()
{

}

Person::Person(const Person &other)
{
    this->age = other.age;
    this->height = other.height;
    this->name = other.name;
}

Person &Person::operator=(const Person &other)
{
    if(this == &other)
    {
        return *this;
    }
    this->age = other.age;
    this->height = other.height;
    this->name = other.name;

    return *this;
}

Person::Person(const int Age, const int Height, const std::string Name)
{
    this->age = Age;
    this->height = Height;
    this->name = Name;
}

void Person::ShowInfo()
{
   std::cout << this->age << " " << this->height << " " << this->name << std::endl;
}

void Person::setAge(const int &Age)
{
    this->age = Age;
}

void Person::setHeight(const int &Height)
{
    this->height = Height;
}

void Person::setName(const std::string &Name)
{
    this->name = Name;
}

const int& Person::Age() const
{
    return (this->age);
}

const int& Person::Height() const
{
    return (this->height);
}

const string& Person::Name() const
{
    return (this->name);
}
student.cpp
#include "student.h"

Student::Student()
{

}

Student::~Student()
{

}



Student::Student(const string Sid, const int sAge,
const int sHeight, string sName):SID(Sid),Person(sAge,sHeight,sName)
{
    //this->SID = Sid;
}

ostream &operator <<(ostream &os,const Student &s)
{
    os << s.SID << " " << s.Age() << " "
       <<s.Height() << " " <<s.Name()<<endl;
    return os;
}

void Student::ShowInfo()const
{
    cout << this->SID << " " << this->Age()<< " "
         << this->Height() << " " << this->Name() << endl;

}

Student& Student::operator=(const Student &s)
{
    if(this == &s)
    {
        return *this;
    }
    else
    {
        this->SID = s.SID;
        this->setAge(s.Age());
        this->setHeight(s.Height());
        this->setName(s.Name());
    }
    return *this;
}

bool Student::CompareSid(const string &sid)
{
    if(sid == SID)
    {
        return true;
    }
    else
    {
        return false;
    }
}

string &Student::StudentSid()
{
    return this->SID;
}

bool Student::setStudentName(const string &name)
{
    this->setName(name);
    return true;
}
teacher.cpp

#include "teacher.h"

Teacher::Teacher()
{
}

Teacher::~Teacher()
{

}

Teacher::Teacher(const std::string tid, const int age,
const int height, const std::string name):TID(tid),Person(age,height,name)
{
//    this->TID = tid;
}

void Teacher::ShowInfo()
{
    std::cout << this->TID << " " << this->Age() << " "
              << this->Height() << " " << this->Name() << std::endl;
}

std::string &Teacher::TeacherID()
{
    return this->TID;
}
collage.cpp

#include "collage.h"
#include "student.h"

Collage::Collage()
{
//    s_list.clear();
//    t_list.clear();
}

Collage::~Collage()
{

}

Collage::Collage(string Cid, string Cname):cid(Cid),cname(Cname)
{
//    this->cid = Cid;
//    this->cname = Cname;
}

bool Collage::StudentAdd(Student *s)
{
    if(nullptr != s)
    {
        s_list.push_back(s);
        return true;
    }
    else
    {
        return false;
    }
}

void Collage::StudentDel(const string &sid)
{
    list<Student *>::iterator iter;

    for(iter = s_list.begin();iter != s_list.end();++iter)
    {
        if((*iter)->CompareSid(sid))
        {
            s_list.erase(iter);
            break;
        }
    }
}

Student *Collage::StudentInfo(const string &sid) const
{
    list<Student *>::const_iterator iter;

    for(iter = s_list.begin(); iter != s_list.end();++iter)
    {
        Student* vaule = *iter;
        if(vaule->StudentSid() == sid)
        {
           vaule->ShowInfo();
           break;
        }
//        else
//        {
//            cout << "NO,Find!" << endl;
//        }
    }
    return 0;
}

void Collage::ShowStudentList() const
{
    list<Student *>::const_iterator iter;

    for(iter = s_list.begin(); iter != s_list.end();++iter)
    {
         (*iter)->ShowInfo();
    }
}

bool Collage::CompareCid(const string &CID) const
{
    if(this->cid == CID)
    {
        return true;
    }
    else
    {
        return false;
    }
}

string &Collage::CollageCid()
{
    return this->cid;
}

bool Collage::TeacherAdd(Teacher* t)
{
    if(nullptr != t)
    {
        t_list.push_back(t);
        return true;
    }
    else
    {
        return false;
    }
}

void Collage::StudentRemove(const string &tid)
{
    list<Student *>::iterator iter;

    for(iter = s_list.begin();iter != s_list.end();iter++)
    {
        if((*iter)->CompareSid(tid))
        {
            s_list.remove(*iter);
            break;
        }
    }
}

Teacher *Collage::TeacherInfo(const string &tid)
{
    list<Teacher *>::iterator iter;

    for(iter = t_list.begin(); iter != t_list.end();iter++)
    {
        Teacher* vaule = *iter;
        if(vaule->TeacherID() == tid)
        {
           vaule->ShowInfo();
           break;
        }
        else
        {
            cout << "NO,Find!" << endl;
        }
    }
    return 0;
}

const string &Collage::getCid() const
{
    return cid;
}

const string &Collage::getCname() const
{
    return cname;
}

const void Collage::ShowInfo() const
{
    cout << this->cid << " " << this->cname << endl;
}

list<Student *> &Collage::sList()
{
    return s_list;
}
university.cpp

#include "university.h"


University::University(const string &uid,
                       const string &uname,
                       const string &uaddr)
:m_uid(uid),m_uname(uname),m_uaddr(uaddr)
{

}

University::~University()
{

}

const void University::ShowInfo() const
{
    cout << this->m_uid << " "
         << this->m_uaddr << " "
         << this->m_uname << endl;

}

bool University::CollageAdd(Collage *c)
{
    if(nullptr != c)
    {
        m_list.push_back(c);
        return true;
    }
    else
    {
        return false;
    }
}

void University::CollageDel(const string &cid)
{
    list<Collage *>::iterator iter;

    for(iter = m_list.begin();iter != m_list.end();++iter)
    {
        if((*iter)->CompareCid(cid))
        {
            m_list.remove(*iter);
            break;
        }
    }
}

Collage *University::CollageInfo(const string &id)const
{
    list<Collage *>::const_iterator iter;
//    Collage *p = new Collage;
    for(iter = m_list.begin();iter != m_list.end();++iter)
    {
        if((*iter)->CompareCid(id))
        {
//            cout << "haveing"<<endl;
//            cout << p->getCid() << p->getCname() <<endl;
            return *iter;
        }
    }
    return 0;
}

const string &University::uName() const
{
    return m_uname;
}

std::list<Collage *> &University::cList()
{
    return m_list;
}

University*University:: unty = NULL;

mianwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <iostream>
using namespace std;


void MainWindow::DataInit()
{

    Student *s1 = new Student("201601",16,169,"zhangsan");
    Student *s2 = new Student("201602",17,168,"zhangtwo");
    Student *s3 = new Student("201603",15,178,"zhangthr");
    Student *s4 = new Student("201604",18,188,"zhangfou");

    Collage *c0 = new Collage("c0001", "XX學院");
    Collage *c1 = new Collage("c0002", "OO學院");

    c0->StudentAdd(s1);
    c0->StudentAdd(s2);
    c0->StudentAdd(s3);
    c0->StudentAdd(s4);

    c1->StudentAdd(s2);
    c1->StudentAdd(s3);
   QTreeWidgetItem *txt = new QTreeWidgetItem;
   txt->setText(0,"University");
    ui->treeWidget->setHeaderItem(txt);
    u = University::CreatUniversity("u0001",
                                    "電子科大", "moziqiao");
    u->CollageAdd(c0);
    u->CollageAdd(c1);
}

void MainWindow::TreeInit()
{
    ui->treeWidget->clear();
    QString str = QString::fromStdString(u->uName());
    QTreeWidgetItem *uitem = new QTreeWidgetItem;
    uitem->setText(0, str);
    ui->treeWidget->insertTopLevelItem(0, uitem);

    list<Collage*> &c = u->cList();
    list<Collage*>::iterator i;
    for (i = c.begin(); i != c.end(); ++i)
    {
        QTreeWidgetItem *citem = new QTreeWidgetItem;
        citem->setText(0, QString::fromStdString((*i)->getCname()));
        citem->setText(1, QString::fromStdString((*i)->getCid()));
        uitem->addChild(citem);
    }
//    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect( ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
             this, SLOT(onItemClicked(QTreeWidgetItem*,int)) );
}

void MainWindow::creatAction()
{
    m_add = new QAction("添加",this);
    m_del = new QAction("刪除",this);
    connect(m_add,SIGNAL(triggered()),
            this,SLOT(OnCollageAdd()));
}

void MainWindow::onItemClicked(QTreeWidgetItem *item, int index)
{
    Q_UNUSED(index);
    if( !item->parent()){
        return ;
    }
    Collage *c = u->CollageInfo(item->text(1) .toStdString());
    list<Student *>& s= c->sList();
    list<Student *>::iterator i;

    int j = 0;
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(0);
    ui->tableWidget->setRowCount(0);
    for (i = s.begin(); i != s.end(); ++i)
    {
        ui->tableWidget->insertRow(j);
        QStringList headr;
        headr << "ID" << "姓名" << "年齡" << "身高" ;
        ui->tableWidget->setHorizontalHeaderLabels(headr);

//        ui->tableWidget->verticalHeader()->setHidden(true);//設置垂直標籤隱藏
        ui->tableWidget->verticalHeader()->setVisible(false);//設置垂直標籤不可見
        QTableWidgetItem *sid = new QTableWidgetItem;
        sid->setText(QString::fromStdString((*i)->StudentSid()));
        QTableWidgetItem *sname = new QTableWidgetItem;
        sname->setText(QString::fromStdString((*i)->Name()));
        QTableWidgetItem *sheight = new QTableWidgetItem;
        sheight->setText(QString::number((*i)->Height()));
        QTableWidgetItem *sage = new QTableWidgetItem;
        sage->setText(QString::number((*i)->Age()));

        if( 0==j )
        {
            ui->tableWidget->insertColumn(0);
        }
        ui->tableWidget->setItem(j, 0, sid);
        if( 0==j )
        {
            ui->tableWidget->insertColumn(1);
        }
        ui->tableWidget->setItem(j, 1, sname);
        if( 0==j )
        {
            ui->tableWidget->insertColumn(2);
        }
        ui->tableWidget->setItem(j, 2, sage);
        if( 0==j )
        {
            ui->tableWidget->insertColumn(3);
        }
        ui->tableWidget->setItem(j, 3, sheight);

        j++;
    }
}
//右鍵菜單顯示
void MainWindow::customContextMenuRequested(QPoint pos)
{
    Q_UNUSED(pos);
    QTreeWidgetItem *item = ui->treeWidget->currentItem();
    QMenu *menu = new QMenu;
    menu->addAction(m_add);
    menu->addAction(m_del);
    if(!item->parent())
    {
        menu->exec(QCursor::pos());
//        menu->exec(ui->treeWidget->mapToGlobal(pos));
    }
}
//添加
void MainWindow::OnCollageAdd()
{
    newCollageDialog *Dlg = new newCollageDialog;
    int ret = Dlg->exec();
    if(ret == QDialog::Accepted)
    {
       QString cname = Dlg->getName();
       QString cid = Dlg->getId();
       Collage *c = new Collage(cid.toStdString(),cname.toStdString());
       u->CollageAdd(c);
       TreeInit();
    }
}

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->mainToolBar->addAction(ui->actionOpen);
    ui->mainToolBar->addAction(ui->actionNew);
    ui->mainToolBar->addAction(ui->actionSave);
    DataInit();//數據初始化
    TreeInit();//列表初始化
    creatAction();
    connect(ui->treeWidget,SIGNAL(customContextMenuRequested(QPoint)),
            this,SLOT(customContextMenuRequested(QPoint)));
}

MainWindow::~MainWindow()
{
    delete m_add;
    delete m_del;
    delete ui;
}



main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

界面設計:

用到了Tree Widget,Table Widget


程序設計思路圖:


運行效果圖:


:功能並不完善,源碼下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章