6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

QtAdvent Calendar 2021

Day 8

QTableWidget 使い方まとめ (PyQt5/Qt Creator C++)

Last updated at Posted at 2021-12-07

はじめに

pyqt5やQt CreatorでQtのtableについて色々と試した内容をメモ

PyQt5

1 シンプルに生成

QTableWidgetを生成し,サイズを4x3に変更するだけのシンプルなプログラム

table1-1.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class window(QWidget):
   def __init__(self, parent = None):
      super(window, self).__init__(parent)
      self.resize(400,400)
      self.setWindowTitle("PyQt5")

      self.table = QTableWidget(self)
      self.table.setRowCount(3)
      self.table.setColumnCount(4)

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Screenshot from 2021-12-07 16-38-52.png

2 tableに値を設定

setItemメソッドを使用QTableWidgetItem("aaa")みたいなクラスを入れれる.->"aaa"が入る

table1-2.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class window(QWidget):
   def __init__(self, parent = None):
      super(window, self).__init__(parent)
      self.resize(400,400)
      self.setWindowTitle("PyQt5")

      self.table = QTableWidget(self)
      self.table.setRowCount(3)
      self.table.setColumnCount(4)

      self.table.setItem(0,0,QTableWidgetItem("aaa"))

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Screenshot from 2021-12-07 20-23-28.png

3.データを設定

table2.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

table_data_ = {'1':['1','2','3','4','5','6','7','8','9'],
        '2':['1','2','3','4','5','6','7','8','9'],
        '3':['1','2','3','4','5','6','7','8','9'],
        '4':['1','2','3','4','5','6','7','8','9']}

class TableView(QTableWidget):
    def __init__(self, data, *args):
        QTableWidget.__init__(self, *args)
        self.data = table_data_
        self.setData()
        self.resizeColumnsToContents()
        self.resizeRowsToContents()
 
    def setData(self): 
        horHeaders = []
        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)
            for m, item in enumerate(self.data[key]):
                newitem = QTableWidgetItem(item)
                self.setItem(m, n, newitem)
        self.setHorizontalHeaderLabels(horHeaders)

class window(QWidget):
   def __init__(self, parent = None):
      super(window, self).__init__(parent)
      self.resize(400,400)
      self.setWindowTitle("PyQt5")

      self.table = TableView(table_data_, 9, 4,self)

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Screenshot from 2021-12-07 16-52-55.png

4.ボタンを押した時にtableの値を取得して表示

table3.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

table_data_ = {'1':['1','2','3','4','5','6','7','8','9'],
        '2':['1','2','3','4','5','6','7','8','9'],
        '3':['1','2','3','4','5','6','7','8','9'],
        '4':['1','2','3','4','5','6','7','8','9']}

class TableView(QTableWidget):
    def __init__(self, data, *args):
        QTableWidget.__init__(self, *args)
        self.data = table_data_
        self.setData()
        self.resizeColumnsToContents()
        self.resizeRowsToContents()
 
    def setData(self): 
        horHeaders = []
        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)
            for m, item in enumerate(self.data[key]):
                newitem = QTableWidgetItem(item)
                self.setItem(m, n, newitem)
        self.setHorizontalHeaderLabels(horHeaders)

class window(QWidget):
  def __init__(self, parent = None):
    super(window, self).__init__(parent)
    self.resize(400,400)
    self.setWindowTitle("PyQt5")

    self.table = TableView(table_data_, 9, 4, self)

    self.button = QPushButton('button', self)
    self.button.move(120,300)
    self.button.clicked.connect(self.print_table_val)

    
  def print_table_val(self):

    # get table value at(0,0)
    print(self.table.item(0,0).text())     

    # get table all value
    datas = []
    for i in range(4):
      row = []
      for j in range(9):
        row.append(self.table.item(j,i).text())
      datas.append(row)
    print(datas)

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Screenshot from 2021-12-07 17-31-08.png

5.tableの値を変える

table4.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

table_data_ = {'1':['1','2','3','4','5','6','7','8','9'],
        '2':['1','2','3','4','5','6','7','8','9'],
        '3':['1','2','3','4','5','6','7','8','9'],
        '4':['1','2','3','4','5','6','7','8','9']}

