基于QT,写一个车辆信息管理系统的代码。每台车辆至少需要包含以下属性,系统中保存多台车辆的信息:车辆型号,厂商,车型级别,座位数,排量,变速箱,车身颜色,价格。能够浏览所有车辆信息:统计共有多少台车辆,并在界面上显示所有车辆信息;按照厂商查询车所有属于该厂商的车辆信息。
时间: 2024-03-07 09:49:23 浏览: 99
以下是基于QT的车辆信息管理系统代码。
mainwindow.h文件
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QStandardItemModel>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_btnAdd_clicked();
void on_btnDelete_clicked();
void on_btnSearch_clicked();
private:
Ui::MainWindow *ui;
QStandardItemModel *model;
void loadData();
};
#endif // MAINWINDOW_H
```
mainwindow.cpp文件
```c++
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStandardItemModel>
#include <QMessageBox>
struct Car {
QString type;
QString manufacturer;
QString level;
int seats;
double displacement;
QString transmission;
QString color;
double price;
};
QList<Car> cars;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 设置表格头
model = new QStandardItemModel(this);
model->setColumnCount(8);
model->setHeaderData(0, Qt::Horizontal, "车辆型号");
model->setHeaderData(1, Qt::Horizontal, "厂商");
model->setHeaderData(2, Qt::Horizontal, "车型级别");
model->setHeaderData(3, Qt::Horizontal, "座位数");
model->setHeaderData(4, Qt::Horizontal, "排量");
model->setHeaderData(5, Qt::Horizontal, "变速箱");
model->setHeaderData(6, Qt::Horizontal, "车身颜色");
model->setHeaderData(7, Qt::Horizontal, "价格");
ui->tableView->setModel(model);
// 加载数据
loadData();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadData()
{
// 模拟数据
cars.clear();
cars.append({"ModelA", "ManufacturerA", "LevelA", 5, 2.0, "Auto", "Red", 100000.0});
cars.append({"ModelB", "ManufacturerB", "LevelB", 7, 3.0, "Manual", "Black", 150000.0});
cars.append({"ModelC", "ManufacturerA", "LevelB", 5, 2.5, "Auto", "White", 120000.0});
// 显示数据
model->removeRows(0, model->rowCount());
for (int i = 0; i < cars.size(); i++) {
const Car &car = cars.at(i);
QList<QStandardItem*> rowItems;
rowItems.append(new QStandardItem(car.type));
rowItems.append(new QStandardItem(car.manufacturer));
rowItems.append(new QStandardItem(car.level));
rowItems.append(new QStandardItem(QString::number(car.seats)));
rowItems.append(new QStandardItem(QString::number(car.displacement)));
rowItems.append(new QStandardItem(car.transmission));
rowItems.append(new QStandardItem(car.color));
rowItems.append(new QStandardItem(QString::number(car.price)));
model->appendRow(rowItems);
}
// 显示车辆数量
ui->lblCount->setText(QString("共 %1 辆车").arg(cars.size()));
}
void MainWindow::on_btnAdd_clicked()
{
// 打开添加对话框
QString type = QInputDialog::getText(this, "添加车辆", "请输入车辆型号");
if (type.isEmpty()) {
return;
}
QString manufacturer = QInputDialog::getText(this, "添加车辆", "请输入厂商");
if (manufacturer.isEmpty()) {
return;
}
QString level = QInputDialog::getText(this, "添加车辆", "请输入车型级别");
if (level.isEmpty()) {
return;
}
bool ok;
int seats = QInputDialog::getInt(this, "添加车辆", "请输入座位数", 0, 0, 100, 1, &ok);
if (!ok) {
return;
}
double displacement = QInputDialog::getDouble(this, "添加车辆", "请输入排量", 0.0, 0.0, 100.0, 1, &ok);
if (!ok) {
return;
}
QString transmission = QInputDialog::getText(this, "添加车辆", "请输入变速箱");
if (transmission.isEmpty()) {
return;
}
QString color = QInputDialog::getText(this, "添加车辆", "请输入车身颜色");
if (color.isEmpty()) {
return;
}
double price = QInputDialog::getDouble(this, "添加车辆", "请输入价格", 0.0, 0.0, 10000000.0, 1, &ok);
if (!ok) {
return;
}
// 添加到列表
cars.append({type, manufacturer, level, seats, displacement, transmission, color, price});
// 显示到界面
QList<QStandardItem*> rowItems;
rowItems.append(new QStandardItem(type));
rowItems.append(new QStandardItem(manufacturer));
rowItems.append(new QStandardItem(level));
rowItems.append(new QStandardItem(QString::number(seats)));
rowItems.append(new QStandardItem(QString::number(displacement)));
rowItems.append(new QStandardItem(transmission));
rowItems.append(new QStandardItem(color));
rowItems.append(new QStandardItem(QString::number(price)));
model->appendRow(rowItems);
// 显示车辆数量
ui->lblCount->setText(QString("共 %1 辆车").arg(cars.size()));
}
void MainWindow::on_btnDelete_clicked()
{
// 获取选中的行
QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows();
if (indexes.isEmpty()) {
QMessageBox::warning(this, "删除车辆", "请先选中要删除的车辆");
return;
}
// 删除行
int row = indexes.first().row();
cars.removeAt(row);
model->removeRow(row);
// 显示车辆数量
ui->lblCount->setText(QString("共 %1 辆车").arg(cars.size()));
}
void MainWindow::on_btnSearch_clicked()
{
// 打开查询对话框
QString manufacturer = QInputDialog::getText(this, "查询车辆", "请输入厂商");
if (manufacturer.isEmpty()) {
return;
}
// 查询车辆
QList<Car> filteredCars;
for (int i = 0; i < cars.size(); i++) {
const Car &car = cars.at(i);
if (car.manufacturer == manufacturer) {
filteredCars.append(car);
}
}
// 显示查询结果
model->removeRows(0, model->rowCount());
for (int i = 0; i < filteredCars.size(); i++) {
const Car &car = filteredCars.at(i);
QList<QStandardItem*> rowItems;
rowItems.append(new QStandardItem(car.type));
rowItems.append(new QStandardItem(car.manufacturer));
rowItems.append(new QStandardItem(car.level));
rowItems.append(new QStandardItem(QString::number(car.seats)));
rowItems.append(new QStandardItem(QString::number(car.displacement)));
rowItems.append(new QStandardItem(car.transmission));
rowItems.append(new QStandardItem(car.color));
rowItems.append(new QStandardItem(QString::number(car.price)));
model->appendRow(rowItems);
}
// 显示车辆数量
ui->lblCount->setText(QString("共 %1 辆车").arg(filteredCars.size()));
}
```
使用QStandardItemModel作为数据模型,实现了添加、删除、查询等功能。其中,loadData()函数用于加载模拟数据,on_btnAdd_clicked()函数用于打开添加对话框并添加一条车辆信息,on_btnDelete_clicked()函数用于删除选中的车辆信息,on_btnSearch_clicked()函数用于打开查询对话框并查询属于指定厂商的车辆信息。
阅读全文