QTableView 的 selectedItems 中选定行/行的顺序
我正在使用 QTableWidget 并希望将一些单元格复制到剪贴板。看来 QTableWidget 仅支持 selectedItems 方法。 由于某种原因,我将输出作为第一列,然后作为第二列。不是:第一行然后第二行。这使得分离列/行变得困难。你知道出了什么问题吗?谢谢!
QList<QTableWidgetItem *> selectedCells(TableView->selectedItems());
QTableWidgetItem * item;
mCopyByteArray.clear();
foreach(item, selectedCells)
{
mCopyByteArray.append(item->text());
mCopyByteArray.append("\r\n");
}
构建它时:
TableView = new QTableWidget(); /* I know that name somehow is wrong ;) */
TableView->setColumnCount(2);
QStringList HHeaderList;
HHeaderList << "Computer name" << "ServiceTag";
TableView->setHorizontalHeaderLabels(HHeaderList);
TableView->verticalHeader()->setVisible(false);
TableView->setEditTriggers(QTableWidget::NoEditTriggers);
有什么想法吗?谢谢你!
I am using a QTableWidget and want to copy some cells to clipboard. It seems the QTableWidget only supports the selectedItems method.
For some reason I get the output as first column and then second column. Not: first row and then second row. This makes it somehow difficult to seperate the cols/rows. Do you know what went wrong? Thanks!
QList<QTableWidgetItem *> selectedCells(TableView->selectedItems());
QTableWidgetItem * item;
mCopyByteArray.clear();
foreach(item, selectedCells)
{
mCopyByteArray.append(item->text());
mCopyByteArray.append("\r\n");
}
When building it up:
TableView = new QTableWidget(); /* I know that name somehow is wrong ;) */
TableView->setColumnCount(2);
QStringList HHeaderList;
HHeaderList << "Computer name" << "ServiceTag";
TableView->setHorizontalHeaderLabels(HHeaderList);
TableView->verticalHeader()->setVisible(false);
TableView->setEditTriggers(QTableWidget::NoEditTriggers);
Any ideas? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我写的这个算法应该可以解决问题:
This algorithm I wrote should do the trick:
您可以使用
QTableWidget::selectedRanges()
来代替。小示例:注意:我不知道这种方法的性能问题。你可以检查一下。
You can use
QTableWidget::selectedRanges()
instead. Small Example:Note: I amn't aware of performance issues for this approach. You can check it.
并不是真正的答案,而是我发现的更多信息:
似乎
selectedItems()
函数返回所选项目的顺序就是它们被选择的顺序。此外,如果
QTableWidget
的selectionBehavior
属性设置为SelectRows
,则所选项目将按照选择行的顺序返回。例如,对于 2x3 表,行编号为“A”、“B”,列编号为“1”、“2”、“3”:如果选择 B2,然后选择 A1,则所选项目为返回为:B1、B2、B3、A1、A2、A3。Not really an answer, but some more information that I found out:
It seems that the order in which the selected items are returned by the
selectedItems()
function is the order in which they were selected.Moreover, if the
selectionBehavior
property of theQTableWidget
is set toSelectRows
, then the selected items are returned in the order in which the rows were selected. For example, for a 2x3 table, where the rows are numbered 'A', 'B' and the columns are numbered '1', '2', '3': if you select B2 and then A1, then the selected items are returned as: B1,B2,B3,A1,A2,A3.