GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Implement an extended table widget to add proper copy to clipboard su… · feiva/sqlitebrowser@d9cd62b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • ExtendedTableWidget.cpp
    • ExtendedTableWidget.h
    • MainWindow.ui
    • src.pro
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/ExtendedTableWidget.cpp‎

Copy file name to clipboard
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <QApplication>
2+
#include <QClipboard>
3+
#include <QKeySequence>
4+
#include <QKeyEvent>
5+
#include "ExtendedTableWidget.h"
6+
7+
ExtendedTableWidget::ExtendedTableWidget(int rows, int columns, QWidget* parent) :
8+
QTableWidget(rows, columns, parent)
9+
{
10+
}
11+
12+
13+
ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) :
14+
QTableWidget(parent)
15+
{
16+
}
17+
18+
void ExtendedTableWidget::copy()
19+
{
20+
// Get list of selected items
21+
QItemSelectionModel* selection = selectionModel();
22+
QModelIndexList indices = selection->selectedIndexes();
23+
24+
// Abort if there's nothing to copy
25+
if(indices.size() == 0)
26+
return;
27+
28+
// Sort the items by row, then by column
29+
qSort(indices);
30+
31+
// Go through all the items...
32+
QString result;
33+
QModelIndex prev = indices.front();
34+
indices.removeFirst();
35+
foreach(QModelIndex index, indices)
36+
{
37+
// Add the content of this cell to the clipboard string
38+
result.append(QString("\"%1\"").arg(prev.data().toString()));
39+
40+
// If this is a new row add a line break, if not add a tab for cell separation
41+
if(index.row() != prev.row())
42+
result.append("\r\n");
43+
else
44+
result.append("\t");
45+
46+
prev = index;
47+
}
48+
result.append(QString("\"%1\"\r\n").arg(indices.last().data().toString())); // And the last cell
49+
50+
// And finally add it to the clipboard
51+
qApp->clipboard()->setText(result);
52+
}
53+
54+
void ExtendedTableWidget::keyPressEvent(QKeyEvent* event)
55+
{
56+
// Call a custom copy method when Ctrl-C is pressed
57+
if(event->matches(QKeySequence::Copy))
58+
copy();
59+
else
60+
QTableWidget::keyPressEvent(event);
61+
}
Collapse file

‎src/ExtendedTableWidget.h‎

Copy file name to clipboard
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __EXTENDEDTABLEWIDGET_H__
2+
#define __EXTENDEDTABLEWIDGET_H__
3+
4+
#include <QTableWidget>
5+
6+
class ExtendedTableWidget : public QTableWidget
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
explicit ExtendedTableWidget(int rows, int columns, QWidget* parent = 0);
12+
explicit ExtendedTableWidget(QWidget* parent = 0);
13+
14+
private:
15+
void copy();
16+
17+
protected:
18+
virtual void keyPressEvent(QKeyEvent* event);
19+
};
20+
21+
#endif
Collapse file

‎src/MainWindow.ui‎

Copy file name to clipboardExpand all lines: src/MainWindow.ui
+10-4Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@
205205
</layout>
206206
</item>
207207
<item>
208-
<widget class="QTableWidget" name="dataTable">
208+
<widget class="ExtendedTableWidget" name="dataTable">
209209
<property name="whatsThis">
210210
<string>This is the database view. You can double-click any record to edit its contents in the cell editor window.</string>
211211
</property>
212212
<property name="selectionMode">
213-
<enum>QAbstractItemView::SingleSelection</enum>
213+
<enum>QAbstractItemView::ExtendedSelection</enum>
214214
</property>
215215
<property name="horizontalScrollMode">
216216
<enum>QAbstractItemView::ScrollPerPixel</enum>
@@ -316,8 +316,8 @@
316316
<rect>
317317
<x>0</x>
318318
<y>0</y>
319-
<width>763</width>
320-
<height>494</height>
319+
<width>278</width>
320+
<height>444</height>
321321
</rect>
322322
</property>
323323
<layout class="QFormLayout" name="formLayout">
@@ -1399,6 +1399,11 @@
13991399
<extends>QTextEdit</extends>
14001400
<header>sqltextedit.h</header>
14011401
</customwidget>
1402+
<customwidget>
1403+
<class>ExtendedTableWidget</class>
1404+
<extends>QTableWidget</extends>
1405+
<header>ExtendedTableWidget.h</header>
1406+
</customwidget>
14021407
</customwidgets>
14031408
<tabstops>
14041409
<tabstop>dbTreeWidget</tabstop>
@@ -2169,5 +2174,6 @@
21692174
<slot>deleteField()</slot>
21702175
<slot>loadPragmas()</slot>
21712176
<slot>savePragmas()</slot>
2177+
<slot>copy()</slot>
21722178
</slots>
21732179
</ui>
Collapse file

‎src/src.pro‎

Copy file name to clipboardExpand all lines: src/src.pro
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ HEADERS += \
2424
ExportCsvDialog.h \
2525
ImportCsvDialog.h \
2626
sqltextedit.h \
27-
sqlitetypes.h
27+
sqlitetypes.h \
28+
ExtendedTableWidget.h
2829

2930
SOURCES += \
3031
sqlitedb.cpp \
@@ -41,7 +42,8 @@ SOURCES += \
4142
ExportCsvDialog.cpp \
4243
ImportCsvDialog.cpp \
4344
sqltextedit.cpp \
44-
sqlitetypes.cpp
45+
sqlitetypes.cpp \
46+
ExtendedTableWidget.cpp
4547

4648
# create a unittest option
4749
CONFIG(unittest) {

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.