• 12543阅读
  • 6回复

C++课程设计(教务管理系统) [复制链接]

上一主题 下一主题
离线hackware
 

图酷模式  只看楼主 倒序阅读 楼主  发表于: 2013-07-14
— 本帖被 XChinux 执行加亮操作(2013-07-14) —
关键词: 求助
花了4天时间做了个教务管理系统,现在基本实现了管理员的功能,当管理员添加教师时,教师的学院属性默认从下拉框中选择,而下拉框中的数据是在启动程序的时候设置的,这样就造成了一个问题,当在“添加院系信息”选项卡添加一个新的学院后,“添加教师信息”选项卡中的“所属学院”下拉框中的信息得不到及时更新。除非重启程序,现在我想实现当“添加教师信息”选项卡被点击后,能重新设置下拉框的值,貌似有一个focusInEvent(),但我试过不能用,求大神给点思路!感激不尽。问题解决之后开放全部源码。
1条评分金钱+3
billions1943 金钱 +3 - 2015-10-10
离线alexltr

只看该作者 1楼 发表于: 2013-07-14
成功添加学院信息后,发送一个自定义信号 emit xxxxxx;
将这个信号与一个槽函数连接 connect();


在这个槽函数中执行你程序启动时设置“所属学院”下拉框的相关代码
    或
直接用addItem将新添加的学院添加到所属学院”下拉框中


就可以啦!

我不从事IT,只是喜欢Qt。
我不是程序员,只是与程序有缘。
我写程序,只是为了让工作变得简单有序!

                      ----  一个一直在入门的编程学习者
离线hackware

只看该作者 2楼 发表于: 2013-07-14
回 1楼(alexltr) 的帖子
很高兴看到你的回复,但是“录入教师信息”和”录入院系信息“属于不同的对话框。二者并无联系,怎么来视线信号和槽的连接呢?我尝试用eventFilter(),收到部分效果但有bug。部分代码如下:
//insert_college
#ifndef ADMINISTRATOR_INSERTCOLLEGEDIALOG_H
#define ADMINISTRATOR_INSERTCOLLEGEDIALOG_H
#include "ui_administrator_insertcollegedialog.h"
#include "departmentsfileitem.h"
#include <string>
#include <QFile>
#include <QDialog>
#include <QMessageBox>

class InsertCollegeDialog:public QDialog,Ui::InsertCollegeDialog
{
    Q_OBJECT
public:
    InsertCollegeDialog(QWidget *parent = 0)
        :QDialog(parent)
    {
        setupUi(this);
        tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        loadDataToTable();
        connect(submitButton,SIGNAL(clicked()),this,SLOT(insertData()));
    }

private slots:
    void insertData()
    {
        if(check(idEdit->text().trimmed().toStdString(),nameEdit->text().trimmed().toStdString()))
        {
            DepartmentsFileItem item(idEdit->text().trimmed().toInt(),nameEdit->text().trimmed().toStdString().c_str());
            if(!item.exist())
            {
                DepartmentsFileItem::insertItem(idEdit->text().trimmed().toInt(),nameEdit->text().trimmed().toStdString().c_str());
                loadDataToTable();
            }
            else
                QMessageBox::warning(this,"警告","该院系已存在!");

            //提示信息
        }

    }

private:
    bool check(string id,string name)
    {
        //一系列数据正确性检查
        if(id.empty())
        {
            QMessageBox::warning(this,"警告","院系编号不能为空!");
            return false;
        }

        if(name.empty())
        {
            QMessageBox::warning(this,"警告","院系名称不能为空!");
            return false;
        }

        return true;
    }

    void loadDataToTable()
    {
        QFile file("departments.dat");
        tableWidget->setRowCount(file.size()/sizeof(DepartmentsFileItem));
        fstream stream;
        stream.open("departments.dat",ios::in|ios::binary);
        DepartmentsFileItem item;
        int i = 0;
        while(stream.read(reinterpret_cast<char *>(&item),sizeof(DepartmentsFileItem)))
        {
            for(int j = 0; j < 3; j++)
            {
                QTableWidgetItem *tabItem = new QTableWidgetItem(item.getItem(j));
                tabItem->setTextAlignment(Qt::AlignCenter);
                tableWidget->setItem(i,j,tabItem);
            }
            i++;
        }
    }

};

#endif // ADMINISTRATOR_INSERTCOLLEGEDIALOG_H
//insert_teacher
#ifndef ADMINISTRATOR_INSERTTEACHERDIALOG_H
#define ADMINISTRATOR_INSERTTEACHERDIALOG_H
#include <QDialog>
#include "ui_administrator_insertteacherdialog.h"
#include "departmentsfileitem.h"
#include <QMessageBox>
#include <string>
#include <QTableWidget>
#include <QTableWidgetItem>
#include <QFile>
#include <QString>
#include <fstream>
#include "teacherfileitem.h"
using namespace std;

