QTableView 的 selectedItems 中选定行/行的顺序

发布于 2024-11-05 03:23:47 字数 861 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朮生 2024-11-12 03:23:47

我写的这个算法应该可以解决问题:

QList<QTableWidgetItem *> selectedCells(TableView->selectedItems());

mCopyByteArray.clear();

QString text;
int row_count = TableView->rowCount();
int column_count = TableView->columnCount();

for( int i = 0; i < row_count; i++ )
{
    for( int j = 0; j < column_count; j++ )
    {
        text = selectedCells.at( i + j * row_count )->text();

        mCopyByteArray.append( text );
        mCopyByteArray.append( "\r\n" );
    }
}

This algorithm I wrote should do the trick:

QList<QTableWidgetItem *> selectedCells(TableView->selectedItems());

mCopyByteArray.clear();

QString text;
int row_count = TableView->rowCount();
int column_count = TableView->columnCount();

for( int i = 0; i < row_count; i++ )
{
    for( int j = 0; j < column_count; j++ )
    {
        text = selectedCells.at( i + j * row_count )->text();

        mCopyByteArray.append( text );
        mCopyByteArray.append( "\r\n" );
    }
}
格子衫的從容 2024-11-12 03:23:47

您可以使用QTableWidget::selectedRanges()来代替。小示例:

#include <QList>
#include <QTableWidget>
#include <QTableWidgetSelectionRange>

/...

// you can have more than one selected areas in the table. So you can have more then one
// selected ranges
QList <QTableWidgetSelectionRange*> selectRanges(TableView->selectedRanges());

for (int i =0; i != selectRanges.size(); ++i) {
  QTableWidgetSelectionRange range = selectRanges.at(i);
  int top = range.topRow();
  int bottom = range.bottomRow();
  for (int i = top; i <= bottom; ++i) {
    QTableWidgetItem *item1 = TableView->itemAt(i, 0); //first column item
    QTableWidgetItem *item2 = TableView->itemAt(i, 1); //second column item
    // do desired stuff
  }
}

注意:我不知道这种方法的性能问题。你可以检查一下。

You can use QTableWidget::selectedRanges() instead. Small Example:

#include <QList>
#include <QTableWidget>
#include <QTableWidgetSelectionRange>

/...

// you can have more than one selected areas in the table. So you can have more then one
// selected ranges
QList <QTableWidgetSelectionRange*> selectRanges(TableView->selectedRanges());

for (int i =0; i != selectRanges.size(); ++i) {
  QTableWidgetSelectionRange range = selectRanges.at(i);
  int top = range.topRow();
  int bottom = range.bottomRow();
  for (int i = top; i <= bottom; ++i) {
    QTableWidgetItem *item1 = TableView->itemAt(i, 0); //first column item
    QTableWidgetItem *item2 = TableView->itemAt(i, 1); //second column item
    // do desired stuff
  }
}

Note: I amn't aware of performance issues for this approach. You can check it.

生来就爱笑 2024-11-12 03:23:47

并不是真正的答案,而是我发现的更多信息:

似乎 selectedItems() 函数返回所选项目的顺序就是它们被选择的顺序。

此外,如果QTableWidgetselectionBehavior属性设置为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 the QTableWidget is set to SelectRows, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文