QTableWidget联合单元格问题:水平联合一系列单元格之后,然后我通常联合这些单元格,所选单元格就会发疯

问题描述 投票:1回答:1

我想制作一个像Excel这样的电子表格。最重要的功能之一是联合细胞和分裂细胞。我编写了示例代码。

首先,我水平接合细胞。 (请水平选择关节单元)enter image description here然后,我联合了以上单元格。 (请选择联合单元)enter image description here请选择相同范围的单元格。enter image description here发生奇怪的事情。

一列单元格按原样连接,但其他列被拆分。然后,我将一个单元格推入单元格中,所有联合的单元格都被选中。

这是错误吗?

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()
python excel qtablewidget pyside2
1个回答
0
投票

目前,我们无法通过重叠将旧的跨单元格合并在一起。我们必须删除拆分后的单元格,然后再次跨度。是虫子吗?现在是五十:五十。

我们将能够通过编码顺序避免此问题。

这里是错误报告的结果:https://bugreports.qt.io/browse/QTBUG-81034

© www.soinside.com 2019 - 2024. All rights reserved.