class InsertTeacherDialog:public QDialog,public Ui::InsertTeacherDialog
{
    Q_OBJECT
public:
    InsertTeacherDialog(QWidget *parent = 0)
        :QDialog(parent)
    {
        setupUi(this);

        //获取学院信息
        updateCollege();
        tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
        loadDataToTable();
        connect(submitButtton,SIGNAL(clicked()),this,SLOT(insertData()));
    }
private slots:
    void insertData()
    {
        if(check(idEdit->text().trimmed().toStdString(),nameEdit->text().trimmed().toStdString()))
        {
            //获取院系编号
            Item **array = new Item*[MAX_NUMBER_OF_ITEMS_TO_DISPLAY];
            DepartmentsFileItem::find(1,collegeComboBox->currentText().toStdString().c_str(),array);
            int collegeId = static_cast<DepartmentsFileItem *>(array[0])->getId();

            //插入新的记录
            Teacherfileitem item(idEdit->text().toInt(),"","",0,"","","","");
            if(!item.exist())
            {
                Teacherfileitem::insertItem(idEdit->text().toInt(),nameEdit->text().trimmed().toStdString().c_str(),sexComboBox->currentText().toStdString().c_str(),collegeId,dateEdit->text().toStdString().c_str(),politicalComboBox->currentText().toStdString().c_str(),positionComboBox->currentText().toStdString().c_str(),idEdit->text().trimmed().right(6).toStdString().c_str());
                loadDataToTable();
            }
            else
                QMessageBox::warning(this,"警告","该用户已存在!");
        }
    }

private:
    bool check(string id,string name)
    {
        if(id.empty())
        {
            QMessageBox::warning(this,"警告","工号不能为空!");
            return false;
        }

        if(name.empty())
        {
            QMessageBox::warning(this,"警告","教师姓名不能为空!");
            return false;
        }

        if(!collegeComboBox->isEnabled())
        {
            QMessageBox::warning(this,"警告","请录入学院信息!");
            return false;
        }

        return true;
    }

    void loadDataToTable()
    {
        QFile file("teacher.dat");
        tableWidget->setRowCount(file.size()/sizeof(Teacherfileitem));
        fstream stream;
        stream.open("teacher.dat",ios::in|ios::binary);
        Teacherfileitem item;
        int i = 0;
        while(stream.read(reinterpret_cast<char *>(&item),sizeof(Teacherfileitem)))
        {
            for(int j = 0; j < 7; j++)
            {
                QTableWidgetItem *tabItem = new QTableWidgetItem(item.getItem(j));
                tabItem->setTextAlignment(Qt::AlignCenter);

                if(j == 3)
                {
                    //院系
                    int collegeId = item.getDepartment();
                    Item **array = new Item*[MAX_NUMBER_OF_ITEMS_TO_DISPLAY];
                    DepartmentsFileItem::find(0,QString::number(collegeId).toStdString().c_str(),array);
                    DepartmentsFileItem *newItem1 = static_cast<DepartmentsFileItem *>(array[0]);
                    tabItem->setText(newItem1->getName());
                }
                tableWidget->setItem(i,j,tabItem);
            }
            i++;
        }
    }

    void updateCollege()
    {
        collegeComboBox->clear();
        fstream stream;
        stream.open("departments.dat",ios::in|ios::binary);
        DepartmentsFileItem item;
        int numberOfItems = 0;
        while(stream.read(reinterpret_cast<char *>(&item),sizeof(DepartmentsFileItem)))
        {
            collegeComboBox->addItem(item.getName());
            numberOfItems++;
        }
        if(numberOfItems == 0)
        {
            collegeComboBox->addItem("空");
            collegeComboBox->setEnabled(false);
        }
        else
            collegeComboBox->setEnabled(true);
    }
};

#endif // ADMINISTRATOR_INSERTTEACHERDIALOG_H

  
离线似水流年

只看该作者 3楼 发表于: 2013-07-14
简单的方法就是和主窗体联系,在主窗体中创建一个公用信号,然后就可以用“录入教师信息”和”录入院系信息“对话框同时连接主窗体公用信号做不同的操作,一个对话框通过信号出发主窗体公用信号,另一个对话框相应公用信号添加新增学院项目。
只看该作者 4楼 发表于: 2013-08-15
回 楼主(hackware) 的帖子
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
离线492891404

只看该作者 5楼 发表于: 2016-12-28
楼主 能来个源代码么?
离线luoshiyong

只看该作者 6楼 发表于: 2019-06-05
楼主您好,能求一份源码学习吗
快速回复
限100 字节
 
上一个 下一个