Results 1 to 9 of 9

Thread: Get checkbox of QStandardItem

  1. #1
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Get checkbox of QStandardItem

    I have a custom QComboBox using a model in which I set some QStandardItems. I made these items checkable:
    Qt Code:
    1. item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    To copy to clipboard, switch view to plain text mode 

    The problem is when I deploy my app for Android, the checkboxes are so small I cannot even click on them. That's why I would like to set a style for them, so my question is: how do I get the checkbox objects of a QStandardItem so I can call QWidget::setStyle() on them?

    Additional issue to be taken into account: the checkboxes do not always show up depending on the OS, so I'm using one of these workarounds:


  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get checkbox of QStandardItem

    There are no checkbox objects. The boxes are drawn by a delegate, so the easiest approach is to provide a custom delegate that will draw the boxes the way you want.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Get checkbox of QStandardItem

    Thanks for your answer.

    Ok then how do I find which image file Qt was using before, so I can paint it again in a bigger size? Looking into the source of QStyledItemDelegate did not provide any information about that.

    How can I paint an image in a delegate? The QStyledItemDelegate doc does not seem to cover painting images.

    And most importantly, how do I tell Qt that the painted image is the actual checkbox (so when I click it the item changes the value of its Qt::CheckStateRole)?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get checkbox of QStandardItem

    Have a look at QStylePainter. Regarding the second question, the delegate handles events for items in editorEvent() method, you have to implement a hit test there and update the model if you hit the checkbox.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    Neptilo (2nd December 2014)

  6. #5
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Get checkbox of QStandardItem

    Thanks. I'm playing around with QStylePainter right now. I'll let you know if I get to achieve what I want.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get checkbox of QStandardItem

    What you want is probably drawControl() and QStyle::CE_CheckBox.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Get checkbox of QStandardItem

    So how to use these?

    I'm not sure but I suspect this will paint the default icons in their default size, so much too small in the case of Android. Or are you thinking of a way to resize them?

    Anyway I'm experimenting with the advice in your previous post now and I feel like it will do fine.

  9. #8
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Default Re: Get checkbox of QStandardItem

    I managed to get by with the paint() method, but I'm kinda stuck with editorEvent(). Edit: I had a look at QItemDelegate source and found how to do it:
    Qt Code:
    1. bool CheckableItemDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
    2. {
    3. Q_UNUSED(option);
    4. if (event->type() == QEvent::MouseButtonRelease) {
    5. QVariant value = index.data(Qt::CheckStateRole);
    6. Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked
    7. ? Qt::Unchecked : Qt::Checked);
    8. return model->setData(index, state, Qt::CheckStateRole);
    9. } else
    10. return false;
    11. }
    To copy to clipboard, switch view to plain text mode 

    This will intercept all mouse clicks on the item (and I'm ok with it).
    Last edited by Neptilo; 2nd December 2014 at 16:49.

  10. #9
    Join Date
    Dec 2012
    Posts
    9
    Thanks
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Android

    Red face Re: Get checkbox of QStandardItem

    Update:

    I think I nailed it, hinted by this post.

    So I subclassed QProxyStyle as AndroidStyle and reimplemented QProxyStyle::pixelMetric this way:

    Qt Code:
    1. int AndroidStyle::pixelMetric(PixelMetric which,
    2. const QStyleOption *option,
    3. const QWidget *widget) const
    4. {
    5. int metric = QProxyStyle::pixelMetric(which, option, widget);
    6. switch (which) {
    7. case PM_IndicatorWidth:
    8. case PM_IndicatorHeight:
    9. return 2*metric;
    10. default:
    11. return metric;
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    and I set this style in the main function like this:
    Qt Code:
    1. QApplication a(argc, argv);
    2. a.setStyle(new AndroidStyle);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Reimplemented QStandardItem
    By Miga in forum Qt Programming
    Replies: 6
    Last Post: 3rd April 2012, 14:48
  2. Replies: 4
    Last Post: 1st June 2011, 14:54
  3. setObjectName for QStandardItem
    By abk883 in forum Newbie
    Replies: 1
    Last Post: 20th May 2010, 18:20
  4. UTF8 and QStandardItem?
    By alexandernst in forum Newbie
    Replies: 17
    Last Post: 26th July 2009, 18:29
  5. QStandardItem question.
    By alexandernst in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2009, 01:01

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.