Ber*_*chi 15 c++ clipboard qt qt4 qtableview
我有一个SQLite-Database,我把它做成了一个QSqlTableModel.为了显示数据库,我将该模型放入了QTableView.
现在我想创建一个方法,将选定的行(或整行)复制到QClipboard.之后我想将它插入我的OpenOffice.Calc-Document.
但我不知道如何处理SelectedSIGNAL QModelIndex以及如何将其放入剪贴板.
qua*_*ark 26
要实际捕获选择,请使用项目视图的选择模型来获取索引列表.鉴于您有一个QTableView *被叫,view您可以通过这种方式获得选择:
QAbstractItemModel * model = view->model();
QItemSelectionModel * selection = view->selectionModel();
QModelIndexList indexes = selection->selectedIndexes();
Run Code Online (Sandbox Code Playgroud)
然后遍历索引列表,调用model->data(index)每个索引.如果数据尚未将数据转换为字符串,则将每个字符串连接在一起.然后,您可以使用QClipboard.setText将结果粘贴到剪贴板.请注意,对于Excel和Calc,每个列通过换行符("\n")与下一列分隔,每行由制表符("\ t")分隔.您必须检查索引以确定何时移动到下一行.
QString selected_text;
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
indexes.removeFirst();
foreach(const QModelIndex ¤t, indexes)
{
QVariant data = model->data(current);
QString text = data.toString();
// At this point `text` contains the text in one cell
selected_text.append(text);
// If you are at the start of the row the row number of the previous index
// isn't the same. Text is followed by a row separator, which is a newline.
if (current.row() != previous.row())
{
selected_text.append('\n');
}
// Otherwise it's the same row, so append a column separator, which is a tab.
else
{
selected_text.append('\t');
}
previous = current;
}
QApplication.clipboard().setText(selected_text);
Run Code Online (Sandbox Code Playgroud)
警告:我没有机会尝试此代码,但PyQt等效工作.
Cor*_*Joy 13
我遇到了类似的问题,最终调整了QTableWidget(这是QTableView的扩展)来添加复制/粘贴功能.以下是基于夸克提供的代码:
qtablewidgetwithcopypaste.h
// QTableWidget with support for copy and paste added
// Here copy and paste can copy/paste the entire grid of cells
#ifndef QTABLEWIDGETWITHCOPYPASTE_H
#define QTABLEWIDGETWITHCOPYPASTE_H
#include <QTableWidget>
#include <QKeyEvent>
#include <QWidget>
class QTableWidgetWithCopyPaste : public QTableWidget
{
Q_OBJECT
public:
QTableWidgetWithCopyPaste(int rows, int columns, QWidget *parent = 0) :
QTableWidget(rows, columns, parent)
{}
QTableWidgetWithCopyPaste(QWidget *parent = 0) :
QTableWidget(parent)
{}
private:
void copy();
void paste();
public slots:
void keyPressEvent(QKeyEvent * event);
};
#endif // QTABLEWIDGETWITHCOPYPASTE_H
Run Code Online (Sandbox Code Playgroud)
qtablewidgetwithcopypaste.cpp
#include "qtablewidgetwithcopypaste.h"
#include <QApplication>
#include <QMessageBox>
#include <QClipboard>
#include <QMimeData>
void QTableWidgetWithCopyPaste::copy()
{
QItemSelectionModel * selection = selectionModel();
QModelIndexList indexes = selection->selectedIndexes();
if(indexes.size() < 1)
return;
// QModelIndex::operator < sorts first by row, then by column.
// this is what we need
// std::sort(indexes.begin(), indexes.end());
qSort(indexes);
// You need a pair of indexes to find the row changes
QModelIndex previous = indexes.first();
indexes.removeFirst();
QString selected_text_as_html;
QString selected_text;
selected_text_as_html.prepend("<html><style>br{mso-data-placement:same-cell;}</style><table><tr><td>");
QModelIndex current;
Q_FOREACH(current, indexes)
{
QVariant data = model()->data(previous);
QString text = data.toString();
selected_text.append(text);
text.replace("\n","<br>");
// At this point `text` contains the text in one cell
selected_text_as_html.append(text);
// If you are at the start of the row the row number of the previous index
// isn't the same. Text is followed by a row separator, which is a newline.
if (current.row() != previous.row())
{
selected_text_as_html.append("</td></tr><tr><td>");
selected_text.append(QLatin1Char('\n'));
}
// Otherwise it's the same row, so append a column separator, which is a tab.
else
{
selected_text_as_html.append("</td><td>");
selected_text.append(QLatin1Char('\t'));
}
previous = current;
}
// add last element
selected_text_as_html.append(model()->data(current).toString());
selected_text.append(model()->data(current).toString());
selected_text_as_html.append("</td></tr>");
QMimeData * md = new QMimeData;
md->setHtml(selected_text_as_html);
// qApp->clipboard()->setText(selected_text);
md->setText(selected_text);
qApp->clipboard()->setMimeData(md);
// selected_text.append(QLatin1Char('\n'));
// qApp->clipboard()->setText(selected_text);
}
void QTableWidgetWithCopyPaste::paste()
{
if(qApp->clipboard()->mimeData()->hasHtml())
{
// TODO, parse the html data
}
else
{
QString selected_text = qApp->clipboard()->text();
QStringList cells = selected_text.split(QRegExp(QLatin1String("\\n|\\t")));
while(!cells.empty() && cells.back().size() == 0)
{
cells.pop_back(); // strip empty trailing tokens
}
int rows = selected_text.count(QLatin1Char('\n'));
int cols = cells.size() / rows;
if(cells.size() % rows != 0)
{
// error, uneven number of columns, probably bad data
QMessageBox::critical(this, tr("Error"),
tr("Invalid clipboard data, unable to perform paste operation."));
return;
}
if(cols != columnCount())
{
// error, clipboard does not match current number of columns
QMessageBox::critical(this, tr("Error"),
tr("Invalid clipboard data, incorrect number of columns."));
return;
}
// don't clear the grid, we want to keep any existing headers
setRowCount(rows);
// setColumnCount(cols);
int cell = 0;
for(int row=0; row < rows; ++row)
{
for(int col=0; col < cols; ++col, ++cell)
{
QTableWidgetItem *newItem = new QTableWidgetItem(cells[cell]);
setItem(row, col, newItem);
}
}
}
}
void QTableWidgetWithCopyPaste::keyPressEvent(QKeyEvent * event)
{
if(event->matches(QKeySequence::Copy) )
{
copy();
}
else if(event->matches(QKeySequence::Paste) )
{
paste();
}
else
{
QTableWidget::keyPressEvent(event);
}
}
Run Code Online (Sandbox Code Playgroud)
Quark的答案(选定的答案)有助于指出人们正确的方向,但他的算法完全不正确.除了一个错误和错误的赋值,它甚至在语法上都不正确.下面是我刚编写和测试的工作版本.
我们假设我们的示例表如下所示:
A | B | C
D | E | F
Quark算法的问题如下:
如果我们用'|替换他的\ t分隔符 ',它会产生这个输出:
B | C | D
E | F |
关闭一个错误是D出现在第一行.错误的分配由A的遗漏证明
以下算法使用正确的语法纠正这两个问题.
QString clipboardString;
QModelIndexList selectedIndexes = view->selectionModel()->selectedIndexes();
for (int i = 0; i < selectedIndexes.count(); ++i)
{
QModelIndex current = selectedIndexes[i];
QString displayText = current.data(Qt::DisplayRole).toString();
// If there exists another column beyond this one.
if (i + 1 < selectedIndexes.count())
{
QModelIndex next = selectedIndexes[i+1];
// If the column is on different row, the clipboard should take note.
if (next.row() != current.row())
{
displayText.append("\n");
}
else
{
// Otherwise append a column separator.
displayText.append(" | ");
}
}
clipboardString.append(displayText);
}
QApplication::clipboard()->setText(clipboardString);
Run Code Online (Sandbox Code Playgroud)
我选择使用计数器而不是迭代器的原因只是因为通过检查计数来更容易测试是否存在另一个索引.使用迭代器,我想也许你可以只增加它并将其存储在一个弱指针中以测试它是否有效但只是像我上面那样使用一个计数器.
我们需要检查下一行是否会在新行上打开.如果我们在一个新行并且我们检查前一行是Quark的算法,那么它已经来不及追加.我们可以预先添加,但是我们必须跟踪最后一个字符串大小.上面的代码将从示例表中生成以下输出:
A | B | C
D | E | F
| 归档时间: |
|
| 查看次数: |
30070 次 |
| 最近记录: |