开发者

order of selected rows/lines in QTableView's selectedItems

开发者 https://www.devze.com 2023-03-02 12:23 出处:网络
I am using a QTableWidget and want to copy some cells to clipboard. It seems the QTableWidget only supports the selectedItems method.

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<QTable开发者_Python百科WidgetItem *> 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!


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" );
    }
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号