Implement an extended table widget to add proper copy to clipboard support

Extend the standard table widget class to make it possible to copy
multiple cells to the clipboard.
This commit is contained in:
Martin Kleusberg
2013-03-16 18:31:12 +01:00
parent 1daaac93e5
commit d9cd62ba49
4 changed files with 96 additions and 6 deletions
+61
View File
@@ -0,0 +1,61 @@
#include <QApplication>
#include <QClipboard>
#include <QKeySequence>
#include <QKeyEvent>
#include "ExtendedTableWidget.h"
ExtendedTableWidget::ExtendedTableWidget(int rows, int columns, QWidget* parent) :
QTableWidget(rows, columns, parent)
{
}
ExtendedTableWidget::ExtendedTableWidget(QWidget* parent) :
QTableWidget(parent)
{
}
void ExtendedTableWidget::copy()
{
// Get list of selected items
QItemSelectionModel* selection = selectionModel();
QModelIndexList indices = selection->selectedIndexes();
// Abort if there's nothing to copy
if(indices.size() == 0)
return;
// Sort the items by row, then by column
qSort(indices);
// Go through all the items...
QString result;
QModelIndex prev = indices.front();
indices.removeFirst();
foreach(QModelIndex index, indices)
{
// Add the content of this cell to the clipboard string
result.append(QString("\"%1\"").arg(prev.data().toString()));
// If this is a new row add a line break, if not add a tab for cell separation
if(index.row() != prev.row())
result.append("\r\n");
else
result.append("\t");
prev = index;
}
result.append(QString("\"%1\"\r\n").arg(indices.last().data().toString())); // And the last cell
// And finally add it to the clipboard
qApp->clipboard()->setText(result);
}
void ExtendedTableWidget::keyPressEvent(QKeyEvent* event)
{
// Call a custom copy method when Ctrl-C is pressed
if(event->matches(QKeySequence::Copy))
copy();
else
QTableWidget::keyPressEvent(event);
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef __EXTENDEDTABLEWIDGET_H__
#define __EXTENDEDTABLEWIDGET_H__
#include <QTableWidget>
class ExtendedTableWidget : public QTableWidget
{
Q_OBJECT
public:
explicit ExtendedTableWidget(int rows, int columns, QWidget* parent = 0);
explicit ExtendedTableWidget(QWidget* parent = 0);
private:
void copy();
protected:
virtual void keyPressEvent(QKeyEvent* event);
};
#endif
+10 -4
View File
@@ -205,12 +205,12 @@
</layout>
</item>
<item>
<widget class="QTableWidget" name="dataTable">
<widget class="ExtendedTableWidget" name="dataTable">
<property name="whatsThis">
<string>This is the database view. You can double-click any record to edit its contents in the cell editor window.</string>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
<enum>QAbstractItemView::ExtendedSelection</enum>
</property>
<property name="horizontalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
@@ -316,8 +316,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>763</width>
<height>494</height>
<width>278</width>
<height>444</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
@@ -1399,6 +1399,11 @@
<extends>QTextEdit</extends>
<header>sqltextedit.h</header>
</customwidget>
<customwidget>
<class>ExtendedTableWidget</class>
<extends>QTableWidget</extends>
<header>ExtendedTableWidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>dbTreeWidget</tabstop>
@@ -2169,5 +2174,6 @@
<slot>deleteField()</slot>
<slot>loadPragmas()</slot>
<slot>savePragmas()</slot>
<slot>copy()</slot>
</slots>
</ui>
+4 -2
View File
@@ -24,7 +24,8 @@ HEADERS += \
ExportCsvDialog.h \
ImportCsvDialog.h \
sqltextedit.h \
sqlitetypes.h
sqlitetypes.h \
ExtendedTableWidget.h
SOURCES += \
sqlitedb.cpp \
@@ -41,7 +42,8 @@ SOURCES += \
ExportCsvDialog.cpp \
ImportCsvDialog.cpp \
sqltextedit.cpp \
sqlitetypes.cpp
sqlitetypes.cpp \
ExtendedTableWidget.cpp
# create a unittest option
CONFIG(unittest) {