Quantcast
Channel: Jobs
Viewing all articles
Browse latest Browse all 18427

Multiple text lines in QListView

$
0
0
Hi All, I want to display multiple lines of text in a QListview. I have tried for that with custom delegate, but it doesn’t working. My code is here… .h file is… #ifndef WARP_H #define WARP_H   #include <QtGui/QDialog> #include <QItemDelegate>   class wrap : public QDialog {     Q_OBJECT   public:     wrap(QWidget *parent = 0, Qt::WFlags flags = 0);     ~wrap();   };   class delegate : public QItemDelegate { public:       delegate(QObject* = NULL);     ~delegate();       virtual void paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const;     virtual QSize sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const; };     #endif // WARP_H .cpp file is #include "wrap.h" #include <QStringListModel> #include <QPainter> #include <QListView>   wrap::wrap(QWidget *parent, Qt::WFlags flags)     : QDialog(parent, flags) {     setMinimumSize(300,200);     setWindowTitle("ListView Custom Delegate");       QStringListModel *mdl = new QStringListModel();     QStringList list;     QListView *listView = new QListView(this);     listView->setSpacing(5);     listView->setMinimumSize(100,100);     (list) << "long word 1longword1lo ngword2l ongword2";     (list) << "long word 2 long word 2";     (list) << "long word 1 long word 2longwor d1longwor d2lon gword2";     (list) << "long word 1 long word 2long word1lon gword2longword2";     (list) << "longwor d1longwo d2longw ord2longword 1lon word2longw ord2";     (list) << "long word 1";     (list) << "longwor d1l ongword2l ongword1 longword 2longwo rd2";     (list) << "3+ experience of Linux Kernel Programming /Linux Device Driver/Open source application porting/Back porting of ALG modules to Linux Kernel/Voice Data path optimization and Architectural changes on LINUX based ARM platform";     (list) << "The <b>QListWidget</b> class provides an item-based list widget."            << "The <b>QListWidgetItem</b> class provides an item for use with the QListWidget item view class."            << "The <b>QObject</b> class is the base class of all Qt objects."            << "Qt's <b>drag and drop</b> infrastructure is fully supported by the model/view framework. Items in lists, tables, and trees can be dragged within the views, and data can be imported and exported as MIME-encoded data.";     mdl->setStringList(list);     listView->setModel(mdl);     listView->setItemDelegate(new delegate(listView)); }     wrap::~wrap() {   }   delegate::delegate(QObject* parent) :QItemDelegate(parent) {   }   delegate::~delegate() {   }   void delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex & index) const {       int width = option.rect.width();     int height = option.rect.height();     //QRect rect(option.rect.topLeft(), option.rect.bottomRight());       QRect timeRect(option.rect.x() + width/2, 0, option.rect.width()/2, height/3);     QRect messageRect(option.rect.x(), width/2+3,  option.rect.width(), height/2);       //  if (option.state & QStyle::State_Selected)          // painter->fillRect(rect, option.palette.highlight());       QRect boundryRect(option.rect.topLeft(), option.rect.bottomRight());     boundryRect.setRight(messageRect.right() - 10);     boundryRect.setTop(messageRect.top() + 1);       painter->save();     painter->setPen(QPen(Qt::gray));        //painter->drawLine(rect.topLeft(), rect.topRight());     painter->drawRect(boundryRect);     painter->restore();       messageRect.setLeft(messageRect.left() + 2);     messageRect.setTop(messageRect.top() + 2);     messageRect.setRight(messageRect.right() - 12);      painter->drawText(timeRect, Qt::AlignRight  , "Time Is Here");       painter->drawText(messageRect,  Qt::AlignLeft | Qt::TextWordWrap , index.data().toString());    // painter->drawText(option.rect, Qt::AlignLeft | Qt::TextWordWrap , index.data().toString());     //painter->drawText(rect, Qt::AlignTop |Qt::AlignRight  , "Time Is Here"); }   QSize delegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const {    /* QListView *p = (QListView*)parent();     QString text = index.data().toString();     QFontMetrics fm(p->font());       float rw = float(p->viewport()->size().width());     float tw = fm.width(text);     float ratio = tw/rw;     int lines = int(ratio) + 1;     return QSize(p->width(),lines*fm.height());*/       QAbstractItemView *view = dynamic_cast<QAbstractItemView*>(parent());         QFontMetricsF fontMetrics(view->fontMetrics());         QString str = index.data().toString().trimmed();         QStringList words = str.split(QChar(' '));         QRectF boundingRect = fontMetrics.boundingRect(str);         int width = view->viewport()->width();         int times = 0;         while (words.size() > 0) {             times++;             qreal lineWidth = 0;             bool enoughSpace = true;             do {                 QString word = words.first();                 qreal wordWidth = fontMetrics.width(word);                 if (wordWidth + lineWidth < width)                 {                     lineWidth += wordWidth;                     lineWidth += fontMetrics.width(QChar(' '));                     words.removeFirst();                 } else                     enoughSpace = false;               } while (enoughSpace && words.size() > 0);         }         return QSize(width, boundingRect.height() * times - times + 5); } Actually i want to display three string in listview in first row one string on left side & one on right side & one more string in next row. so how can i get that. plz suggest me for that?

Viewing all articles
Browse latest Browse all 18427

Trending Articles