-
Notifications
You must be signed in to change notification settings - Fork 0
/
motortransport.cpp
86 lines (77 loc) · 2.65 KB
/
motortransport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "motortransport.h"
#include "ui_motortransport.h"
#include "newmotortransport.h"
#include <QTableWidgetItem>
#include <QSqlQuery>
MotorTransport::MotorTransport(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MotorTransport)
{
ui->setupUi(this);
m_pNewMotorTransport = new NewMotorTransport(this);
m_rowCountMotorTransport = this->searchRowCountMotorTransport();
ui->tableWidget->setRowCount(m_rowCountMotorTransport);
QSqlQuery queryShowData;
queryShowData.exec("SELECT brand, stateNumber, speedometer FROM MotorTransport");
queryShowData.next();
for (int i = 0; i < m_rowCountMotorTransport; i++)
{
for (int j = 0; j < 3; j++)
{
QTableWidgetItem *pItem = new QTableWidgetItem();
pItem->setText(QString(queryShowData.value(j).toString()).arg(i).arg(j));
ui->tableWidget->setItem(i, j, pItem);
}
queryShowData.next();
}
}
MotorTransport::~MotorTransport()
{
delete ui;
delete m_pNewMotorTransport;
}
int MotorTransport::searchRowCountMotorTransport()
{
int count;
QSqlQuery queryGetCountMotorTransport;
queryGetCountMotorTransport.exec("SELECT COUNT(*) FROM MotorTransport");
while (queryGetCountMotorTransport.next())
{
count = queryGetCountMotorTransport.value(0).toInt();
}
return count;
}
void MotorTransport::showNewMotorTransportForm()
{
m_pNewMotorTransport->show();
}
void MotorTransport::addTableRowMotorTransport(int count)
{
ui->tableWidget->setRowCount(count + 1);
QSqlQuery queryGetData;
queryGetData.prepare("SELECT brand, stateNumber, speedometer FROM MotorTransport WHERE idMotorTransport = :rowCount");
queryGetData.bindValue(":rowCount", count);
queryGetData.exec();
while (queryGetData.next())
{
for (int j = 0; j < 3; j++)
{
QTableWidgetItem *pItem = new QTableWidgetItem();
pItem->setText(QString(queryGetData.value(j).toString()).arg(count).arg(j));
ui->tableWidget->setItem(count, j, pItem);
}
}
}
void MotorTransport::removeMotorTransport()
{
QModelIndexList selectedRows = ui->tableWidget->selectionModel()->selectedRows();
while (!selectedRows.empty())
{
ui->tableWidget->removeRow(selectedRows[0].row());
QSqlQuery queryRemoveDataMotorTransport;
queryRemoveDataMotorTransport.prepare("DELETE FROM MotorTransport WHERE idMotorTransport = :motorTransport");
queryRemoveDataMotorTransport.bindValue(":motorTransport", selectedRows[0].row());
queryRemoveDataMotorTransport.exec();
selectedRows = ui->tableWidget->selectionModel()->selectedRows();
}
}