×

Discussion Board

Results 1 to 12 of 12

Hybrid View

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    47

    List selection colour

    Hi all.

    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 am facing the problem, only when the item is selected non selected item colour is fine.

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

    main-string("item") string draw with default selection color, but the sub-string("Test) will not draw with same color as main string.

    i think we need to handle the selection and getting pallet color or some sort of logic is required i guess..

    i am not getting the exact way..

    pls give me the solution.

    here is the code
    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();
    
            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();
    }
    Thanks

  2. #2
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: List selection colour

    This is a sample implementation:
    Code:
    #include <QtGui>
    #include <QDebug>
    
    class SettingDelegate : public QStyledItemDelegate
    {
    public:
        SettingDelegate(QObject* parent = 0);
        enum ItemDataRole {SubheaderTextRole = Qt::UserRole + 1};
        QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
    };
    
    SettingDelegate::SettingDelegate(QObject* parent)
            : QStyledItemDelegate(parent)
    {
    }
    
    QSize SettingDelegate::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 SettingDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    {
        QStyledItemDelegate::paint(painter, option, index);
        QString headerText(index.data(SubheaderTextRole).toString());
    
        if (option.state & QStyle::State_Selected)
            painter->setPen(QPen(option.palette.color(QPalette::HighlightedText)));
        else
            painter->setPen(QPen(option.palette.color(QPalette::Text)));
        painter->drawText(option.rect, Qt::AlignLeft, headerText);
    }
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QStandardItemModel model;
    
        for (int i = 0; i < 10; ++i) {
            QStandardItem *item = new QStandardItem(QString("item%1").arg(i));
            item->setData(QString("Test%1").arg(i), SettingDelegate::SubheaderTextRole);
            model.appendRow(item);
        }
    
        SettingDelegate delegate;
        QListView MylistView;
        MylistView.setViewMode(QListView::ListMode);
        MylistView.setMovement(QListView::Free);
        MylistView.setSelectionMode(QAbstractItemView::SingleSelection);
        MylistView.setItemDelegate(&delegate);
        MylistView.setModel(&model);
    
        MylistView.showMaximized();
    
        return a.exec();
    }
    Last edited by divanov; 2010-03-04 at 08:39. Reason: Test application updated

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    47

    Re: List selection colour

    HI, danial.

    Thank you very much for reply.

    have you tried that code in Symbian emulator or mobile? is it working for you?

    i have tried the way you suggested, still i am facing the problem.

    still i am not able to get the highligthed color text when the selection is done.

    i am getting the different color for "item" and "test" strings. pls clarify me once.

    Thanks.

  4. #4
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: List selection colour

    Quote Originally Posted by ravindra.n View Post
    have you tried that code in Symbian emulator or mobile? is it working for you?
    i have tried the way you suggested, still i am facing the problem.
    still i am not able to get the highligthed color text when the selection is done.
    i am getting the different color for "item" and "test" strings. pls clarify me once.
    I've updated the code. Now it's final.
    Last edited by divanov; 2010-03-04 at 08:41.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    47

    Re: List selection colour

    Hi danial

    i have tried your updated code, still i am facing the problem..
    i tried in both mobile and emulator.

    i will give you the screen shot of it.. pls see is same image appearing for you when you run the code which
    you have given or not..

    uploaded image link

    pls clarify once..

    Thanks

  6. #6
    Super Contributor
    Join Date
    Oct 2009
    Posts
    4,326

    Re: List selection colour

    Quote Originally Posted by ravindra.n View Post
    Hi danial

    i have tried your updated code, still i am facing the problem..
    i tried in both mobile and emulator.

    i will give you the screen shot of it.. pls see is same image appearing for you when you run the code which
    you have given or not..

    uploaded image link

    pls clarify once..

    Thanks
    Great, it works as intended. Try to change ColorRoles
    http://doc.trolltech.com/4.6/qpalett...ColorRole-enum

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    47

    Re: List selection colour

    Hi danili,

    i am very sorry, but you need to help me to get out of this.

    i have tried these possibilities, none are working.

    Code:
    painter->setBrush(option.palette.color(QPalette::All,QPalette::Highlight));
    painter->setPen(option.palette.color(QPalette::All, QPalette::HighlightedText));
    Code:
    painter->setBrush(option.palette.color(QPalette::All,QPalette::HighlightedText));
    painter->setPen(option.palette.color(QPalette::All, QPalette::Highligh));
    Code:
               QBrush highlightBrush = option.palette.highlight();
              QBrush highlightedStr = option.palette.highlightedText();
    Code:
               painter->setBrush(highlightBrush);
               painter->setPen(highlightedStr .color());
    i don't no how its working for u.
    i am struggling hard to get out of this..

    how to solve this issue?..
    which version QT are you using?.
    4.6.2? or wat?

    i tried in both S60 and N96 emulators its working none of these emulators even in phones also i tried, but its not working

    Thanks

Similar Threads

  1. Changing list box selection dynamically
    By jinuthomas in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-09-22, 10:19
  2. simple list with no selection
    By jinuthomas in forum Symbian User Interface
    Replies: 2
    Last Post: 2009-08-28, 13:45
  3. Selection list Dialog problem....
    By sunil304047 in forum Symbian
    Replies: 10
    Last Post: 2008-12-10, 12:57
  4. Problem with Skin enabling - disabling and Setting List in S60
    By sauvikds in forum Symbian User Interface
    Replies: 1
    Last Post: 2008-08-22, 13:45
  5. cba for selection list dialog
    By ninidotnet in forum Symbian User Interface
    Replies: 16
    Last Post: 2006-05-17, 05:59

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×