-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomTableModelQt
More file actions
345 lines (274 loc) · 9.56 KB
/
CustomTableModelQt
File metadata and controls
345 lines (274 loc) · 9.56 KB
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <QApplication>
#include <QSqlQueryModel>
#include <QHeaderView>
#include <QCheckBox>
#include <QComboBox>
#include <QHBoxLayout>
#include <QMainWindow>
#include <QTableView>
class CustomTableModel : public QSqlQueryModel
{
Q_OBJECT
public:
CustomTableModel(QObject *parent = nullptr)
: QSqlQueryModel(parent), m_checkBoxColumn(0)
{
// Initialize the header data and add a checkbox column
m_headers << "Checkbox";
m_checkBoxColumn = m_headers.size() - 1;
// Fetch column names from the database view
fetchColumnNames();
}
Qt::ItemFlags flags(const QModelIndex &index) const override
{
// Set checkboxes to be editable
if (index.column() == m_checkBoxColumn)
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
return QSqlQueryModel::flags(index);
}
QVariant data(const QModelIndex &index, int role) const override
{
if (role == Qt::CheckStateRole && index.column() == m_checkBoxColumn)
{
// Return the check state for the checkbox column
return isChecked(index) ? Qt::Checked : Qt::Unchecked;
}
return QSqlQueryModel::data(index, role);
}
bool setData(const QModelIndex &index, const QVariant &value, int role) override
{
if (role == Qt::CheckStateRole && index.column() == m_checkBoxColumn)
{
// Update the check state when the checkbox is clicked
setChecked(index, value == Qt::Checked);
emit dataChanged(index, index);
return true;
}
return QSqlQueryModel::setData(index, value, role);
}
void setCheckBoxColumn(int column)
{
// Set the column for checkboxes
m_checkBoxColumn = column;
}
void fetchColumnNames()
{
// Fetch column names from the database view
QSqlQuery query;
query.prepare("PRAGMA table_info(v_fooBar)");
if (query.exec())
{
while (query.next())
{
m_headers << query.value(1).toString();
}
}
}
void setDistinctValues(int column, QComboBox *comboBox)
{
// Fetch distinct values for a column and populate the combo box
QStringList distinctValues;
QSqlQuery query;
query.prepare("SELECT DISTINCT " + m_headers.at(column) + " FROM v_fooBar");
if (query.exec())
{
while (query.next())
{
distinctValues << query.value(0).toString();
}
}
comboBox->addItems(distinctValues);
comboBox->addItem("-");
}
private:
QStringList m_headers;
int m_checkBoxColumn;
bool isChecked(const QModelIndex &index) const
{
// Retrieve the check state for the checkbox column
return data(index, Qt::CheckStateRole) == Qt::Checked;
}
void setChecked(const QModelIndex &index, bool checked)
{
// Set the check state for the checkbox column
setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
};
class CustomHeaderView : public QHeaderView
{
Q_OBJECT
public:
CustomHeaderView(Qt::Orientation orientation, QWidget *parent = nullptr)
: QHeaderView(orientation, parent)
{
m_comboBoxes.reserve(5); // Assuming a maximum of 5 columns for demonstration
}
void setComboBox(int logicalIndex, QComboBox *comboBox)
{
// Set up the combo box for filtering at the specified logical index
m_comboBoxes.insert(logicalIndex, comboBox);
connect(comboBox, QOverload<int>::of(&QComboBox::activated),
this, &CustomHeaderView::filterActivated);
}
signals:
void filterChanged(int column, const QString &text);
private slots:
void filterActivated(int index)
{
// Emit signal when the combo box selection changes
emit filterChanged(logicalIndex(m_comboBoxes.indexOf(qobject_cast<QComboBox *>(sender()))), currentText());
}
private:
QVector<QComboBox *> m_comboBoxes;
};
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr)
: QMainWindow(parent)
{
// Set up the SQLite database connection and open it
// Assuming you have a QSqlDatabase named "db" connected to your SQLite database
// QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
// db.setDatabaseName("your_database_name.db");
// db.open();
// Create the table view and model
m_tableView = new QTableView(this);
m_customModel = new CustomTableModel(this);
// Set up the initial query for the model (replace this with your actual query)
m_customModel->setQuery("SELECT * FROM v_fooBar");
m_tableView->setModel(m_customModel);
// Create the custom header view
m_customHeaderView = new CustomHeaderView(Qt::Horizontal, m_tableView);
m_tableView->setHorizontalHeader(m_customHeaderView);
// Set up combo boxes for each column in the header view
for (int i = 0; i < m_customModel->columnCount(); ++i)
{
QComboBox *comboBox = new QComboBox(m_customHeaderView);
m_customHeaderView->setComboBox(i, comboBox);
m_customModel->setDistinctValues(i, comboBox);
}
// Set up the layout
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_tableView);
setCentralWidget(new QWidget);
centralWidget()->setLayout(layout);
// Connect signals for filtering
connect(m_customHeaderView, &CustomHeaderView::filterChanged,
this, &MainWindow::filterColumn);
}
private slots:
void filterColumn(int column, const QString &text)
{
// Apply filtering when the combo box selection changes
if (text == "-")
m_customModel->setFilter("");
else
m_customModel->setFilter(QString("%1 = '%2'").arg(m_customModel->headerData(column, Qt::Horizontal).toString(), text));
}
private:
QTableView *m_tableView;
CustomTableModel *m_customModel;
CustomHeaderView *m_customHeaderView;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include <Windows.h>
#include <QDialog>
#include <QApplication>
class MyQtDialog : public QDialog
{
public:
MyQtDialog(QWidget* parent = nullptr) : QDialog(parent), hOverlayWindow(nullptr) {}
void showModallyWithOverlay()
{
createOverlayWindow();
QDialog::exec();
destroyOverlayWindow();
}
private:
HWND hOverlayWindow;
static LRESULT CALLBACK OverlayWindowProcStatic(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
MyQtDialog* instance = reinterpret_cast<MyQtDialog*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (instance)
{
return instance->overlayWindowProc(hwnd, uMsg, wParam, lParam);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK overlayWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
// Make the window transparent
SetLayeredWindowAttributes(hwnd, RGB(0, 0, 0), 0, LWA_COLORKEY);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), SWP_SHOWWINDOW);
break;
case WM_DESTROY:
// Clean up when the overlay window is destroyed
if (qtDialog)
{
qtDialog->close();
delete qtDialog;
}
break;
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
// Ignore mouse and keyboard input to block user interaction with the main window
return 0;
case WM_CLOSE:
// Close the overlay window when the Qt dialog is closed
DestroyWindow(hwnd);
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}
void createOverlayWindow()
{
WNDCLASS wndClass = {};
wndClass.lpfnWndProc = OverlayWindowProcStatic;
wndClass.hInstance = GetModuleHandle(nullptr);
wndClass.lpszClassName = L"OverlayWindowClass";
if (RegisterClass(&wndClass))
{
hOverlayWindow = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST, L"OverlayWindowClass", L"Overlay Window",
WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), nullptr, nullptr, nullptr, nullptr);
SetWindowLongPtr(hOverlayWindow, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
SetLayeredWindowAttributes(hOverlayWindow, RGB(0, 0, 0), 0, LWA_COLORKEY);
ShowWindow(hOverlayWindow, SW_SHOWNORMAL);
UpdateWindow(hOverlayWindow);
}
}
void destroyOverlayWindow()
{
// Clean up when the overlay window is destroyed
if (hOverlayWindow)
{
DestroyWindow(hOverlayWindow);
hOverlayWindow = nullptr;
}
}
};
int main(int argc, char *argv[])
{
// Initialize the Qt application
QApplication a(argc, argv);
// Create an instance of MyQtDialog
MyQtDialog myQtDialog;
// Show the Qt dialog modally with overlay
myQtDialog.showModallyWithOverlay();
return a.exec();
}