Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

An example of QStyledItemDelegate formatting doubles



  • Hello, I tried approaching formatting through telling the QSqlQueryModel on which columns to return Qt::AlignRight for Qt::TextAlignmentRole. Now I understand that I need to subclass QStyledItemDelegate. All I need to know is how to subclass in a minimalistic way (according to this post, I only need to override displayText() method). I tried googling examples and only found star-rating editor example which is not helpful in my case. I tried googling QSID source code, but didn't get any idea.
    Please, clarify how do I change formatting (of toString conversion) and alignment of content.



  • change formatting is easy, as you already hinted is just about reimplementing displayText and return a QString representation of the data you have. The alignment is completely different. You need to reimplement paint, and make a small change to the original one:

    void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        Q_ASSERT(index.isValid());
        QStyleOptionViewItem opt = option;
        initStyleOption(&opt, index);
    opt.displayAlignment = Qt::AlignRight | Qt::AlignVCenter; // only change vs the original
        const QWidget *widget = option.widget;
        QStyle *style = widget ? widget->style() : QApplication::style();
        style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    }
    

    alternatively you can subclass QSqlQueryModel and reimplement data:

    class AlignedQueryModel : public QSqlQueryModel{
    Q_OBJECT
    Q_DISABLE_COPY(AlignedQueryModel)
    public:
    explicit AlignedQueryModel(QObjetc* parent = Q_NULLPTR) : QSqlQueryModel(parent){}
    QVariant data(const QModelIndex &item, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE{
    if(role == Qt::TextAlignmentRole && item.column()<2) //the first 2 columns are right aligned
    return Qt::AlignRight | Qt::AlignVCenter;
    return QSqlQueryModel::data(item,role);
    }
    };
    


  • @VRonin Hello, for now I can't unfortunately get a single of those methods to work. I've started debugging with simpler one, displayText() and it just doesn't work. My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't, I hope it's not what makes the difference
    Here's doubledelegate .h/.cpp



  • @starryeyed said in An example of QStyledItemDelegate formatting doubles:

    My other delegates which are inherited from QItemDelegate have a QWidget argument in the constructor. This one doesn't,

    What one doesn't?

    Only @VRonin's first example uses QItemDelegate, and he's only given you the paint() override, so you can have what you like in your constructor for MyDelegate, like the base QItemDeletegate.

    His second example doesn't use a QItemDelegate, it just sub-classes QSqlQueryModel.

    Both QItemDelegate & QSqlQueryModel constructors take an optional QObject *parent = nullptr. Like many Qt classes, this is just to set the parent. I don't see how that's relevant to whether they work or not.



  • @JonB I'm sorry for digging in wrong direction, I mistook compiler's errors about "overriding private parts of class", thought that it was related to constructor because the error was on constructor's definition line.
    So everything works. Blessings go out to the patience of local gurus



  • @starryeyed You want to be careful about "overriding private parts", it can be painful ;-)


Log in to reply