在我的应用程序中,我有一个表格小部件。我创建了一个新类来实现表格单元格对齐的委托作为中心。委托工作正常,但样式表无法正常工作。下面是我实现委托类的方法:
//i create new class with "QStyledItemDelegate" as base class
//QAlignmentDelegate.h
#ifndef QALIGNMENTDELEGATE_H
#define QALIGNMENTDELEGATE_H
#include <QStyledItemDelegate>
class QAlignmentDelegate : public QStyledItemDelegate
{
public:
QAlignmentDelegate(Qt::Alignment alignment);
virtual void paint(QPainter* painter,
const QStyleOptionViewItem& option,
const QModelIndex& index) const;
private:
Qt::Alignment m_alignment;
};
#endif // QALIGNMENTDELEGATE_H
//QAlignmentDelegate.cpp
#include "qalignmentdelegate.h"
QAlignmentDelegate::QAlignmentDelegate(Qt::Alignment alignment)
{
m_alignment=alignment;
}
void QAlignmentDelegate::
paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem alignedOption(option);
alignedOption.displayAlignment = m_alignment;
QStyledItemDelegate::paint(painter, alignedOption, index);
}
//main.cpp
QTableWidgetItem * protoitem = new QTableWidgetItem();
protoitem->setTextAlignment(Qt::AlignRight);
QTableWidgetItem * newitem = protoitem->clone();
tableWidget->setItemPrototype(newitem);
现在如何在这个委托类中实现样式表?