Results 1 to 1 of 1

Thread: QItemDelegate with QRadioButton improper behaviour if QStandarditemModel has one row

  1. #1
    Join Date
    Jun 2010
    Posts
    36
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default QItemDelegate with QRadioButton improper behaviour if QStandarditemModel has one row

    This is somewhat hard to explain. This is the deal:
    I have a QStandarditemModel that needs a single selection some specific column, therefore I inserted a radio button on every row. Only one row must always be selected, just like the radio button group behavior.

    I did a QItemDelegate with a QRadioButton editor and magically I got the radio button behavior in the column, just like I needed. But If I only have one row on the model, I get a check box behavior, meaning its possible to select and unselect the radio button each time I click it. I don't really know if this is a normal, in that case what can I do to always have one radio button selected even if its alone.

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

    Could it be a bug?
    Last edited by toglia3d; 30th September 2010 at 00:11.

Similar Threads

  1. Improper size issue in QWidget::sizeHint()
    By George Neil in forum Qt Programming
    Replies: 2
    Last Post: 23rd September 2009, 06:37
  2. Improper execution!
    By dreamh4k in forum Newbie
    Replies: 1
    Last Post: 19th September 2009, 03:08
  3. problem in QRadioButton
    By wagmare in forum Qt Programming
    Replies: 4
    Last Post: 16th March 2009, 11:37
  4. QRadioButton
    By sonuani in forum Newbie
    Replies: 1
    Last Post: 20th February 2008, 10:30
  5. QRadioButton
    By sabeesh in forum Qt Programming
    Replies: 3
    Last Post: 8th October 2007, 08:25

Tags for this Thread

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.