×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Customize QListWidgetItem, how to?

    I'm using QListWidget to list things in my application. I now want a custom design of my QListWidgetItem, I want it to show two different labels and an image. It would also be nice to be able to design the ui of my custom QListWidgetItem in the Designer Editor.

    I found out that I could use QListWidget::setItemWidget() or using a delegate. What is the best way? Using a delegate or setItemWidget()? What about performance, which method is heaviest to use?

    This is what I tried:
    Code:
                    //MyItem is a custom widget that takes two strings and sets two labels to those string.
    		MyItem *myItem = new MyItem("Text for label1","Text for label2");
    		QListWidgetItem *item = new QListWidgetItem();
    		item->setSizeHint(QSize(0,65));
    		ui.listWidget->addItem(item);
    		ui.listWidget->setItemWidget(item,myItem);
    Thanks for your help!

  2. #2
    Regular Contributor
    Join Date
    Mar 2009
    Posts
    430

    Re: Customize QListWidgetItem, how to?

    Delegate is the way to go, it is more light weight than setItemWidget, however, setItemWidget is easier to use. I'd go for the delegate.

  3. #3
    Regular Contributor
    Join Date
    May 2009
    Posts
    146

    Re: Customize QListWidgetItem, how to?

    Quote Originally Posted by axeljaeger View Post
    Delegate is the way to go, it is more light weight than setItemWidget, however, setItemWidget is easier to use. I'd go for the delegate.
    Thanks for your help! In the code below you can see how I'm testing my delegate right now. It works just like I want as I can place my text with painter wherever I want. Thanks!

    My problem is now that from this blog http://labs.trolltech.com/blogs/2007...ng-item-views/ I have understood that my delegate should be able to use the QStyles that is set in the application. So what I do in my application is just to set the stylesheets for QListWidget::item and QListWidget::item:selected but nothing happens to my QListWidget::items that is created in the delegate. What am I doing wrong here? Should I do something else in my paint method to get Stylesheets working?

    This is my code:
    Code:
    class MyDelegate : public QStyledItemDelegate  {
    	public:
    	MyDelegate(QObject *parent=0) : QStyledItemDelegate (parent){}
    
    	void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    		if(option.state & QStyle::State_Selected){
    			painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
    		}
    		
    		QString title = index.data(Qt::DisplayRole).toString();
    		QString description = index.data(Qt::UserRole + 1).toString();
    		
    		r = option.rect.adjusted(50, 0, 0, -50);
    		painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft|Qt::TextWordWrap, title, &r);
    		
    		r = option.rect.adjusted(50, 50, 0, 0);
    		painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignLeft|Qt::TextWordWrap, description, &r);
    	}
    		
    	QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
    		return QSize(200, 100);
    	}
    };
    
    int main(int argc, char **argv){
    	QApplication app(argc, argv);
    	QListWidget listWidget;
    	
    	app.setStyleSheet("QListWidget { background: red; } QListWidget::item { background: yellow; } QListWidget::item:selected { background: blue; }");
    	
    	for (int i = 0; i < 4; i++) {
    		QListWidgetItem *item = new QListWidgetItem();
    		item->setData(Qt::DisplayRole, "This is the title");
    		item->setData(Qt::UserRole + 1, "This is description");
    		listWidget.addItem(item);
    	}
    	
    	listWidget.setItemDelegate(new MyDelegate(&listWidget));
    	listWidget.showMaximized();
    	return app.exec();
    }

  4. #4
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Customize QListWidgetItem, how to?

    Maybe you should parent your QListWidgetItems off the QListWidget.

Similar Threads

  1. Customize browser UI and communicate
    By MForceOne in forum [Archived] Flash Lite on Nokia Devices
    Replies: 1
    Last Post: 2009-05-12, 16:02
  2. How can I create my own Customize Menu in Flashlite
    By pakkami22 in forum [Archived] Flash Lite on Nokia Devices
    Replies: 5
    Last Post: 2008-12-31, 07:31
  3. Customize email application
    By hitonly2008 in forum Symbian
    Replies: 5
    Last Post: 2008-11-28, 18:42
  4. customize button
    By kush_192 in forum Symbian User Interface
    Replies: 1
    Last Post: 2008-11-12, 07:11
  5. customize the minus depend on witch View active
    By msalamah82 in forum Symbian User Interface
    Replies: 3
    Last Post: 2006-06-14, 12:33

Posting Permissions

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