Search code examples
pythonexcelqtablewidgetpyside2

QTableWidget Joint Cells Problem : After jointing a range of cells horizontally, and then I joint the cells generally, the selected cells go mad


I want to make a spreadsheet like Excel. One of the most important functionality is to joint cells and split cells. I make a sample code.

First, I jointed cells horizontally. (Please select Joint Cells Horizontally) enter image description here And then, I jointed the above cells. (Please select Joint Cells) enter image description here Please select the same range of cells. enter image description here A odd thing occurs.

One column cells are jointed as they were, but the other columns are splitted. And, I push a cell in the cells, all the jointed cells are selected.

Is this a bug?

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()

Solution

  • At the present, we cannot joint the old spanned cells by overlapping them.We have to remove the splitted cells and span them again. Is it a bug? It is fifty : fifty.

    We will be able to avoid this problem by the order of coding.

    Here is the result of bug report: https://bugreports.qt.io/browse/QTBUG-81034