QTableView中选定的行,复制到QClipboard
我有一个 SQLite 数据库,并将其放入 QSqlTableModel
中。 为了显示数据库,我将该模型放入 QTableView
中。
现在我想创建一个方法,将选定的行(或整行)复制到 QClipboard
中。 之后我想将其插入到我的 OpenOffice.Calc-Document 中。
但我不知道如何处理 Selected
SIGNAL 和 QModelIndex
以及如何将其放入剪贴板。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
要实际捕获选择,您可以使用项目视图的 选择模型 来获取索引列表。 假设您有一个名为
view
的QTableView *
,您可以通过以下方式进行选择:然后循环遍历索引列表,调用
model->data(index)。 将数据转换为字符串(如果尚未转换)并将每个字符串连接在一起。 然后您可以使用
QClipboard.setText
将结果粘贴到剪贴板。 请注意,对于 Excel 和 Calc,每列与下一列之间用换行符 ("\n") 分隔,每行由制表符 ("\t") 分隔。 您必须检查索引以确定何时移动到下一行。警告:我还没有机会尝试这段代码,但 PyQt 的等效代码可以工作。
To actually capture the selection you use the item view's selection model to get a list of indices. Given that you have a
QTableView *
calledview
you get the selection this way:Then loop through the index list calling
model->data(index)
on each index. Convert the data to a string if it isn't already and concatenate each string together. Then you can useQClipboard.setText
to paste the result to the clipboard. Note that, for Excel and Calc, each column is separated from the next by a newline ("\n") and each row is separated by a tab ("\t"). You have to check the indices to determine when you move to the next row.Warning: I have not had a chance to try this code, but a PyQt equivalent works.
我遇到了类似的问题,最终调整了 QTableWidget (它是 QTableView 的扩展)来添加复制/粘贴功能。 以下是基于上面 quark 提供的代码构建的代码:
qtablewidgetwithcopypaste.h
qtablewidgetwithcopypaste.cpp
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
夸克的答案(选定的答案)很有利于为人们指明正确的方向,但他的算法完全不正确。 除了一处错误和不正确的赋值之外,它甚至在语法上也不正确。 下面是我刚刚编写和测试的工作版本。
假设我们的示例表如下所示:
A | 乙| C
d | 电子| F
夸克算法的问题如下:
如果我们将他的\t分隔符替换为'| ',它将产生以下输出:
B | C | D
电子| F |
一个错误是 D 出现在第一行。 省略 A 可以证明分配不正确。
下面的算法用正确的语法纠正了这两个问题。
我选择使用计数器而不是迭代器的原因只是因为通过检查计数来测试是否存在另一个索引更容易。 使用迭代器,我想也许您可以增加它并将其存储在弱指针中以测试它是否有效,但只需像我上面那样使用计数器即可。
我们需要检查下行是否会出现在新行上。 如果我们在一个新行上,并且像夸克算法那样检查前一行,那么追加就已经太晚了。 我们可以添加前缀,但随后我们必须跟踪最后一个字符串的大小。 上述代码将从示例表中生成以下输出:
A | 乙| C
D | 电子| F
Quark's answer (the selected one) is good for pointing people in the right direction, but his algorithm is entirely incorrect. In addition to an off by one error and incorrect assignment, its not even syntactically correct. Below is a working version that I just wrote and tested.
Let's assume our example table looks like so:
A | B | C
D | E | F
The problem with Quark's algorithm is the following:
If we replace his \t separator with a ' | ', it will produce this output:
B | C | D
E | F |
The off by one error is that D appears in the first row. The incorrect assignment is evidenced by the omission of A
The following algorithm corrects these two problems with correct syntax.
The reason I chose to use a counter instead of an iterator is just because it is easier to test if there exists another index by checking against the count. With an iterator, I suppose maybe you could just increment it and store it in a weak pointer to test if it is valid but just use a counter like I did above.
We need to check if the next line will be on on a new row. If we are on a new row and we check the previous row as Quark's algorithm does, its already too late to append. We could prepend, but then we have to keep track of the last string size. The above code will produce the following output from the example table:
A | B | C
D | E | F
无论出于何种原因,我无法访问 std::sort 函数,但是我确实发现,作为 Corwin Joy 解决方案的一个巧妙替代方案,排序函数可以通过替换来实现
这与
编写相同:
感谢您的有用的代码伙计们!
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!
我根据其他人的一些答案编写了一些代码。 我对 QTableWidget 进行了子类化并重写了 keyPressEvent() ,以允许用户通过键入 Control-C 将所选行复制到剪贴板。
输出示例(制表符分隔):
I wrote some code based on some of the others' answers. I subclassed
QTableWidget
and overrodekeyPressEvent()
to allow the user to copy the selected rows to the clipboard by typing Control-C.Output example (tab-separated):
您需要做的是访问模型中的文本数据,然后将该文本传递到
QClipboard
。要访问模型中的文本数据,请使用
QModelIndex::data()< /代码>
。 默认参数是
Qt::DisplayRole
,即显示的文本。检索到文本后,使用
QClipboard 将该文本传递到剪贴板: :setText()
。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()
.pyqt py2.x 示例:
a pyqt py2.x example:
我终于明白了,谢谢。
和
I finally got it, thanks.
and
我不禁注意到您可以使用
foreach( )
构造和QStringList
类,它有一个方便的join()
函数。I can't help but notice that you can simplify your code using a
foreach()
construct and theQStringList
class, which has a convenientjoin()
function.小心最后一个元素。 请注意,在“removeFirst()”之后索引可能会变空。 因此,“current”永远无效,不应该在 model()->data(current) 中使用。
考虑
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
这是 Corwin Joy 发布的内容的变体,它与 QTableView 一起使用并以不同的方式处理稀疏选择。 使用此代码,如果您在不同的行中选择了不同的列(例如选定的单元格为(1,1)、(1, 2)、(2, 1)、(3,2)),那么当您粘贴它时,您将得到空与您选择的“孔”相对应的单元格(例如单元格(2,2)和(3,1))。 它还会拉入与所选内容相交的列的列标题文本。
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.
如果有人感兴趣,这个网页提供了一个关于这个主题的工作代码项目,它运行得很好。
QAbstractTableModel / QTableView 的复制/粘贴功能实现
If anybody is interested, this web page provide a working code project on this topic, it's working pretty well.
Copy / paste functionality implementation for QAbstractTableModel / QTableView