如何将QTableView的表头复制到剪贴板?

huangapple go评论64阅读模式
英文:

how to copy the QTableView header to the clipboard?

问题

是否有一种方法允许将QTableView的表头复制到剪贴板?

我使用QTableViewQAbstractTableModel创建了一个表格。为了启用行和列的选择,我在我的QTableView构造函数中添加了以下代码:

setSelectionMode(QAbstractItemView::ExtendedSelection);

为了将数据表格复制到剪贴板,我在一个继承自QTableView的类中实现了以下函数:

bool MyTableView::eventFilter(QObject * obj, QEvent * event) {
    if (event->type() == QEvent::KeyPress ) {
        auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
        if (keyEvent->key() == Qt::Key_C && (keyEvent->modifiers() == Qt::ControlModifier)) {
            QModelIndexList cells = selectedIndexes();
            std::sort(cells.begin(), cells.end()); // 必要,否则它们将按列顺序排列

            QString text;
            int currentRow = 0; // 用于确定何时插入换行符
            foreach (const QModelIndex& cell, cells) {
                if (text.length() == 0) {
                    // 第一个项目
                } else if (cell.row() != currentRow) {
                    // 新行
                    text += '\n';
                } else {
                    // 下一个单元格
                    text += '\t';
                }
                currentRow = cell.row();
                text += cell.data().toString();
            }
            QApplication::clipboard()->setText(text);
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

上述函数允许我复制表格的任何单元格/行,甚至列。唯一的问题是我无法复制表头到剪贴板。是否有一种方法可以使表头可选择并可复制?

MyTableModel类中,我实现了flags函数来选择表格项。

Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const {
    return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
}

有什么建议或想法吗?

英文:

Is there a way to allow the header of a QTableView to be copied to the clipboard?

I have a table created with QTableView and QAbstractTableModel. To enable rowd and columns selection, I add this line to the constructor of my QTableView

setSelectionMode(QAbstractItemView::ExtendedSelection);

And to copy the data table to the clipboard I implemented the following function in a class that inherits from QTableView:

bool MyTableView::eventFilter(QObject * obj, QEvent * event) {
    if(event-&gt;type() == QEvent::KeyPress ) {
        auto* keyEvent = dynamic_cast&lt;QKeyEvent*&gt;(event);
        if (keyEvent-&gt;key() == Qt::Key_C &amp;&amp; (keyEvent-&gt;modifiers() == Qt::ControlModifier)) {
            QModelIndexList cells = selectedIndexes();
            std::sort(cells.begin(), cells.end()); // Necessary, otherwise they are in column order

            QString text;
            int currentRow = 0; // To determine when to insert newlines
            foreach (const QModelIndex&amp; cell, cells) {
                if (text.length() == 0) {
                    // First item
                } else if (cell.row() != currentRow) {
                    // New row
                    text += &#39;\n&#39;;
                } else {
                    // Next cell
                    text += &#39;\t&#39;;
                }
                currentRow = cell.row();
                text += cell.data().toString();
            }
            QApplication::clipboard()-&gt;setText(text);
            return true;
        }
    }
    return QObject::eventFilter(obj, event);
}

The above function allows me to copy any cell/row and even column of a table. The only problem is that I can't copy a table header to the clipboard. Is there a way to make the header selectable and copyable?

In MyTableModel class I implement the flags function to select the table items.

Qt::ItemFlags MyTableModel::flags(const QModelIndex &amp;index) const {
    return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
}

Any suggestion or idea?

答案1

得分: 1

你可以使用model()->headerData(column, Qt::Horizontal).toString()来获取标题数据,只需在迭代数据之前从selectedIndexes中收集列索引的列表,然后对每个项目调用它,并将结果追加到text中。

要使标题可选择,你需要覆盖默认的QHeaderView行为,默认情况下点击标题部分会选择整行或整列。

英文:

You can use model()-&gt;headerData(column, Qt::Horizontal).toString() to get header data, just collect list of column indexes from selectedIndexes before itarating over data and call it for each item, appending result to text.

To make header selectable you need to override default QHeaderView behaviour, by default clicking on header section makes whole row or column selected.

huangapple
  • 本文由 发表于 2023年6月2日 04:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定