I have a SQLite-Database and I did it into a QSqlTableModel
.
To show the Database, I put that Model into a QTableView
.
Now I want to create a Method where the selected Rows (or the whole Line) will be copied into the QClipboard
. After that I want to insert it into my OpenOffice.Calc-Document.
But I have no Idea what to do with the Selected
SIGNAL and the QModelIndex
and how to put this into the Clipboard.
I had a similar problem and ended up adapting QTableWidget (which is an extension of QTableView) to add copy/paste functionality. Here is the code which builds on what was provided by quark above:
qtablewidgetwithcopypaste.h
qtablewidgetwithcopypaste.cpp
Here is a variation on what Corwin Joy posted that works with QTableView and handles sparse selections differently. With this code if you have different columns selected in different rows (e.g. selected cells are (1,1), (1, 2), (2, 1), (3,2)) then when you paste it you will get empty cells corresponding to the "holes" in your selection (e.g. cells (2,2) and (3,1)). It also pulls in the column header text for columns that intersect the selection.
For whatever reason I didn't have access to the std::sort function, however I did find that as a neat alternative to Corwin Joy's solution, the sort function can be implemented by replacing
with
This is the same as writing:
Thanks for your helpful code guys!
What you'll need to do is access the text data in the model, then pass that text to the
QClipboard
.To access the text data in the model, use
QModelIndex::data()
. The default argument isQt::DisplayRole
, i.e. the displayed text.Once you've retrieved the text, pass that text to the clipboard using
QClipboard::setText()
.a pyqt py2.x example:
Careful with the last element. Note below, indexes may become empty after 'removeFirst()'. Thus, 'current' is never valid and should not be used in model()->data(current).
Consider