#include "mainwindow.h"
#include <QGridLayout>
#include <QAbstractItemView>
#include <QHeaderView>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QMessageBox>
#include <QSqlRecord>
#include "editdialog.h"
extern int uniqueCarId;
extern int uniqueFactoryId;
MainWindow::MainWindow(const QString &factoryTable,const QString &carTable,QFile *carDetails,QWidget *parent)
: QMainWindow(parent)
{
file = carDetails;
readCarData();
carModel = new QSqlRelationalTableModel(this);
carModel->setTable(carTable);
carModel->setRelation(2, QSqlRelation(factoryTable, "id", "manufactory"));
carModel->select();
factoryModel = new QSqlTableModel(this);
factoryModel->setTable(factoryTable);
factoryModel->select();
QGroupBox *factory = createFactoryGroupBox();
QGroupBox *cars = createCarGroupBox();
QGroupBox *details = createDetailsGroupBox();
uniqueCarId = carModel->rowCount(); //(a)
uniqueFactoryId = factoryModel->rowCount(); //(b)
//布局
QGridLayout *layout = new QGridLayout;
layout->addWidget(factory, 0, 0);
layout->addWidget(cars, 1, 0);
layout->addWidget(details, 0, 1, 2, 1);
layout->setColumnStretch(1, 1);
layout->setColumnMinimumWidth(0, 500);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
createMenuBar();
resize(850, 400);
setWindowTitle(tr("主从视图"));
}
QGroupBox* MainWindow::createFactoryGroupBox()
{
factoryView = new QTableView;
factoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
//(a)
factoryView->setSortingEnabled(true);
factoryView->setSelectionBehavior(QAbstractItemView::SelectRows);
factoryView->setSelectionMode(QAbstractItemView::SingleSelection);
factoryView->setShowGrid(false);
factoryView->setAlternatingRowColors(true);
factoryView->setModel(factoryModel);
connect(factoryView,SIGNAL(clicked (QModelIndex )),this,SLOT(changeFactory(QModelIndex)));
QGroupBox *box = new QGroupBox(tr("汽车制造商"));
QGridLayout *layout = new QGridLayout;
layout->addWidget(factoryView, 0, 0);
box->setLayout(layout);
return box;
}
QGroupBox* MainWindow::createCarGroupBox()
{
QGroupBox *box = new QGroupBox(tr("汽车"));
carView = new QTableView;
carView->setEditTriggers(QAbstractItemView::NoEditTriggers);
carView->setSortingEnabled(true);
carView->setSelectionBehavior(QAbstractItemView::SelectRows);
carView->setSelectionMode(QAbstractItemView::SingleSelection);
carView->setShowGrid(false);
carView->verticalHeader()->hide();
carView->setAlternatingRowColors(true);
carView->setModel(carModel);
connect(carView, SIGNAL(clicked(QModelIndex)),this,SLOT(showCarDetails(QModelIndex)));
connect(carView, SIGNAL(activated(QModelIndex)),this,SLOT(showCarDetails(QModelIndex)));
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(carView, 0, 0);
box->setLayout(layout);
return box;
}
QGroupBox* MainWindow::createDetailsGroupBox()
{
QGroupBox *box = new QGroupBox(tr("详细信息"));
profileLabel = new QLabel;
profileLabel->setWordWrap(true);
profileLabel->setAlignment(Qt::AlignBottom);
titleLabel = new QLabel;
titleLabel->setWordWrap(true);
titleLabel->setAlignment(Qt::AlignBottom);
attribList = new QListWidget;
QGridLayout *layout = new QGridLayout;
layout->addWidget(profileLabel, 0, 0, 1, 2);
layout->addWidget(titleLabel, 1, 0, 1, 2);
layout->addWidget(attribList, 2, 0, 1, 2);
layout->setRowStretch(2, 1);
box->setLayout(layout);
return box;
}
void MainWindow::createMenuBar()
{
QAction *addAction = new QAction(tr("添加"), this);
QAction *deleteAction = new QAction(tr("删除"), this);
QAction *quitAction = new QAction(tr("退出"), this);
addAction->setShortcut(tr("Ctrl+A"));
deleteAction->setShortcut(tr("Ctrl+D"));
quitAction->setShortcut(tr("Ctrl+Q"));
QMenu *fileMenu = menuBar()->addMenu(tr("操作菜单"));
fileMenu->addAction(addAction);
fileMenu->addAction(deleteAction);
fileMenu->addSeparator();
fileMenu->addAction(quitAction);
connect(addAction, SIGNAL(triggered(bool)), this, SLOT(addCar()));
connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(delCar()));
connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
}
void MainWindow::changeFactory(QModelIndex index)
{
QSqlRecord record = factoryModel->record(index.row()); //(a)
QString factoryId = record.value("id").toString(); //(b)
carModel->setFilter("id = '"+ factoryId +"'") ; //(c)
showFactorytProfile(index); //(d)
}
void MainWindow::showFactorytProfile(QModelIndex index)
{
QSqlRecord record = factoryModel->record(index.row()); //(a)
QString name = record.value("manufactory").toString(); //(b)
int count = carModel->rowCount(); //(c)
profileLabel->setText(tr("汽车制造商:%1\n产品数量: %2").arg(name). arg(count)); //(d)
profileLabel->show();
titleLabel->hide();
attribList->hide();
}
void MainWindow::showCarDetails(QModelIndex index)
{
QSqlRecord record = carModel->record(index.row()); //(a)
QString factory = record.value("manufactory").toString();
//(b)
QString name = record.value("name").toString(); //(c)
QString year = record.value("year").toString(); //(d)
QString carId = record.value("carid").toString(); //(e)
showFactorytProfile(indexOfFactory(factory)); //(f)
titleLabel->setText(tr("品牌: %1 (%2)").arg(name).arg(year));
//(g)
titleLabel->show();
QDomNodeList cars = carData.elementsByTagName("car"); //(h)
for (int i = 0; i < cars.count(); i++) //找出所有car标签
{
QDomNode car = cars.item(i);
if (car.toElement().attribute("id") == carId) //(i)
{
getAttribList(car.toElement()); //(j)
break;
}
}
if (!attribList->count() == 0)
attribList->show();
}
void MainWindow::getAttribList(QDomNode car)
{
attribList->clear();
QDomNodeList attribs = car.childNodes();
QDomNode node;
QString attribNumber;
for (int j = 0; j < attribs.count(); j++)
{
node = attribs.item(j);
attribNumber = node.toElement().attribute("number");
QListWidgetItem *item = new QListWidgetItem(attribList);
QString showText(attribNumber + ": " + node.toElement().text());
item->setText(tr("%1").arg(showText));
}
}
void MainWindow::delCar()
{
QModelIndexList selection = carView->selectionModel() ->selectedRows(0);
if (!selection.empty()) //(a)
{
QModelIndex idIndex = selection.at(0);
int id = idIndex.data().toInt();
QString name = idIndex.sibling(idIndex.row(), 1).data(). toString();
QString factory = idIndex.sibling(idIndex.row(), 2).data(). toString();
QMessageBox::StandardButton button;
button = QMessageBox::question(this, tr("删除汽车记录"),
QString(tr("确认删除由'%1'生产的'%2'吗?").arg(factory).arg
(name)),QMessageBox::Yes | QMessageBox::No); //(b)
if (button == QMessageBox::Yes) //得到用户确认
{
removeCarFromFile(id); //从XML文件中删除相关内容
removeCarFromDatabase(idIndex); //从数据库表中删除相关内容
decreaseCarCount(indexOfFactory(factory)); //(c)
}
else
没有合适的资源?快使用搜索试试~ 我知道了~
Qt5开发及实例-CH1302.rar,Qt5以主/从视图的模式展现汽车制造商与生产汽车的关系的例程
共29个文件
cpp:7个
o:7个
h:5个
需积分: 1 1 下载量 128 浏览量
2023-09-15
18:32:38
上传
评论
收藏 2.05MB RAR 举报
温馨提示
Qt5开发及实例,实例CH1302,以主/从视图的模式展现汽车制造商与生产汽车的关系。当在汽车制造商表中选中某一个制造商时,下面的汽车表中将显示出该制造商生产的所有产品。当在汽车表中选中某个车型时,右边的列表将显示出该车的车型和制造商的详细信息,所不同的是,车型的相关信息存储在XML文件中。 1.主界面布局 (1)主窗口MainWindow类继承自QMainWindow类,定义了主显示界面,头文件“mainwindow.h”的具体代码。 (2)源文件“mainwindow.cpp”的具体内容。
资源推荐
资源详情
资源评论
收起资源包目录
Qt5开发及实例_CH1302.rar (29个子文件)
CH1302
build-SQLEx-Desktop_Qt_5_8_0_MinGW_32bit-Debug
ui_connectdlg.h 9KB
attribs.xml 3KB
Makefile 26KB
debug
moc_mainwindow.cpp 4KB
moc_mainwindow.o 483KB
connectdlg.o 795KB
mainwindow.o 889KB
editdialog.o 880KB
SQLEx.exe 4.31MB
moc_editdialog.cpp 3KB
moc_connectdlg.cpp 4KB
main.o 522KB
moc_connectdlg.o 464KB
moc_editdialog.o 821KB
moc_predefs.h 10KB
release
Makefile.Release 130KB
databasefile 5KB
Makefile.Debug 130KB
.qmake.stash 726B
SQLEx
connectdlg.cpp 5KB
mainwindow.h 2KB
mainwindow.cpp 10KB
connectdlg.h 861B
main.cpp 482B
editdialog.cpp 7KB
SQLEx.pro.user 23KB
editdialog.h 1KB
SQLEx.pro 1KB
connectdlg.ui 6KB
共 29 条
- 1
资源评论
华为奋斗者精神
- 粉丝: 8211
- 资源: 241
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功