TTkTable alignment #467
Replies: 5 comments 5 replies
|
Yes, I can implement the alignment but it is going to take few days. |
|
hi, about the alignment, I can think of different solution. from PySide6.QtWidgets import QApplication, QTableView
from PySide6.QtCore import Qt, QAbstractTableModel
class MyTableModel(QAbstractTableModel):
def __init__(self, data):
super().__init__()
self._data = data
def rowCount(self, parent=None):
return len(self._data)
def columnCount(self, parent=None):
return len(self._data[0])
def data(self, index, role=Qt.DisplayRole):
if not index.isValid():
return None
value = self._data[index.row()][index.column()]
if role == Qt.DisplayRole:
return value
if role == Qt.TextAlignmentRole:
if index.column() == 0:
return Qt.AlignLeft | Qt.AlignTop
elif index.column() == 1:
return Qt.AlignCenter
elif index.column() == 2:
return Qt.AlignRight | Qt.AlignVCenter
return None
app = QApplication([])
data = [
["Left", "Center\npippo", "Right"],
["Apple", "Banana\npippo", "Cherry"],
["Dog", "Elephant\npippo", "Fox"]
]
model = MyTableModel(data)
view = QTableView()
view.setModel(model)
view.show()
app.exec()in pyTermTk could be somethinglike: class ModelWithAlignment(ttk.TTkTableModelList):
def data(self, row, col, role=ttk.TTkK.DisplayRole):
if role == ttk.TTkK.TextAlignmentRole:
if col == 0:
return ttk.TTkK.AlignLeft | ttk.TTkK.AlignTop
elif col == 1:
return ttk.TTkK.AlignCenter
elif col == 2:
return ttk.TTkK.AlignRight | ttk.TTkK.AlignVCenter
return supet().data(row,col,role)
tableModel = ModelWithAlignment(data=dataList)Another solution is to have a displayModel that you can use in the tableModel and that you can customize to return a different alignment per cell and I can provide a set of predefined models, where you for example can specify the alignment per column, row or cell or area. I prefer to avoid to overspecialize the TableWidget, |
|
WIP: Screen.Recording.2025-10-18.at.22.47.56.002.movI opted to extend the tableModel with the displayData method that can be used to customize the alignment. class MyTableModel(ttk.TTkTableModelList):
def displayData(self:ttk.TTkTableModelList, row:int, col:int) -> Tuple[ttk.TTkString, ttk.TTkK.Alignment]:
data, legacy_align = super().displayData(row,col)
if 0 == col%4:
return data, ttk.TTkK.Alignment.LEFT_ALIGN
if 1 == col%4:
return data, ttk.TTkK.Alignment.CENTER_ALIGN
if 2 == col%4:
return data, ttk.TTkK.Alignment.RIGHT_ALIGN
if 3 == col%4:
return data, ttk.TTkK.Alignment.JUSTIFY
return data, legacy_align |
|
Actually I was thinking, considering that excel and google docs, by default align numbers to right, I can use this as default behavior, so you don't need to customize the alignment unless you need it. |
|
the change has been deployed, it will be ready to the next release |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I don't found a way to to align text inside columns and to set the column size. In the older table there was these two methods:
the first one I'm not sure that there's no other way to do the same thing... but for the second, the alignment is the most important to me...could you implement these in the newer TkTable as well?
Thanks in advance,
Luca
All reactions