如何以这种方式对齐 qtreewidget 列中的文本:“...qtreewidgetitemdata”而不是 "qtreewidgetitemdata..."?

How to align the text in qtreewidget column in this manner : "...qtreewidgetitemdata" instead of "qtreewidgetitemdata..."?

如何以这种方式对齐 qtreewidget 的第 1 列中的数据:

|Column1 |Column2|
|+...abcd|efgh   |
|+...ijkl|mnop   |

而不是

|Column1 |Column2|
|+xyab...|efgh   |
|+pqij...|mnop   |

您必须使用委托建立省略模式:

#include <QtWidgets>

class ElideLeftDelegate: public QStyledItemDelegate
{
public:
    using QStyledItemDelegate::QStyledItemDelegate;
protected:
    void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const{
        QStyledItemDelegate::initStyleOption(option, index);
        option->textElideMode = Qt::ElideLeft;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTreeWidget w;
    w.setItemDelegateForColumn(0, new ElideLeftDelegate{&w});
    w.setColumnCount(2);
    new QTreeWidgetItem(&w, {"abcdefghijklmnopqrdstuvwxyz", "AVCDEFGHIJKLMNOPQRSTUVWXYZ"});
    new QTreeWidgetItem(&w, {"AVCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrdstuvwxyz"});
    w.show();
    return a.exec();
}