-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlgresult.cpp
214 lines (167 loc) · 5.98 KB
/
dlgresult.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "dlgresult.h"
#include "combodelegate.h"
#include "func.h"
#include <QHBoxLayout>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
#include <QDebug>
#include <QDateTime>
QDlgResult::QDlgResult( QString strTabName, QWidget* parent, Qt::WindowFlags flags )
: QDialog(parent, flags)
{
this->strTabName = strTabName ;
modelQuery = new QSqlQueryModel ;
tabView = new QTableView;
tabView->setModel( modelQuery );
tabView->setSelectionMode(QAbstractItemView::SingleSelection);
tabView->setSelectionBehavior(QAbstractItemView::SelectRows);
tabView->verticalHeader()->setVisible(false);
tabView->setEditTriggers( QAbstractItemView::NoEditTriggers );
tabView->horizontalHeader()->setStretchLastSection( true ); //最后一列占满可视空间
buttonInsert = new QPushButton(tr("增加&A")); //设置功能按钮
buttonDelete = new QPushButton(tr("删除&D"));
buttonUpdate = new QPushButton(tr("保存&S"));
buttonClose = new QPushButton(tr("关闭&C"));
buttonSaveXls = new QPushButton(tr("另存XLS"));
connect(buttonInsert, SIGNAL(clicked()), this, SLOT(insertRow()));
connect(buttonDelete, SIGNAL(clicked()), this, SLOT(deleteRow()));
connect(buttonUpdate, SIGNAL(clicked()), this, SLOT(updateRow()));
connect(buttonClose, SIGNAL(clicked()), this, SLOT(close()));
connect(buttonSaveXls, SIGNAL(clicked()), this, SLOT( slotSaveXls( )));
lblStart = new QLabel(tr("开始时间:")) ;
lblEnd = new QLabel(tr("结束时间:")) ;
edtStart = new QLineEdit ;
edtEnd = new QLineEdit ;
buttonQuery = new QPushButton(tr("查询"));
connect(buttonQuery, SIGNAL(clicked()), this, SLOT(slotQuery()));
QDateTime dtToday = QDateTime::currentDateTime( );
QDate d = dtToday.date() ;
QString strTime1, strTime2 ;
strTime1.sprintf( "%04d-%02d-%02d 00:00:00", d.year(),d.month(),d.day() );
strTime2.sprintf( "%04d-%02d-%02d 23:59:59", d.year(),d.month(),d.day() );
edtStart->setText( strTime1 );
edtEnd->setText( strTime2 );
QHBoxLayout *queryLayout = new QHBoxLayout;
queryLayout->addWidget( lblStart );
queryLayout->addWidget( edtStart );
queryLayout->addWidget( lblEnd );
queryLayout->addWidget( edtEnd );
queryLayout->addWidget( buttonQuery );
QHBoxLayout *bottomLayout = new QHBoxLayout;
bottomLayout->addStretch( );
// bottomLayout -> addWidget(buttonInsert);
// bottomLayout -> addWidget(buttonDelete);
// bottomLayout -> addWidget(buttonUpdate);
bottomLayout -> addWidget(buttonSaveXls);
bottomLayout -> addWidget(buttonClose);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout -> addLayout( queryLayout );
mainLayout -> addWidget( tabView );
mainLayout -> addLayout( bottomLayout );
setLayout( mainLayout );
resize(1200,600);
slotQuery( );
setWindowTitle(tr("流速测量结果"));
}
QDlgResult::~QDlgResult()
{
}
void QDlgResult::retrieveTableView()
{
tabModel->select( );
}
void QDlgResult::insertRow()
{
int row = tabModel->rowCount();
tabModel->insertRow(row);
// tabModel->setData( tabModel->index(row, 0), QVariant(iBoxid) ); //给boxid 字段置初值
QModelIndex index = tabModel->index(row, 0); //设置当前行添加到行尾并定位改行可编辑
tabView->setCurrentIndex(index);
tabView->edit( index );
}
void QDlgResult::deleteRow()
{
QModelIndex index = tabView->currentIndex( );
if(index.isValid())
{
tabModel->removeRow(index.row());
tabModel->submitAll();
retrieveTableView();
}
}
void QDlgResult::updateRow()
{
tabView->selectRow( 0 );
tabView->selectRow( -1 );
tabModel->submitAll( );
retrieveTableView( );
}
//格式化时间串
QString QDlgResult::formatTime( QString strTime )
{
QStringList list1, listDate, listTime ;
list1 = strTime.split( " " );
qDebug() << "list size " << list1.size() ;
if( list1.size() != 2 ) return strTime ;
listDate = list1.at(0).split( "-" ) ;
listTime = list1.at(1).split( ":" ) ;
if( listDate.size() != 3 ) return strTime ;
if( listTime.size() != 3 ) return strTime ;
int iTime[6] ;
iTime[0] = listDate.at(0).toInt();
iTime[1] = listDate.at(1).toInt();
iTime[2] = listDate.at(2).toInt();
iTime[3] = listTime.at(0).toInt();
iTime[4] = listTime.at(1).toInt();
iTime[5] = listTime.at(2).toInt();
QString str1 ;
str1.sprintf( "%04d-%02d-%02d %02d:%02d:%02d", iTime[0], iTime[1], iTime[2], \
iTime[3], iTime[4], iTime[5] );
qDebug( ) << str1 ;
return str1 ;
}
void QDlgResult::slotQuery( )
{
QString strSQL ;
strSQL = QString("select dt,qdj,ss,ls,fV, fT, fN, fK, fC, fCdSs from result where dt > '%1' and dt < '%2' order by dt desc")
.arg( formatTime( edtStart->text()) ).arg( formatTime(edtEnd->text()) );
// qDebug( ) << strSQL ;
modelQuery->setQuery( strSQL );
QSqlQuery query;
QString strSql ;
int i = 0 ;
strSql = QString( "select fieldtitle from customtab where tablename = '%1' order by id " )
.arg( strTabName );
query.exec( strSql );
QString strTitle ;
while ( query.next() ){
strTitle = query.value(0).toString();
modelQuery->setHeaderData( i , Qt::Horizontal, strTitle );
i++ ;
}
strSql = QString( "select displaywidth, editkind, edit2 from customtab where tablename = '%1' order by id " )
.arg( strTabName );
query.exec( strSql );
int iWidth, iEditKind ;
QString strEdit2 ;
i = 0 ;
while ( query.next() ){
iWidth = query.value(0).toInt( );
iEditKind = query.value(1).toInt( );
strEdit2 = query.value(2).toString();
tabView->setColumnWidth( i, iWidth );
if( iEditKind == 1 ){
ComboDelegate * pDelegate = new ComboDelegate( iEditKind, strEdit2 );
tabView->setItemDelegateForColumn( i, pDelegate );
}
i++ ;
// qDebug() << iWidth ;
}
}
//存成xls文件
void QDlgResult::slotSaveXls( )
{
QFunc func ;
func.saveXLS( tabView, modelQuery );
}