Results 1 to 5 of 5

Thread: using QListWidget and checkboxs ?

  1. #1
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default using QListWidget and checkboxs ?

    Hello everybody!
    I want to create a QString listwidget and which contains a checkbox and qstring on the same row. But I can not pass in the second with a time listwidget be, so I used 2 listwidget to save QString and checkbox. So my interface looks very bad, I want my interface like in the picture below. How do I do?
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QListWidget and checkboxs ?

    Instead of QListWidget use a QTreeWidget with two columns.
    Then use QTreeWidget::setItemWidget() to insert a Check Box

    Qt Code:
    1. item = new QTreeWidgetItem (ui->treeWidget);
    2. item->setText (0, yourText);
    3. ui->treeWidget->setItemWidget (item, 1, new QCheckBox);
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2011
    Posts
    2
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: using QListWidget and checkboxs ?

    you can subclass QStyledItemDelegate and in re implement paintEvent as
    Qt Code:
    1. void styledListItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    2. {
    3.  
    4. QStyleOptionViewItem myOpt = option;
    5. initStyleOption(&myOpt,index);
    6. //for drawing the checkbox
    7.  
    8.  
    9. QRect chkRect(option.rect.right() - option.rect.height(),
    10. option.rect.top(),
    11. option.rect.height(),
    12. option.rect.height());
    13. opt.rect = chkRect;
    14.  
    15. if(option.state & QStyle::State_Selected)
    16. opt.state = QStyle::State_On;
    17.  
    18. QApplication::style()->drawControl(QStyle::CE_CheckBox,
    19. &opt, painter);
    20.  
    21. //adjust the rect for displayed text
    22. int dw = -chkRect.width();
    23. myOpt.rect.adjust(0,0,dw,0);
    24. QStyledItemDelegate::paint(painter, myOpt, index);
    25. }
    To copy to clipboard, switch view to plain text mode 

    set the item delegate for the list widget in your code

    Qt Code:
    1. listWidget = new QListWidget();
    2.  
    3. listWidget->setItemDelegate(new styledListItemDelegate());
    4.  
    5. new QListWidgetItem(tr("Oak"), listWidget);
    6.  
    7. new QListWidgetItem(tr("Fir"), listWidget);
    8.  
    9. new QListWidgetItem(tr("Pine"), listWidget);
    To copy to clipboard, switch view to plain text mode 


    Please note that I have shown code for enabling checkboxes on the left side of the text you need to write code to handle stuff like switching on and off the checked state etc.

    Hope this helps

    -Sukesh

  4. #4
    Join Date
    Jun 2011
    Posts
    16
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: using QListWidget and checkboxs ?

    Thank mcosta, Sukesh ! I have done.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany, Munich
    Posts
    16
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QListWidget and checkboxs ?

    The Widget what you want looks for me like a QTableWidget and neither like a QListWidget or QTreeWidget.
    Maybe you can use QCheckBox with "openPersistentEditor" ?

Similar Threads

  1. Replies: 2
    Last Post: 1st April 2011, 09:32
  2. QListWidget
    By babygal in forum Newbie
    Replies: 6
    Last Post: 1st September 2010, 12:01
  3. Replies: 1
    Last Post: 27th August 2010, 05:37
  4. QListWidget
    By bismitapadhy in forum Qt Programming
    Replies: 17
    Last Post: 22nd March 2010, 11:44
  5. Another QListWidget bug?
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2006, 13:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.