我想制作一个类似于 Excel
的电子表格。
最重要的功能之一是连接单元和 split 单元。
我编写了一个示例代码。
首先,我水平连接单元格。 (请水平选择关节单元) 然后,我将上面的单元格连接起来。 (请选择关节细胞) 请选择相同的单元格范围。 奇怪的事情发生了。
一列单元格按原样连接,但其他列则分开。 并且,我将一个单元格插入单元格中,所有连接的单元格都被选中。
这是一个错误吗?
from PySide2 import QtWidgets
from PySide2 import QtCore
from PySide2 import QtGui
import PySide2
import os
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
import sys
import itertools
alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
alphabet2 =[i[0]+i[1] for i in itertools.product(alphabet, alphabet)]
alphabet3 =[i[0]+i[1] for i in itertools.product(alphabet, alphabet2)]
alphabet = alphabet + alphabet2 + alphabet3
ROW_MAX = 1048576
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=None)
menuBar = self.menuBar()
joint_cell_menu = QtWidgets.QMenu("Joint Cells")
joint_cell_and_central_alignment_action = QtWidgets.QAction("Joint Cells and align center", joint_cell_menu)
self.connect(joint_cell_and_central_alignment_action, QtCore.SIGNAL("triggered(bool)"), self.joint_cell_and_central_alignment)
joint_cell_action = QtWidgets.QAction("Joint Cells", joint_cell_menu)
self.connect(joint_cell_action, QtCore.SIGNAL("triggered(bool)"), self.joint_cell)
split_cell_action = QtWidgets.QAction("Split cells", joint_cell_menu)
self.connect(split_cell_action, QtCore.SIGNAL("triggered(bool)"), self.split_cell)
horizontal_joint_cell_action = QtWidgets.QAction("Joint Cells Horizontally", joint_cell_menu)
self.connect(horizontal_joint_cell_action, QtCore.SIGNAL("triggered(bool)"), self.horizontal_joint_cell_action)
joint_cell_menu.addAction(joint_cell_and_central_alignment_action)
joint_cell_menu.addAction(joint_cell_action)
joint_cell_menu.addAction(horizontal_joint_cell_action)
joint_cell_menu.addAction(split_cell_action)
menuBar.addMenu(joint_cell_menu)
self.view = View()
self.setCentralWidget(self.view)
def joint_cell_and_central_alignment(self):
indexes = self.view.excel.selectedIndexes()
first_index = indexes[0]
last_index = indexes[-1]
first_row = first_index.row()
first_column = first_index.column()
last_row = last_index.row()
last_column = last_index.column()
self.view.excel.setSpan(first_row, first_column, last_row-first_row+1, last_column-first_column+1)
item = self.view.excel.item (first_row, first_column)
if item is not None:
item.setData(QtCore.Qt.TextAlignmentRole, QtCore.Qt.AlignCenter)
def joint_cell(self):
indexes = self.view.excel.selectedIndexes()
first_index = indexes[0]
last_index = indexes[-1]
first_row = first_index.row()
first_column = first_index.column()
last_row = last_index.row()
last_column = last_index.column()
self.view.excel.setSpan(first_row, first_column, last_row-first_row+1, last_column-first_column+1)
def split_cell(self):
indexes = self.view.excel.selectedIndexes()
first_index = indexes[0]
last_index = indexes[-1]
first_row = first_index.row()
first_column = first_index.column()
last_row = last_index.row()
last_column = last_index.column()
self.view.excel.setSpan(first_row, first_column, last_row-first_row+1, last_column-first_column+1)
def horizontal_joint_cell_action(self):
indexes = self.view.excel.selectedIndexes()
first_index = indexes[0]
last_index = indexes[-1]
first_row = first_index.row()
first_column = first_index.column()
last_row = last_index.row()
last_column = last_index.column()
columns = last_column-first_column+1
for r in range(first_row, last_row+1, 1):
self.view.excel.setSpan(r, first_column, 1, columns)
class View(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(View, self).__init__(parent=None)
desktop = QtWidgets.QDesktopWidget()
self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setWindowTitle("Excel Imitation")
self.scene = Scene()
self.excel = Excel(7000, 18278)
self.setScene(self.scene)
p = self.scene.addWidget(self.excel)
self.scene.setSceneRect(p.sceneBoundingRect().x(), p.sceneBoundingRect().y(), desktop.width(), desktop.height()+100)
class Scene(QtWidgets.QGraphicsScene):
def __init__(self, parent=None):
super(Scene, self).__init__(parent=None)
class Excel(QtWidgets.QTableWidget):
def __init__(self, rows, columns, parent=None):
super(Excel, self).__init__(parent=None)
self.setParent(parent)
desktop = QtWidgets.QDesktopWidget()
self.setWordWrap(True)
self.setTextElideMode(QtCore.Qt.ElideNone)
self.setRowCount(rows)
self.setColumnCount(columns)
self.setHorizontalHeaderLabels(alphabet)
self.resize(desktop.width(), desktop.height())
def main():
try:
QtWidgets.QApplication([])
except Exception as e:
print(e)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(QtWidgets.QApplication.exec_())
if __name__ == "__main__":
main()
最佳答案
目前,我们无法通过重叠来连接旧的跨越单元格。我们必须删除 split 的单元格并再次跨越它们。 这是一个错误吗?现在是五十:五十。
我们可以通过编码顺序来避免这个问题。
以下是错误报告的结果:https://bugreports.qt.io/browse/QTBUG-81034
关于python - QTableWidget联合单元格问题: After jointing a range of cells horizontally,然后我一般联合单元格,所选单元格发疯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59206505/