Редакция 165 | Редакция 172 | К новейшей редакции | Только различия | Учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS
Редакция 165 | Редакция 166 | ||
---|---|---|---|
1 | 1 | #include "mymodel.h"
|
|
2 | 2 | ||
3 | 3 | MyModel::MyModel(QObject *parent) : |
|
4 | 4 | QSqlTableModel(parent) |
|
5 | 5 | {
|
|
6 | 6 | setEditStrategy(QSqlTableModel::OnManualSubmit); |
|
7 | 7 | }
|
|
8 | 8 | ||
9 | 9 | Qt::ItemFlags MyModel::flags( |
|
10 | 10 | const QModelIndex &index) const { |
|
11 | 11 | Qt::ItemFlags flags = QSqlTableModel::flags(index); |
|
12 | 12 | if ((index.column() != 3) && (index.column() != 0) /* && (index.column() != 5)*/) |
|
13 | 13 | flags |= Qt::ItemIsEditable; |
|
14 | 14 | //if (index.column() == 4)
|
|
15 | 15 | // flags |= Qt::ItemIsUserCheckable;
|
|
16 | 16 | else
|
|
17 | 17 | {
|
|
18 | 18 | if ((index.column() == 3) || (index.column() == 0) /* || (index.column() == 5)*/) flags &= ~Qt::ItemIsEditable; // çàïðåùàåì ðåäàêòèðîâàíèå 0, 3 è 5 ñòîëáöà |
|
19 | 19 | }
|
|
20 | 20 | return flags; |
|
21 | 21 | }
|
|
22 | 22 | ||
23 | 23 | ||
24 | 24 | bool MyModel::setData(const QModelIndex &index, const QVariant &value, int role) { |
|
25 | 25 | //// if (index.column()==3) return false; // â òðåòüåé êîëîíêå íå äàåì ïîìåíÿòü çíà÷åíèå
|
|
26 | 26 | bool lResult = false; |
|
27 | 27 | ||
28 | 28 | /*
|
|
29 | 29 | QString str_tmp;
|
|
30 | 30 | if (index.column()==1) { // åñëè ìåíÿåì èìÿ êëàññà
|
|
31 | 31 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
|
32 | 32 | str_tmp.append(tr(" instance"));
|
|
33 | 33 | }
|
|
34 | 34 | */
|
|
35 | 35 | /*
|
|
36 | 36 | QString str_tmp;
|
|
37 | 37 | if (index.column()==0) { // åñëè ìåíÿåì èìÿ êëàññà
|
|
38 | 38 | str_tmp = value.toString(); // òî ïîìåíÿåì è èìÿ òàáëèöû
|
|
39 | 39 | str_tmp.append(tr("_instance"));
|
|
40 | 40 | }
|
|
41 | 41 | */
|
|
42 | 42 | lResult = QSqlTableModel::setData(index, value, role); |
|
43 | 43 | if (lResult) { |
|
44 | 44 | QModelIndex newindex;
|
|
45 | 45 | QSqlRecord rec = record(index.row()); |
|
46 | 46 | ||
47 | 47 | rec.setValue(index.column(), value); |
|
48 | 48 | rec.setGenerated(index.column(), true); |
|
49 | 49 | ||
50 | 50 | /*
|
|
51 | 51 | if (index.column()==1){
|
|
52 | 52 | rec.setValue(index.column()+2, QVariant(str_tmp));
|
|
53 | 53 | rec.setGenerated(index.column()+2, true);
|
|
54 | 54 | newindex = MyModel::index(index.row(), index.column()+2);
|
|
55 | 55 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
56 | 56 | }
|
|
57 | 57 | */
|
|
58 | 58 | /*
|
|
59 | 59 | ||
60 | 60 | if (index.column()==0){
|
|
61 | 61 | rec.setValue(index.column()+3, QVariant(str_tmp));
|
|
62 | 62 | rec.setGenerated(index.column()+3, true);
|
|
63 | 63 | newindex = MyModel::index(index.row(), index.column()+3);
|
|
64 | 64 | QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
65 | 65 | }
|
|
66 | 66 | */
|
|
67 | 67 | ||
68 | 68 | lResult = updateRowInTable(index.row(), rec); |
|
69 | 69 | /// QSqlTableModel::setData(newindex, QVariant(str_tmp), Qt::EditRole);
|
|
70 | 70 | ||
71 | 71 | ||
72 | 72 | }
|
|
73 | 73 | return lResult; |
|
74 | 74 | }
|
|
75 | 75 | ||
76 | 76 | ||
77 | 77 | ||
78 | 78 | QVariant MyModel::data(const QModelIndex &index, int role) const { |
|
79 | 79 | ||
80 | 80 | QVariant value = QSqlTableModel::data(index, role); |
|
81 | 81 | switch (role) { |
|
82 | 82 | case Qt::DisplayRole: return value; |
|
83 | 83 | case Qt::EditRole: return value; |
|
84 | 84 | case Qt::TextColorRole: |
|
85 | 85 | if(index.column() == 1) |
|
86 | 86 | return qVariantFromValue(QColor(Qt::blue)); |
|
87 | 87 | else
|
|
88 | 88 | return value; |
|
89 | 89 | case Qt::TextAlignmentRole: return value; |
|
90 | 90 | case Qt::FontRole: |
|
91 | 91 | if(index.column() == 1) { |
|
92 | 92 | QFont font = QFont("Helvetica", 16, QFont::Bold); |
|
93 | 93 | return qVariantFromValue(font); |
|
94 | 94 | }else |
|
95 | 95 | return value; |
|
96 | 96 | ||
97 | 97 | case Qt::BackgroundColorRole: { |
|
98 | 98 | int a = (index.row() % 2) ? 14 : 0; |
|
99 | 99 | if(index.column() == 2) |
|
100 | 100 | return qVariantFromValue(QColor(220,240-a,230-a)); |
|
101 | 101 | else if(index.column() == 4) |
|
102 | 102 | return qVariantFromValue(QColor(200,220-a,255-a)); |
|
103 | 103 | else
|
|
104 | 104 | return value; |
|
105 | 105 | }
|
|
106 | 106 | ||
107 | 107 | ||
108 | 108 | ||
109 | 109 | case Qt::CheckStateRole: return value; |
|
110 | 110 | ||
111 | 111 | case Qt::SizeHintRole: |
|
112 | 112 | {
|
|
- | 113 | ||
- | 114 | int height = value.toSize().height(); |
|
- | 115 | int widht = value.toSize().width(); |
|
- | 116 | if(index.column() == 5) { |
|
- | 117 | QVariant currentImage = QSqlTableModel::data(index, 0); |
|
- | 118 | QByteArray bytes = currentImage.toByteArray(); |
|
- | 119 | QPixmap pxmp;
|
|
- | 120 | ||
- | 121 | ||
- | 122 | if (currentImage.isValid()) { |
|
- | 123 | pxmp.loadFromData(bytes); |
|
- | 124 | if (height < pxmp.height()) height = pxmp.height() +10; |
|
- | 125 | if (widht < pxmp.width()) widht = pxmp.width() +10; |
|
- | 126 | return QVariant(QSize(widht, height)); |
|
- | 127 | ||
- | 128 | }
|
|
- | 129 | else return value; |
|
- | 130 | ||
- | 131 | }
|
|
- | 132 | ||
- | 133 | ||
113 | 134 | // QSize size(200,100);
|
|
- | 135 | ||
- | 136 | return value; |
|
- | 137 | // return QVariant(size);
|
|
- | 138 | }
|
|
- | 139 | ||
- | 140 | case Qt::DecorationRole: { |
|
- | 141 | /* if(index.column() == 5) {
|
|
- | 142 | QVariant currentImage = QSqlTableModel::data(index, 0);
|
|
- | 143 | QByteArray bytes = currentImage.toByteArray();
|
|
- | 144 | QPixmap pxmp;
|
|
- | 145 | if (currentImage.isValid()) {
|
|
- | 146 | pxmp.loadFromData(bytes);
|
|
- | 147 | QIcon icon;
|
|
- | 148 | icon.addPixmap(pxmp);
|
|
- | 149 | return icon;
|
|
- | 150 | ||
- | 151 | }
|
|
- | 152 | else {
|
|
- | 153 | //QItemDelegate::paint(painter, option, index);
|
|
- | 154 | return value;
|
|
- | 155 | }
|
|
- | 156 | ||
- | 157 | }
|
|
- | 158 | */
|
|
- | 159 | ||
- | 160 | }
|
|
- | 161 | ||
- | 162 | ||
- | 163 | ||
- | 164 | }
|
|
- | 165 | return value; |
|
- | 166 | }
|
|
- | 167 | ||
- | 168 |