×

Discussion Board

Results 1 to 12 of 12
  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

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

    Re: List selection colour

    Hi danili..

    at last i found satisfactory answer for my question.

    i come to know its bug in QT symbian, then will fix it in next release..

    FYI pls go through this link

    http://bugreports.qt.nokia.com/browse/QTBUG-8704

    i thank all the contributes for this post..

    Thank you very much.

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

    Re: List selection colour

    Quote Originally Posted by ravindra.n View Post
    i come to know its bug in QT symbian, then will fix it in next release..
    You can easily workaround this bug by fetching color from the theme as QS60Style does.

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

    Re: List selection colour

    Hi, danili..

    Thanks,

    i am very new to QT, can you pls pass me the peace of code, which fetches the s60style and sets to the font.

    i have no idea about style and style sheets, i am learning QT still..

    it will be helpfull if you help..

    Thanks

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

    Re: List selection colour

    This is S60style code:
    http://qt.gitorious.org/qt/qt/blobs/...e.cpp#line1482
    it uses private implementation to get the color. You would need to copy part of private implementation into your code.

  12. #12
    Nokia Developer Champion
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: List selection colour

    If you are wondering about fonts, there are only three installed on the hardware by default (at least on our N97), though you can supposedly install more if you want. (Note that the fonts on the emulator are different.)

    I wrote this quickie to display the fonts:
    Code:
    #include "FontDemo.h"
    
    FontDemo::FontDemo(QWidget *parent)
        : QMainWindow(parent)
    {
    	ui.setupUi(this);
    	QStringList families;
    	families << "Arial";
    	families << database.families();
    	ui.fontName ->addItems(families);
    	QStringList styles;
    	styles << "Light" << "Normal" << "DemiBold" << "Bold" << "Black";		
    	ui.fontStyle ->addItems(styles);
    	QStringList pointSizes;
    	pointSizes << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11" << "12";
    	pointSizes << "14" << "16" << "18" << "20" << "22" << "24" << "26";
    	pointSizes << "28" << "30" << "32" << "34" << "36" << "38" << "40";
    	ui.pointSize ->addItems(pointSizes);
    	connect(ui.fontName, SIGNAL(currentIndexChanged(int)), this, SLOT(changed()));
    	connect(ui.fontStyle, SIGNAL(currentIndexChanged(int)), this, SLOT(changed()));
    	connect(ui.pointSize, SIGNAL(currentIndexChanged(int)), this, SLOT(changed()));
    	connect(ui.exit, SIGNAL(clicked(bool)), this, SLOT(ended()));
    	ui.textEdit ->setReadOnly(true);
    	ui.textEdit ->setHtml("<p>The quick brown fox jumped over the lazy dog's back.</p><p>Now is the time for all good men to come to the aid of their country!</p><p>$123,456,789.01%</p><p>How is one to assess and evaluate a typeface in terms of its asthetic design?</p>");
    	changed();
    }
    
    FontDemo::~FontDemo()
    {
    
    }
    
    void FontDemo::changed() {
    	QString fontName = ui.fontName ->currentText();
    	QString fontStyle = ui.fontStyle ->currentText();
    	QString pointText = ui.pointSize ->currentText();
    	int pointSize = pointText.toInt();
    	QFont font;
    	if (fontName.compare("Arial") == 0 || true) {
    		int weight = -1;
    		if (fontStyle.compare("Light") == 0) {
    			weight = QFont::Light;
    		}
    		else if (fontStyle.compare("Normal") == 0) {
    			weight = QFont::Normal;
    		}
    		else if (fontStyle.compare("DemiBold") == 0) {
    			weight = QFont::DemiBold;
    		}
    		else if (fontStyle.compare("Bold") == 0) {
    			weight = QFont::Bold;
    		}
    		else {
    			weight = QFont::Black;
    		}
    		font = QFont(fontName, pointSize, weight);
    	}
    	else {
    		font = database.font(fontName, fontStyle, pointSize);
    	}
    	ui.textEdit ->setFont(font);
    	ui.textEdit ->update();
    }
    
    void FontDemo::ended() {
    	app ->exit();
    }
    (I added "Arial" font to the list so one can see what it gets mapped to -- the code will make a "best effort" to map unsupported fonts to supported fonts.)

    The UI is straight-forward -- three QComboBoxes for the selection, a QTextEdit to display the font, and an exit button. One could add selections for italic, underline, etc.

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
  •