LoginSignup
5
3

More than 5 years have passed since last update.

Qt QTableWidgetで行を入れ替える

Posted at

QTableWidgetで行と行を入れ替えたいと思ったのですが、APIが用意されていませんでした。順番の入れ替えや特定行をひとつ上に移動したい場合などけっこうありそうなのになぁ。簡単ですが対処法をメモで残しておきます。言語はC++です。

void exchangeTalbeRow(int row1, int row2)
{
        QTableWidgetItem *temp;
        for (int col = 0; col < ui.tableWidget->columnCount(); col++)
        {
                temp = ui.tableWidget->takeItem(row1, col);
                ui.tableWidget->setItem(row1, col, ui.tableWidget->takeItem(row2, col));
                ui.tableWidget->setItem(row2, col, temp);
        }
        ui.tableWidget->setCurrentCell(row2, ui.tableWidget->currentColumn());
}
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3