【Qt数据库程序实例】
首先放几张程序运行的截图:
【备注】源码下载地址(可以直接运行):
https://download.csdn.net/download/zqxdsy/11049683
程序部分
程序介绍:
以主主从视图的形式展示汽车制造厂和生产汽车的关系。
当在汽车制造商中xuan选取某制造商时,下面的汽车列表中将显示出与制造商生产的所有产品;当选中某个车型时,右边的列表将显示出该车车型和制造商的详细信息,车型的相关信息信息存储在XML的文件中。PS:各部分程序的相关注释解析会在后续过程中逐渐加上,先把代码贴上。
一.主界面布局
1.主窗口MianWindow定义主显示界面
头文件mainmainwindow.h具体代码如下:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QGroupBox>
#include <QTableView>
#include <QListWidget>
#include <QLabel>
#include <QFile>
#include <QSqlRelationalTableModel>
#include <QSqlTableModel>
#include <QModelIndex>
#include <QDomNode>
#include <QDomDocument>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
//MainWindow(QWidget *parent = 0);
MainWindow(const QString &factoryTable,const QString &carTable,QFile *carDetails,QWidget *parent = 0); //构造函数
~MainWindow();
private slots:
void addCar();
void changeFactory(QModelIndex index);
void delCar();
void showCarDetails(QModelIndex index);
void showFactorytProfile(QModelIndex index);
private:
QGroupBox *createCarGroupBox();
QGroupBox *createFactoryGroupBox();
QGroupBox *createDetailsGroupBox();
void createMenuBar();
QTableView *carView;
QTableView *factoryView;
QListWidget *attribList;
QLabel *profileLabel;
QLabel *titleLabel;
void decreaseCarCount(QModelIndex index);
void getAttribList(QDomNode car);
QModelIndex indexOfFactory(const QString &factory);
void readCarData();
void removeCarFromDatabase(QModelIndex index);
void removeCarFromFile(int id);
QDomDocument carData;
QFile *file;
QSqlRelationalTableModel *carModel;
QSqlTableModel *factoryModel;
};
#endif // MAINWINDOW_H
2.源文件mainwindow.cpp,各函数的实现
2.1 构造函数的始化和析构函数
#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();
uniqueFactoryId = factoryModel->rowCount();
//布局
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("主从视图"));
}
MainWindow::~MainWindow()
{
}
2.2 创建企业显示布局
createFactoryGroupBox()
{
factoryView = new QTableView;
factoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
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;
}
2.3 创建汽车显示布局
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;
}
2.4 创建详细信息布局
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;