はじめに
pyqt5やQt CreatorでQtのtableについて色々と試した内容をメモ
PyQt5
1 シンプルに生成
QTableWidgetを生成し,サイズを4x3に変更するだけのシンプルなプログラム
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()
2 tableに値を設定
setItem
メソッドを使用QTableWidgetItem("aaa")
みたいなクラスを入れれる.->"aaa"が入る
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()
3.データを設定
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()
4.ボタンを押した時にtableの値を取得して表示
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()
5.tableの値を変える
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に変わります
6.Qt Designerで使う場合の要点
1.デザイン上の注意点
デザイン上綺麗にitemだけ表示される様にするためには写真の様に
テーブルのQWidgetのsizePolicy
をPreferred
+ QAbstractScrollArea
をAdjustToContents
にします。
左上に配置したい場合はバネを配置します。
7.tableにwidgetを埋め込む
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_())
8.ボタン埋め込み
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_())
ボタン埋め込みシンプルver
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()
2.python表示
- コード生成
pyuic5 -o table_ui.py table.ui
- 生成したpythonコードを読み込んで表示するプログラム
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_())
- テーブルの値を変えてみる
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_())
Qt Creator
1.値を入れる
1.UIはデザイナーのときと同じ様に以下のように設定.
2.mainWindow.cpp
に以下の2行を追加し,テーブルに値をいれます
QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(333));
ui->tableWidget->setItem(0,0,newItem);
追加後は以下のようになります
#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;
}
2.テーブルにwidgetを入れる
まず,UIでテーブルとボタンを配置
mainwindow.cpp内で以下の様に記載すると,tableの(0,0)位置にpushbuttonが埋め込まれる.
ui->tableWidget->setCellWidget(0,0,ui->pushButton);
#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から作成したプログラムに追加して,ボタンクリックでラベル文字の変更するように