[Qt-interest] Listview Selection problem

Ramesh ramesh.bs at robosoftin.com
Tue Mar 2 13:35:57 CET 2010


Hi all.

in Qt, i want to customize the listview. 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();

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

 

pls help me 

Thank u

 


-----------------------------------------------
Robosoft Technologies - Come home to Technology

Disclaimer: This email may contain confidential material. If you were not an intended recipient, please notify the sender and delete all copies. Emails to and from our network may be logged and monitored. This email and its attachments are scanned for virus by our scanners and are believed to be safe. However, no warranty is given that this email is free of malicious content or virus.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100302/e65ccf9c/attachment.html 


More information about the Qt-interest-old mailing list