开发者

Mistake in Code

in Qt, i want to customize the listview in QT Symbian. so for the customization purpose i have set delegate for the listview. and i am handling the paint event also.. the problem is the text colour.

i am not getting the default text colour, when the item is selected from the listview.

i.e below sample code,i have two strings namely "item"(main-string) and "Test"(sub-string)

main-string draw with default selection colour, but the sub-string will not draw with same color as main string.

i think we need to handle the selection and getting pallete colour.. i am not getting the exact way.

pls give me the solution.

here is the code

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();

        i开发者_如何学运维nt 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();
}

pls help me

Thank u


Try this -

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);
}

The state to colorgroup/colorrole mapping is from the text drawing portion of QCommonStyle::DrawControl for the CE_ItemViewItem element.


It is a bug in Qt 4.6.2 for symbian.

hope it might get fix in next release..

FYI....

Check out here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