如何在同一单元格中将一个数字左对齐,一个数字右对齐?也让他们不同的颜色

How to align one number to the left and one to the right in the same cell? Also make them different colour

我有一个 QAbstractTableModel,我想在同一个单元格中绘制一个左对齐的数字和一个右对齐的数字,如下所示:

╔══════════════════════╦═════╗
║ 31             +15   ║ foo ║
║══════════════════════╦═════║
║ 11             +15   ║ xxx ║
║══════════════════════╦═════║
║ 15             +15   ║ aaa ║
╚══════════════════════╩═════╝

我也希望左边的数字和右边的数字颜色不同

我在 Python 3 中使用 PySide2,但欢迎使用任何语言的解决方案

这是我目前用来在单元格中设置数据的代码。

    def data(self, index, role=Qt.DisplayRole):
        if index.isValid():
            if role == Qt.DisplayRole:
                return str(self.data[index.row()][index.column()]) + " +15" #how do I align right and change colour
        return None

我想过制作两个单独的列并隐藏它们之间的线,但我相信一定有更好的解决方案。

给你。这应该为您指明正确的方向。我使用的是标准模型,因为这是我可用的模型,但设置应该适用于任何类型的模型。

查看此示例以更深入地了解委托:Star Delegate

我的代码答案:

import sys

from PySide2 import QtGui, QtCore, QtWidgets


data = {
    "31":{
        "status":"foo"
    },
    "11":{
        "status":"xxx"
    },
    "15":{
        "status":"aaa"
    }
}

class MyTableDelegate(QtWidgets.QItemDelegate):
    def __init__(self, view):
        super(MyTableDelegate, self).__init__()
        self._view = view

    def paint(self, painter, option, index):

        rect_item = option.rect

        # Get Left number
        index_number = index.data(role=QtCore.Qt.DisplayRole)

        # Position left number
        rect_number = QtCore.QRect(
            rect_item.left(),
            rect_item.top(),
            rect_item.width(),
            rect_item.height()
        )

        font_name = QtGui.QFont("Segoe UI", 12, QtGui.QFont.Normal)

        # Store default painter settings
        painter.save()
        painter.setPen(QtGui.QColor('#af1cbd'))
        painter.setFont(font_name)

        # Draw text
        QtWidgets.QApplication.style().drawItemText(
            painter,
            rect_number,
            QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter,
            QtWidgets.QApplication.palette(),
            True,
            index_number)

        painter.restore()

        # Get +15
        index_number = index.data(role=QtCore.Qt.UserRole)

        # Position +15
        rect_plus15 = QtCore.QRect(
            rect_item.right() - (rect_item.width()/2),
            rect_item.top(),
            rect_item.width(),
            rect_item.height()
        )

        font_name = QtGui.QFont("Segoe UI", 12, QtGui.QFont.Normal)

        # Store default painter settings
        painter.save()
        painter.setPen(QtGui.QColor('#1bcc4a'))
        painter.setFont(font_name)

        # Draw text
        QtWidgets.QApplication.style().drawItemText(
            painter,
            rect_plus15,
            QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter,
            QtWidgets.QApplication.palette(),
            True,
            str(index_number))

        painter.restore()


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()

        self.tableview = QtWidgets.QTableView()
        self.tableview.setItemDelegateForColumn(0, MyTableDelegate(self.tableview))

        self.setCentralWidget(self.tableview)

        self.model = QtGui.QStandardItemModel()
        self.tableview.setModel(self.model)
        self.fillModel()

        self.show()

    def fillModel(self):
        # For key in dict data
        for i in data:
            name_str = i
            status_str = data[i]["status"]

            # Store data in 1st column item using roles
            item_0 = QtGui.QStandardItem()
            item_0.setData(name_str, QtCore.Qt.DisplayRole)
            item_0.setData('+15', QtCore.Qt.UserRole)
            # How to store more data:
            # item_0.setData(+15, QtCore.Qt.UserRole + 1)
            # item_0.setData(+15, QtCore.Qt.UserRole + 2)
            # item_0.setData(+15, QtCore.Qt.UserRole + etc)
            # UserRole is just an integer. This lets you store a bunch of data

            # Store data in 2nd column item using roles
            item_1 = QtGui.QStandardItem()
            item_1.setData(status_str, QtCore.Qt.DisplayRole)

            # Append row
            items = [item_0, item_1]
            self.model.appendRow(items)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())