英文:
how to copy the QTableView header to the clipboard?
问题
是否有一种方法允许将QTableView
的表头复制到剪贴板?
我使用QTableView
和QAbstractTableModel
创建了一个表格。为了启用行和列的选择,我在我的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->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()); // Necessary, otherwise they are in column order
QString text;
int currentRow = 0; // To determine when to insert newlines
foreach (const QModelIndex& cell, cells) {
if (text.length() == 0) {
// First item
} else if (cell.row() != currentRow) {
// New row
text += '\n';
} else {
// Next cell
text += '\t';
}
currentRow = cell.row();
text += cell.data().toString();
}
QApplication::clipboard()->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 &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()->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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论