So far I tried this.
I want to use ctrl-c and ctrl-v when I have selected a column or two.
Thanks for your help,
Arend
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QStandardItemModel"
#include "QAbstractItemModel"
#include "QItemSelectionModel"
#include "QKeyEvent"
#include "QKeySequence"
#include "QClipboard"
{
ui->setupUi(this);
model->setItem(0,0,firstRow);
model->setItem(1,0,secondRow);
ui->tableView->setModel(model);
QModelIndexList list = model->selectionIndexes();
qSort(list);
for(int i = 0; i < list.size(); i++)
{
QString text
= abmodel
->data
(index
).
toString();
copy_table.append(text);
if(index.row() != previous.row())
{
copy_table.append('\n');
}
else
{
copy_table.append('\t');
}
previous = index;
}
clipboard->setText(copy_table);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QStandardItemModel"
#include "QAbstractItemModel"
#include "QItemSelectionModel"
#include "QKeyEvent"
#include "QKeySequence"
#include "QClipboard"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
QStandardItemModel *model = new QStandardItemModel(3,3,this);
model->setHorizontalHeaderItem(0, new QStandardItem(QString("Column1 Header")));
model->setHorizontalHeaderItem(1, new QStandardItem(QString("Column2 Header")));
QStandardItem *firstRow = new QStandardItem(QString("1"));
QStandardItem *secondRow = new QStandardItem(QString("2"));
model->setItem(0,0,firstRow);
model->setItem(1,0,secondRow);
ui->tableView->setModel(model);
QAbstractItemModel *abmodel = ui.tableview->model();
QItemSelectionModel *model = ui.tableview->selectionModel();
QModelIndexList list = model->selectionIndexes();
qSort(list);
QModelIndex index = list.first();
for(int i = 0; i < list.size(); i++)
{
QModelIndex index = list.at(i);
QString text = abmodel->data(index).toString();
copy_table.append(text);
if(index.row() != previous.row())
{
copy_table.append('\n');
}
else
{
copy_table.append('\t');
}
previous = index;
}
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(copy_table);
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
Bookmarks