class TableView(QTableWidget):
    def __init__(self, data, *args):
        QTableWidget.__init__(self, *args)
        self.data = table_data_
        self.setData()
        self.resizeColumnsToContents()
        self.resizeRowsToContents()
 
    def setData(self): 
        horHeaders = []
        for n, key in enumerate(sorted(self.data.keys())):
            horHeaders.append(key)
            for m, item in enumerate(self.data[key]):
                newitem = QTableWidgetItem(item)
                self.setItem(m, n, newitem)
        self.setHorizontalHeaderLabels(horHeaders)

class window(QWidget):
  def __init__(self, parent = None):
    super(window, self).__init__(parent)
    self.resize(400,400)
    self.setWindowTitle("PyQt5")

    self.table = TableView(table_data_, 9, 4, self)

    self.button = QPushButton('button', self)
    self.button.move(120,300)
    self.button.clicked.connect(self.change_table_val)
    
  def change_table_val(self):
    table_data_['1'][0] = '33'
    self.table.setData() 

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

ボタンを押すと,tableの(0,0)の値が33に変わります

Screenshot from 2021-12-07 17-40-15.png

6.Qt Designerで使う場合の要点

1.デザイン上の注意点

デザイン上綺麗にitemだけ表示される様にするためには写真の様に
テーブルのQWidgetのsizePolicyPreferred + QAbstractScrollAreaAdjustToContentsにします。
左上に配置したい場合はバネを配置します。

Screenshot from 2021-12-07 19-30-57.png

Peek 2021-12-07 19-30.gif

7.tableにwidgetを埋め込む

table7.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class LoadTable(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(LoadTable, self).__init__(1, 5, parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))   
        headertitle = ("A","B","C","D","E")
        self.setHorizontalHeaderLabels(headertitle)
        self.verticalHeader().hide()
        self.horizontalHeader().setHighlightSections(False)
        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)

        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
        self.setColumnWidth(0, 130)

        combox_lay = QtWidgets.QComboBox(self)
        combox_lay.addItems(["I","II"])
        self.setCellWidget(0, 4, combox_lay)

        self.cellChanged.connect(self._cellclicked)

    @QtCore.pyqtSlot(int, int)
    def _cellclicked(self, r, c):
        it = self.item(r, c)
        it.setTextAlignment(QtCore.Qt.AlignCenter)        

    @QtCore.pyqtSlot()
    def _addrow(self):
        rowcount = self.rowCount()
        self.insertRow(rowcount)
        combox_add = QtWidgets.QComboBox(self)
        combox_add.addItems(["I","II"])
        self.setCellWidget(rowcount, 4, combox_add)

    @QtCore.pyqtSlot()
    def _removerow(self):
        if self.rowCount() > 0:
            self.removeRow(self.rowCount()-1)


class ThirdTabLoads(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ThirdTabLoads, self).__init__(parent)    

        table = LoadTable()

        add_button = QtWidgets.QPushButton("Add")
        add_button.clicked.connect(table._addrow)

        delete_button = QtWidgets.QPushButton("Delete")
        delete_button.clicked.connect(table._removerow)

        button_layout = QtWidgets.QVBoxLayout()
        button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
        button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)


        tablehbox = QtWidgets.QHBoxLayout()
        tablehbox.setContentsMargins(10, 10, 10, 10)
        tablehbox.addWidget(table)

        grid = QtWidgets.QGridLayout(self)
        grid.addLayout(button_layout, 0, 1)
        grid.addLayout(tablehbox, 0, 0)        


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = ThirdTabLoads()
    w.show()
    sys.exit(app.exec_())

Screenshot from 2021-12-09 12-35-33.png

8.ボタン埋め込み

table8.py
import sys
from PyQt5 import QtCore, QtGui, QtWidgets


class LoadTable(QtWidgets.QTableWidget):
    def __init__(self, parent=None):
        super(LoadTable, self).__init__(1, 5, parent)
        self.setFont(QtGui.QFont("Helvetica", 10, QtGui.QFont.Normal, italic=False))   
        headertitle = ("A","B","C","D","E")
        self.setHorizontalHeaderLabels(headertitle)
        self.verticalHeader().hide()
        self.horizontalHeader().setHighlightSections(False)
        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Fixed)

        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
        self.setColumnWidth(0, 130)

        #combox_lay = QtWidgets.QComboBox(self)
        #combox_lay.addItems(["I","II"])
        button = QtWidgets.QPushButton(self)
        button.setText('Click!')
        self.setCellWidget(0, 4, button)

        self.cellChanged.connect(self._cellclicked)

    @QtCore.pyqtSlot(int, int)
    def _cellclicked(self, r, c):
        it = self.item(r, c)
        it.setTextAlignment(QtCore.Qt.AlignCenter)        

    @QtCore.pyqtSlot()
    def _addrow(self):
        rowcount = self.rowCount()
        self.insertRow(rowcount)
        button = QtWidgets.QPushButton(self)
        button.setText('Click!')
        self.setCellWidget(rowcount, 4, button)

    @QtCore.pyqtSlot()
    def _removerow(self):
        if self.rowCount() > 0:
            self.removeRow(self.rowCount()-1)


