在Qt中,我想在QT Symbian中自定义列表视图。所以为了自定义的目的,我为listview设置了委托。我也在处理油漆事件..问题是文字颜色。
当从列表视图中选择项目时,我没有获得默认文本颜色。
即下面的示例代码,我有两个字符串,即“item”(主要字符串)和“Test”(子字符串)
使用默认选择颜色绘制主字符串,但子字符串不会使用与主字符串相同的颜色绘制。
我认为我们需要处理选择并获得托盘颜色..我没有得到确切的方法。
请给我解决方案。
这是代码
class SettingDeligate : public QStyledItemDelegate
{
// Q_OBJECT
public:
explicit SettingDeligate(QObject* parent = 0);
virtual ~SettingDeligate();
enum ItemDataRole {SubheaderTextRole = Qt::UserRole + 100};
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
};
SettingDeligate::SettingDeligate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
SettingDeligate::~SettingDeligate()
{
}
QSize SettingDeligate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QString line1 = index.data(Qt::DisplayRole).toString();
QString line2 = index.data(SubheaderTextRole).toString();
int textW = qMax(option.fontMetrics.width(line1), option.fontMetrics.width(line2));
//QSize iconSize = icon.actualSize(option.decorationSize);
QSize iconSize(27,19);
return QSize(qMax(textW, iconSize.width()) + 4,
iconSize.height() + 2 + option.fontMetrics.lineSpacing() * 2 + 4);
}
void SettingDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter,option,index);
QString headerText(qvariant_cast<QString>(index.model()->data(index, SubheaderTextRole)));
QPoint point1 (option.rect.topLeft());
painter->drawText(point1.x()+25,point1.y()+25,headerText);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QListView *MylistView = new QListView();
MylistView->setViewMode(QListView::ListMode);
MylistView->setMovement(QListView::Free);
MylistView->setItemDelegate(new (SettingDeligate));
MylistView->setSelectionMode(QAbstractItemView::SingleSelection);
QStandardItemModel *model = new QStandardItemModel();
MylistView->setModel(model);
QStandardItem *item = new QStandardItem("item1");
item->setData("Test1", SettingDeligate::SubheaderTextRole);
model->appendRow(item);
QStandardItem *item1 = new QStandardItem("item2");
item1->setData("Test2", SettingDeligate::SubheaderTextRole);
model->appendRow(item1);
QStandardItem *item2 = new QStandardItem("item3");
item2->setData("Test3", SettingDeligate::SubheaderTextRole);
model->appendRow(item2);
QDesktopWidget* desktopWidget = QApplication::desktop();
//get client rect.
QRect clientRect = desktopWidget->availableGeometry();
MylistView->setGeometry(clientRect);
MylistView->show();
return a.exec();
}
请帮帮我
谢谢你
答案 0 :(得分:0)
试试这个 -
void SettingDeligate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter,option,index);
QString headerText(qvariant_cast<QString>(index.model()->data(index, SubheaderTextRole)));
QPoint point1 (option.rect.topLeft());
QPalette::ColorGroup colorGroup = QPalette::Normal;
if(!option.state.testFlag(QStyle::State_Enabled))
colorGroup = QPalette::Disabled;
else if (!option.state.testFlag(QStyle::Active))
colorGroup = QPalette::Inactive;
QPalette::ColorRole colorRole = QPalette::Text;
if(option.state.testFlag(QStyle::State_Selected))
colorRole = QPalette::HighlightedText;
if(option.status.testFlag(QStyle::State_Editing))
colorRole = QPallete::Text;
painter->setPen(option.palette.color(colorGroup, colorRole));
painter->drawText(point1.x()+25,point1.y()+25,headerText);
}
colorgroup / colorrole映射的状态来自QCommonStyle :: DrawControl的文本绘图部分,用于CE_ItemViewItem元素。
答案 1 :(得分:0)