Results 1 to 5 of 5

Thread: adding radio button in listview

  1. #1
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default adding radio button in listview

    Dear experts,

    i ve created a sample application to display radio button in list view. each item in list view should contain a radio button, an icon and text. I'm using a delegate class to do so.
    hut it is not displaying in the view.

    please tell em what i need to correct in below code.

    Qt Code:
    1. delegate.cpp
    2. #include "radiobtndelegates.h"
    3.  
    4.  
    5. RadioButtonDelegate::RadioButtonDelegate(QObject *parent) : QItemDelegate(parent)
    6. {
    7. };
    8.  
    9. RadioButtonDelegate::~RadioButtonDelegate(){
    10.  
    11. };
    12.  
    13. QWidget* RadioButtonDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    14. {
    15. Q_UNUSED(option);
    16. Q_UNUSED(index);
    17. QRadioButton* editor = new QRadioButton(parent);
    18. editor->setAutoFillBackground(true);
    19. connect(editor, SIGNAL(toggled(bool)), this, SLOT(emitCommitData()));
    20. return editor;
    21. };
    22.  
    23.  
    24.  
    25. void RadioButtonDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    26. {
    27. if(qVariantCanConvert<bool>(index.data())){
    28. QRadioButton* radio = static_cast<QRadioButton*>(editor);
    29. bool value = index.data().toBool();
    30. radio->setChecked(value);
    31. }
    32. };
    33.  
    34. void RadioButtonDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    35. {
    36. Q_UNUSED(model);
    37. QRadioButton* radio = static_cast<QRadioButton*>(editor);
    38. qDebug() << radio->isChecked(); // returns true and false per click
    39. model->setData(index, radio->isChecked());
    40. };
    41.  
    42.  
    43.  
    44. delegate.h
    45.  
    46. class RadioButtonDelegate : public QItemDelegate
    47. {
    48. Q_OBJECT
    49.  
    50. public:
    51. RadioButtonDelegate(QObject *parent = 0) ;
    52.  
    53. virtual ~RadioButtonDelegate();
    54.  
    55. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    56.  
    57.  
    58.  
    59. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    60.  
    61. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
    62.  
    63. // private slots:
    64. // void emitCommitData(){
    65. // emit commitData(qobject_cast<QWidget *>(sender()));
    66. // }
    67.  
    68. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Dialog.h
    2. class dialog : public QDialog, public Ui::Dialog
    3. {
    4. Q_OBJECT
    5. public:
    6. explicit dialog(QWidget *parent = 0);
    7.  
    8. signals:
    9.  
    10. public slots:
    11.  
    12. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. dialog.cpp
    2. dialog::dialog(QWidget *parent) :
    3. QDialog(parent)
    4. {
    5. setupUi(this);
    6.  
    7. _model= new QStandardItemModel ();
    8.  
    9. RadioButtonDelegate* itemDelegate = new RadioButtonDelegate(listView);
    10. listView->setItemDelegate( itemDelegate );
    11.  
    12. listView->setModel(_model);
    13.  
    14. //creating and adding data to model
    15. item->setText("testing");
    16.  
    17. item1->setText("testing1");
    18.  
    19. _model->appendRow(item);
    20. _model->appendRow(item1);
    21. }
    To copy to clipboard, switch view to plain text mode 

    Thank u all in advance.
    Last edited by phillip_Qt; 5th July 2013 at 07:50.
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: adding radio button in listview

    The editor is only used when the given cell is being edited.

    You might have to overwrite protected void drawCheck() to delegate to the style's radio button drawing instead of the checkbox drawing.
    (assuming your on/off value is presented using the check state role)

    Cheers,
    _

  3. #3
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding radio button in listview

    Quote Originally Posted by anda_skoa View Post
    The editor is only used when the given cell is being edited.

    You might have to overwrite protected void drawCheck() to delegate to the style's radio button drawing instead of the checkbox drawing.
    (assuming your on/off value is presented using the check state role)

    Cheers,
    _
    Thanks Anda_skoa.

    Do u mean i should override drawCheck() instead of createEditor()? Do i have to implement paint() method also inside my delegate class?
    Cheers,
    Phillip



    --- Please post the solution you got to solve your problem. It may help others.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: adding radio button in listview

    Maybe in addition to createEditor, not sure.

    I don't think you need to implement paint itself, I think it calls the draw methods so to make it easier to only customize certain things.

    Haven't tried anything myself, this was mostly where I would look first

    Cheers,
    _

  5. #5
    Join Date
    Oct 2007
    Posts
    201
    Thanks
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding radio button in listview

    Dear All,

    Please help me. I'm stuck with this problem. I'm not sure whether I've to draw a radio button in delegate class paint event or to create in seteditor event.

    I want to create radiobutton in listview dynamically accoring to the input. E.g. If i gie a input string "row1" listview should add a row with raoibutton having text "row1".

    Please help me.

Similar Threads

  1. how to check radio button
    By tommy in forum Newbie
    Replies: 2
    Last Post: 24th February 2012, 02:50
  2. Coloring of Radio Button
    By jogeshwarakundi in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 22nd July 2008, 15:05
  3. QTreeWidgetItem + radio Button
    By desch in forum Qt Programming
    Replies: 5
    Last Post: 14th November 2007, 14:15
  4. Undoing radio button checks.
    By Doug Broadwell in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2007, 09:12
  5. Paint XP radio button to pixmap
    By Ben.Hines in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2006, 21:15

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.