单元格编辑期间 QTableWidgetItem 中的居中文本
Centering Text in QTableWidgetItem during cell Edit
下面的 PyQt4 示例演示了我的问题。 QTableWidget 单元格中的文本很好地居中,直到您编辑单元格。当您编辑单元格时,文本左对齐。
我想在编辑每个单元格时保持居中对齐。
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.table = QtGui.QTableWidget(parent=self)
self.table.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.table.setColumnCount(2)
self.table.setRowCount(2)
self.table.setHorizontalHeaderLabels(['col1','col2'])
self.table.setVerticalHeaderLabels(['row1','row2'])
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(0, 0, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(0, 1, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(1, 0, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(1, 1, item)
layout = QtGui.QGridLayout()
layout.addWidget(self.table, 1, 0)
self.setLayout(layout)
################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle("Plastique") # set style
myapp = MainWindow() # instantiate the main window
myapp.show() # show the main window
rc = app.exec_()
myapp.close()
sys.exit(rc) # exit with the same return code of Qt application
一个可能的解决方案是实现委托:
class CenterDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QStyledItemDelegate.createEditor(self, parent, option, index)
editor.setAlignment(Qt.AlignCenter)
return editor
然后将其设置为 QTableWidget
:
self.table = QtGui.QTableWidget(parent=self)
delegate = CenterDelegate()
self.table.setItemDelegate(delegate)
下面的 PyQt4 示例演示了我的问题。 QTableWidget 单元格中的文本很好地居中,直到您编辑单元格。当您编辑单元格时,文本左对齐。
我想在编辑每个单元格时保持居中对齐。
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.table = QtGui.QTableWidget(parent=self)
self.table.setEditTriggers(QtGui.QAbstractItemView.AllEditTriggers)
self.table.setColumnCount(2)
self.table.setRowCount(2)
self.table.setHorizontalHeaderLabels(['col1','col2'])
self.table.setVerticalHeaderLabels(['row1','row2'])
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(0, 0, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(0, 1, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(1, 0, item)
item = QtGui.QTableWidgetItem('x')
item.setFlags(Qt.NoItemFlags | Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable)
item.setTextAlignment(Qt.AlignHCenter|Qt.AlignVCenter|Qt.AlignCenter)
self.table.setItem(1, 1, item)
layout = QtGui.QGridLayout()
layout.addWidget(self.table, 1, 0)
self.setLayout(layout)
################################################################
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle("Plastique") # set style
myapp = MainWindow() # instantiate the main window
myapp.show() # show the main window
rc = app.exec_()
myapp.close()
sys.exit(rc) # exit with the same return code of Qt application
一个可能的解决方案是实现委托:
class CenterDelegate(QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QStyledItemDelegate.createEditor(self, parent, option, index)
editor.setAlignment(Qt.AlignCenter)
return editor
然后将其设置为 QTableWidget
:
self.table = QtGui.QTableWidget(parent=self)
delegate = CenterDelegate()
self.table.setItemDelegate(delegate)