class ThirdTabLoads(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ThirdTabLoads, self).__init__(parent)    

        table = LoadTable()

        add_button = QtWidgets.QPushButton("Add")
        add_button.clicked.connect(table._addrow)

        delete_button = QtWidgets.QPushButton("Delete")
        delete_button.clicked.connect(table._removerow)

        button_layout = QtWidgets.QVBoxLayout()
        button_layout.addWidget(add_button, alignment=QtCore.Qt.AlignBottom)
        button_layout.addWidget(delete_button, alignment=QtCore.Qt.AlignTop)


        tablehbox = QtWidgets.QHBoxLayout()
        tablehbox.setContentsMargins(10, 10, 10, 10)
        tablehbox.addWidget(table)

        grid = QtWidgets.QGridLayout(self)
        grid.addLayout(button_layout, 0, 1)
        grid.addLayout(tablehbox, 0, 0)        


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = ThirdTabLoads()
    w.show()
    sys.exit(app.exec_())

Screenshot from 2021-12-09 12-48-55.png

ボタン埋め込みシンプルver

table9.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class window(QWidget):
   def __init__(self, parent = None):
      super(window, self).__init__(parent)
      self.resize(400,400)
      self.setWindowTitle("PyQt5")

      self.table = QTableWidget(self)
      self.table.setRowCount(3)
      self.table.setColumnCount(4)

      button = QPushButton(self)
      button.setText('Click!')
      self.table.setCellWidget(0, 0, button)

def main():
   app = QApplication(sys.argv)
   ex = window()
   ex.show()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Screenshot from 2021-12-09 12-52-54.png

2.python表示

  • コード生成
pyuic5 -o table_ui.py table.ui
  • 生成したpythonコードを読み込んで表示するプログラム
table1.py
import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow

from table_ui import Ui_MainWindow


class Gui(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Gui, self).__init__(parent)
        self.setupUi(self)

if __name__ == '__main__':
    argvs = sys.argv
    app = QApplication(argvs)
    gui = Gui()
    gui.show()
    sys.exit(app.exec_())

Peek 2021-12-07 19-52.gif

  • テーブルの値を変えてみる
table2.py
import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem

from table_ui import Ui_MainWindow


class Gui(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Gui, self).__init__(parent)
        self.setupUi(self)
        self.tableWidget.setItem(0,0,QTableWidgetItem("aaa"))

if __name__ == '__main__':
    argvs = sys.argv
    app = QApplication(argvs)
    gui = Gui()
    gui.show()
    sys.exit(app.exec_())

Screenshot from 2021-12-07 20-28-31.png

Qt Creator

1.値を入れる

1.UIはデザイナーのときと同じ様に以下のように設定.

Screenshot from 2021-12-07 22-05-28.png

2.mainWindow.cppに以下の2行を追加し,テーブルに値をいれます

    QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(333));
    ui->tableWidget->setItem(0,0,newItem);

追加後は以下のようになります

mainWindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(333));
    ui->tableWidget->setItem(0,0,newItem);
}

MainWindow::~MainWindow()
{
    delete ui;
}

Screenshot from 2021-12-07 22-07-36.png

2.テーブルにwidgetを入れる

まず,UIでテーブルとボタンを配置

Screenshot from 2021-12-09 13-24-34.png

mainwindow.cpp内で以下の様に記載すると,tableの(0,0)位置にpushbuttonが埋め込まれる.

ui->tableWidget->setCellWidget(0,0,ui->pushButton);

Screenshot from 2021-12-09 13-24-30.png

mainwindow.cpp
#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->tableWidget->setCellWidget(0,0,ui->pushButton);
}

MainWindow::~MainWindow()
{
    delete ui;
}

最後に

色々と理解していって,速くまとまったアプリケーションを作成できたらいいなと思います。

参考

uiから作成したプログラムに追加して,ボタンクリックでラベル文字の変更するように

6
1
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
6
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?