QTableWidget Joint Cells Problem :水平连接一系列单元格后,然后我一般连接单元格,选定的单元格会疯狂
QTableWidget Joint Cells Problem : After jointing a range of cells horizontally, and then I joint the cells generally, the selected cells go mad
我想制作一个类似 Excel
的电子表格。
最重要的功能之一是连接单元格和拆分单元格。
我做了一个示例代码。
首先,我水平连接单元格。 (请select横向联合单元格)
然后,我连接了上面的单元格。 (请select联合细胞)
请 select 相同范围的单元格。
奇怪的事情发生了。
一列单元格按原样连接在一起,但其他列被拆分。
而且,我在单元格中推入一个单元格,所有连接的单元格都是 selected.
这是一个错误吗?
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()
目前,我们无法通过重叠来连接旧的跨越单元格them.We必须移除分裂的单元格并再次跨越它们。
这是一个错误吗?现在是五十:五十。
我们可以通过编码顺序来避免这个问题。
我想制作一个类似 Excel
的电子表格。
最重要的功能之一是连接单元格和拆分单元格。
我做了一个示例代码。
首先,我水平连接单元格。 (请select横向联合单元格)
一列单元格按原样连接在一起,但其他列被拆分。 而且,我在单元格中推入一个单元格,所有连接的单元格都是 selected.
这是一个错误吗?
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()
目前,我们无法通过重叠来连接旧的跨越单元格them.We必须移除分裂的单元格并再次跨越它们。 这是一个错误吗?现在是五十:五十。
我们可以通过编码顺序来避免这个问题。