-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_combo_model.cpp
54 lines (47 loc) · 1.45 KB
/
base_combo_model.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
#include "base_combo_model.h"
#include <QSqlQuery>
namespace {
enum Columns {
Id,
Data,
};
}
BaseComboModel::BaseComboModel( const QString& visualColumn, const QString& queryTail, QObject *parent ) : QSqlQueryModel( parent ) {
QSqlQuery query;
query.prepare( QString( "SELECT %1.id, %2 FROM %3" ).arg( queryTail.split( ' ' ).first() ).arg( visualColumn ).arg( queryTail ) );
query.exec();
QSqlQueryModel::setQuery( query );
}
QVariant BaseComboModel::dataFromParent( QModelIndex index, int column ) const {
return QSqlQueryModel::data( QSqlQueryModel::index( index.row() - 1, column ) );
}
int BaseComboModel::rowCount(const QModelIndex &parent) const {
return QSqlQueryModel::rowCount( parent ) + 1;
}
QVariant BaseComboModel::data(const QModelIndex & item, int role) const {
QVariant result;
if( item.row() == 0 ) {
switch( role ) {
case Qt::UserRole:
result = 0;
break;
case Qt::DisplayRole:
result = "(please select)";
break;
default:
break;
}
} else {
switch( role ) {
case Qt::UserRole:
result = dataFromParent( item, Id );
break;
case Qt::DisplayRole:
result = dataFromParent( item, Data );
break;
default:
break;
}
}
return result;
}