将上次的实现的表格添加新增按钮,并能产生新的数据显示在表格中。
通过布局使新数据在表格居中布局。
#-*- codingg:utf8 -*-
from main_serve import *
from PyQt5 import QtCore
from PyQt5.QtWidgets import QMainWindow,QWidget,QApplication,QTableWidgetItem,QCheckBox,QHBoxLayout
import sys
class MyWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MyWindow, self).__init__(parent)
self.setupUi(self)
self.row_count = 0;
def startHandle(self):
self.tableWidget.setRowCount(1)
# _translate = QtCore.QCoreApplication.translate
# item = self.tableWidget.item(0, 0)
# item.setText(_translate("MainWindow", "11"))
self.tableWidget.setItem(0, 0, QTableWidgetItem('11'))
print(111)
def addAccount(self):
widget = QWidget()
self.tableWidget.setRowCount(self.row_count+1)
# self.check = QtWidgets.QTableWidgetItem()
# self.check.setCheckState(QtCore.Qt.Unchecked)
# self.tableWidget.setItem(self.row_count, 0, self.check)
check=QCheckBox()
check.setCheckState(QtCore.Qt.Unchecked)
check.setStyleSheet(''' text-align : center;align:center; ''')
#居中布局
hLayout = QHBoxLayout()
hLayout.addStretch()#水平伸缩
hLayout.addWidget(check)
hLayout.addStretch() #水平伸缩
hLayout.setContentsMargins(5, 2, 5, 2)
widget.setLayout(hLayout)
self.tableWidget.setCellWidget(self.row_count, 0, widget)
item = QtWidgets.QTableWidgetItem('11')
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.tableWidget.setItem(self.row_count, 1, item)
self.row_count = self.row_count +1
if __name__=="__main__":
app = QApplication(sys.argv)
myWin = MyWindow()
myWin.show()
sys.exit(app.exec_())