您的位置:首页 > 编程语言 > C语言/C++

学生管理系统分解学习(一)

2016-11-08 20:23 141 查看
1.首先是学生管理系统的菜单初始化:

void StuInfoManagementSystem::menuBarInit()
{
    menuBar = new QMenuBar(0);//创建菜单
    OperationMenu = new QMenu(QString::fromLocal8Bit("学生"));
    DeleteAction = new QAction(QString::fromLocal8Bit("删除"),this);
    InsertAction = new QAction(QString::fromLocal8Bit("插入"),this);
    OperationMenu->addAction(DeleteAction);
    OperationMenu->addAction(InsertAction);

    helpMenu = new QMenu(QString::fromLocal8Bit("帮助"));
    aboutAction = new QAction(QString::fromLocal8Bit("关于软件"),this);
    aboutQtAction = new QAction(QString::fromLocal8Bit("关于Qt"),this);
    helpMenu->addAction(aboutAction);
    helpMenu->addAction(aboutQtAction);

    schoolAndGradeMenu = new QMenu(QString::fromLocal8Bit("学校/班级"));
    addSchoolAction = new QAction(QString::fromLocal8Bit("添加 学校"),this);
    addGradeAction = new QAction(QString::fromLocal8Bit("添加 班级"),this);
    delSchoolOrGradeAction = new QAction(QString::fromLocal8Bit("删除"),this);
    schoolAndGradeMenu->addAction(addSchoolAction);
    schoolAndGradeMenu->addAction(addGradeAction);
    schoolAndGradeMenu->addAction(delSchoolOrGradeAction);

    menuBar->addMenu(schoolAndGradeMenu);
    menuBar->addMenu(OperationMenu);
    menuBar->addMenu(helpMenu);

    connect(InsertAction,SIGNAL(triggered(bool)),SLOT(sltInsertAction()));
    connect(DeleteAction,SIGNAL(triggered(bool)),SLOT(sltDeleteAction()));

    connect(addSchoolAction,SIGNAL(triggered(bool)),SLOT(sltAddSchoolAction()));
    connect(addGradeAction,SIGNAL(triggered(bool)),SLOT(sltAddGradeAction()));
    connect(delSchoolOrGradeAction,SIGNAL(triggered(bool)),SLOT(sltDelSchoolOrGradeAction()));

  connect(aboutAction,SIGNAL(triggered(bool)),SLOT(sltAboutAction()));
    connect(aboutQtAction,SIGNAL(triggered(bool)),qApp,SLOT(aboutQt()));
}


上面红色字体部分,是对treeWidget部分的实现,学校/班级的增加、删除操作。

绿色字体部分实现的是对每个槽与对应的信号连接:

(1)//插入操作

void StuInfoManagementSystem::sltInsertAction() //插入行操作

{

QList<QStandardItem*> item;

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.append(new QStandardItem(NULL));

item.at(0)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter); //设置指定项的对齐方式

item.at(7)->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);

tableModel->insertRow(tableModel->rowCount(),item);

}


(2)删除指定行操作

void StuInfoManagementSystem::sltDeleteAction() //删除指定行操作

{

int curreantRow = table->currentIndex().row(); //获取当前行数

tableModel->removeRow(curreantRow);

}


(3)添加学校

void StuInfoManagementSystem::sltAddSchoolAction()

{

bool ok;

QString text = QInputDialog::getText(this,

"Please Input School Name",

tr("School name:"),

QLineEdit::Normal,

tr(""),

&ok);

if(ok && !text.isEmpty())

{

QTreeWidgetItem *item = new QTreeWidgetItem;

item->setText(0,text);

int schoolCount = treeWidget->topLevelItemCount();

treeWidget->insertTopLevelItem(schoolCount,item);

}

}

(4)添加班级

void StuInfoManagementSystem::sltAddGradeAction()

{

if(treeWidget->currentItem())

{

    bool ok;

    QString text = QInputDialog::getText(this,

"Please Input Grade Name",

tr("Grade name:"),

    QLineEdit::Normal,

    tr(""),

    &ok);

    if(ok && !text.isEmpty())

{

    QTreeWidgetItem *item = new QTreeWidgetItem;

    item->setText(0,text);


if(treeWidget->currentItem()->parent())//当前选中的是班级

    {

for(int i = 0; i< treeWidget->currentItem()->parent()->childCount(); i++) //同一个学校的班级不能重复

{

if(treeWidget->currentItem()->parent()->child(i)->text(0) == text)

    {


QMessageBox::information(this, QString::fromLocal8Bit("警告"),

QString::fromLocal8Bit("%1 已经存在").arg(text));

return;

}

}

treeWidget->currentItem()->parent()->addChild(item);

}

else

    {

for(int i = 0; i< treeWidget->currentItem()->childCount(); i++) //同一个学校的班级不能重复

{

if(treeWidget->currentItem()->child(i)->text(0) == text)

    {


QMessageBox::information(this, QString::fromLocal8Bit("警告"),

QString::fromLocal8Bit("%1 已经存在").arg(text));

return;

}

}

treeWidget->currentItem()->addChild(item);

}

}

}

treeWidget->expandAll();

}


红色部分多注意逻辑写法

(5)关于操作

void StuInfoManagementSystem::sltAboutAction()

{

QMessageBox::about(this, tr("About Aoftware"),tr("Make By Hero"));

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  QT C++