大阪市中央区 システムソフトウェア開発会社

営業時間:平日09:15〜18:15
MENU

Qtプログラミング – スプレッドシート編集機能 – コピー、削除、切り取り続き

著者:津路高広
公開日:2019/12/23
最終更新日:2019/12/23
カテゴリー:技術情報

津路です。
今回は、前回実装した機能を利用します。
その前に、Spreadsheetクラスに、コピー、削除関数の宣言を、public slotsとして付け加えます。

class Spreadsheet : public QTableWidget
{
    Q_OBJECT
public:
    Spreadsheet(QWidget *parent = 0);
public slots:
    void cut();
    void copy();
    void del();
    void paste();
}

paste関数の内容です。
大まかには、現在選択中のセル数と、クリップボード上のセル数とを比較して、一致すれば貼り付けます。
前回同様、selectedRangeで選択中のセルを取得します。
各列は、タブ記号で区切られているものとします。

void Spreadsheet::paste()
{
    QTableWidgetSelectionRange range = selectedRange();
    QString str = QApplication::clipboard()->text();
    QStringList rows = str.split('\n');
    int numRows = rows.count();
    int numColumns = rows.first().count('\t') +1;
    if(range.rowCount() * range.columnCount() != 1
       && (range.rowCount() != numRows || range.columnCount() != numColumns)){
          QMessageBox::information(this,tr("SpreadSheet"), tr("The information cannot be pasted because the copy and paste areas aren't the same size."));
          return;
    }
    for(int i=0; i< numRows; i++){
       QStringList columns = rows[i].split('\t');
       for(int j=0; j<numColumns; j++){
           int row = range.topRow() + i;
           int column = range.leftColumn() + j;
           if(row < RowCount && column < ColumnCount)
              setFormula(row, column, columns[j]);
       }
    }
    somethingChanged();
}

MainWindowクラスから利用するための定義をします。

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();
protected:
    void closeEvent(QCloseEvent *event);

private slots:
    void cut();
private:
    QAction *deleteAction;
    QAction *cutAction;
    QAction *copyAction;
    QAction *deleteAction;
}
void MainWindow::cut()
{
    spreadsheet->cut();
}
void MainWindow::createActions()
{
    cutAction = new QAction(tr("&Cut"),this);
//    cutAction->setIcon(QIcon(":/images/cut.png"));
    cutAction->setShortcut(tr("Ctrl+X"));
    cutAction->setStatusTip(tr("cut the selected cell"));
    connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
    copyAction = new QAction(tr("&Copy"),this);
//    copyAction->setIcon(QIcon(":/images/copy.png"));
    copyAction->setShortcut(tr("Ctrl+C"));
    copyAction->setStatusTip(tr("copy the selected cell"));
    connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
    delAction = new QAction(tr("&Delete"),this);
//    delAction->setIcon(QIcon(":/images/delete.png"));
    delAction->setShortcut(tr("Del"));
    delAction->setStatusTip(tr("delete the selected cell"));
    connect(delAction, SIGNAL(triggered()), this, SLOT(del()));
    pasteAction = new QAction(tr("&Paste"),this);
//    pasteAction->setIcon(QIcon(":/images/paste.png"));
    pasteAction->setShortcut(tr("Ctrl+V"));
    pasteAction->setStatusTip(tr("paste the copied cell"));
    connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
}
void MainWindow::createMenus()
{
    editMenu = menuBar()->addMenu(tr("&Edit"));
    selectMenu = menuBar()->addMenu(tr("&Select"));
    editMenu->addSeparator();
    editMenu->addAction(cutAction);
    editMenu->addAction(copyAction);
    editMenu->addAction(pasteAction);
    editMenu->addAction(delAction);
}
    上に戻る