Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
11
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@MOKYN

QTableWidgetでExcelとコピペできるようにする

残念ながらQTableWidgetはデフォルトでコピー、ペーストができないので実装する必要があります。また、どうせならExcelと連携できると便利です。とりあえずの実装ですがメモがてら共有しときます。

環境:Windows、Qt4.8.6、C++

// 初期化処理時にショートカットを登録
{
        QShortcut *scCtrlC = new QShortcut(QKeySequence("Ctrl+C"), ui.tableWidget);
        connect(scCtrlC, SIGNAL(activated()), this, SLOT(copyCell()));
        QShortcut *scCtrlV = new QShortcut(QKeySequence("Ctrl+V"), ui.tableWidget);
        connect(scCtrlV, SIGNAL(activated()), this, SLOT(pasteCell()));
}
// 選択したセルをコピーする
void mainwindow::copyCell()
{
        QAbstractItemModel* model = ui.tableWidget->model();
        QItemSelectionModel* selection = ui.tableWidget->selectionModel();
        QModelIndexList indexes = selection->selectedIndexes();

        if (indexes.count() == 0) {
                return;
        }

        qSort(indexes); // デフォルトだと列ソートのため行でソートする

        QString clip;
        QModelIndex previous = indexes.first();
        indexes.removeFirst();
        clip.append(model->data(previous).toString());

        for each(QModelIndex current in indexes.toVector())
        {
                if (current.row() != previous.row())
                {
                        clip.append("\n");
                }
                else
                {
                        clip.append("\t");
                }

                clip.append(model->data(current).toString());
                previous = current;
        }

        QApplication::clipboard()->setText(clip);
}

// クリップボードの内容をセルを貼り付ける
void mainwindow::pasteCell()
{
        if (ui.tableWidget->selectedItems().size() != 1)
        {
                return;
        }
        QString clip = QApplication::clipboard()->text();
        QStringList rowList = clip.split("\n");

        QTableWidgetItem *currentItem = ui.tableWidget->selectedItems().first();
        int pasteRow = currentItem->row();
        int pasteCol = currentItem->column();
        for each(QString row in rowList.toVector())
        {
                QStringList colList = row.split("\t");
                for each (QString cell in colList.toVector())
                {
                        table->item(pasteRow, pasteCol)->setText(cell);
                        pasteCol++;
                }
                pasteCol = currentItem->column();
                pasteRow++;
        }
}

参考
http://stackoverflow.com/questions/1230222/selected-rows-line-in-qtableview-copy-to-qclipboard

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
11
Help us understand the problem. What are the problem?