Results 1 to 9 of 9

Thread: QColorDialog always return 0 as a result

  1. #1
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default QColorDialog always return 0 as a result

    Hey @all,

    within my sublacc of QItemDelegate, the QColorDialog returns always 0 when i want to get the result by using:
    Qt Code:
    1. void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    2. const QModelIndex &index) const {
    3. QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
    4. if(pDialog) {
    5. int resultCode = pDialog->result();
    6. qDebug() << "Result Code : " << resultCode;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    It doesn't matter if i press "OK" or "Cancel", the result is ever 0.

    Hope anybody could help

    Best Regards
    NoRulez

  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: QColorDialog always return 0 as a result

    What exactly do you need result() for?
    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
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QColorDialog always return 0 as a result

    In "createEditor()" I return a QColorDialog object, and set the inital data in "setEditorData()".
    Now in the "setModelData()" I want to write the new color, so i need the "result()" to check if the user pressed the "OK" or the "Cancel" button,
    because only if the user has pressed the "OK" button I want to write the "currentColor()" back to the model.
    "selectedColor()" always returns "InvalidColor".


    Best Regards
    NoRulez

  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: QColorDialog always return 0 as a result

    Quote Originally Posted by NoRulez View Post
    In "createEditor()" I return a QColorDialog object
    Can you show a screenshot of how does that look? I somehow cannot imagine a QColorDialog inside a cell of a table...
    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. #5
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QColorDialog always return 0 as a result

    It's also an normal QColorDialog...I've attached a screenshot.
    So when i double click on a cell, the QColorDialog appears. Know i want to save the data/color when the user press the "OK" button, but the "result()" function always returns 0 which is QDialog::rejected, but i need QDialog::Accepted. "result()" returns ever 0, no matter which button was pressed.

    Could you help me?
    Best Regards
    Attached Images Attached Images

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QColorDialog always return 0 as a result

    From QDialog::result reference : Returns the modal dialog's result code, Accepted or Rejected.
    Show us how QColorDialog is invoked.

  7. #7
    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: QColorDialog always return 0 as a result

    Quote Originally Posted by NoRulez View Post
    It's also an normal QColorDialog...I've attached a screenshot.
    So how is that related to the delegate?

    So when i double click on a cell, the QColorDialog appears.
    That's not what QAbstractItemDelegate::createEditor() is for.
    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. #8
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QColorDialog always return 0 as a result

    @wysota: Why not?
    Returns the editor to be used for editing the data item with the given index. Note that the index contains information about the model being used. The editor's parent widget is specified by parent, and the item options by option.
    The base implementation returns 0. If you want custom editing you will need to reimplement this function.
    The returned editor widget should have Qt::StrongFocus; otherwise, QMouseEvents received by the widget will propagate to the view. The view's background will shine through unless the editor paints its own background (e.g., with setAutoFillBackground()).
    So the QColorDialog inherits QDialog, and this inherits QWidget... It's a QWidget, so why I can't use them?
    I view color bars in the tableview, on double click, the QColorDialog appears and I can change the color...
    The only problem I have is to not get the Accepted result from the "result()" function.

    @Lesiok: Here is the code of the delegate:
    Qt Code:
    1. QWidget* MyDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem &option,
    2. const QModelIndex &index) const {
    3. if (index.column() == 4) {
    4. return new QColorDialog(parent);
    5. }
    6. return QItemDelegate::createEditor(parent, option, index);
    7. }
    8.  
    9. void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
    10. QColor baseColor = QColor(index.sibling(index.row(), 5).data().toInt(),
    11. index.sibling(index.row(), 6).data().toInt(),
    12. index.sibling(index.row(), 7).data().toInt(),
    13. index.sibling(index.row(), 8).data().toInt());
    14. QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
    15. if(pDialog)
    16. pDialog->setCurrentColor(baseColor);
    17. }
    18.  
    19. void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    20. const QModelIndex &index) const {
    21. QColorDialog *pDialog = qobject_cast<QColorDialog*>(editor);
    22. if(pDialog) {
    23. int resultCode = pDialog->result();
    24. qDebug() << "Result Code : " << resultCode;
    25. // Here i only get QDialog::Rejected, no matter if the "OK" or the "Cancel" button was pressed
    26. if(resultCode == QDialog::Accepted) {
    27. QColor currentColor = pDialog->currentColor();
    28. model->setData(index.model()->index(index.row(), 5), currentColor.red(), Qt::EditRole);
    29. model->setData(index.model()->index(index.row(), 6), currentColor.green(), Qt::EditRole);
    30. model->setData(index.model()->index(index.row(), 7), currentColor.blue(), Qt::EditRole);
    31. model->setData(index.model()->index(index.row(), 8), currentColor.alpha(), Qt::EditRole);
    32. }
    33. }
    34. }
    35.  
    36. void MyDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    37. const QModelIndex &index) const {
    38. if(option.state & QStyle::State_Selected)
    39. painter->fillRect(option.rect, option.palette.highlight());
    40.  
    41. QColor baseColor(index.sibling(index.row(), 5).data().toInt(),
    42. index.sibling(index.row(), 6).data().toInt(),
    43. index.sibling(index.row(), 7).data().toInt(),
    44. index.sibling(index.row(), 8).data().toInt());
    45.  
    46. painter->save();
    47. painter->setBrush(baseColor);
    48. painter->setPen(Qt::black);
    49. painter->drawRect(option.rect.x()+2, option.rect.y()+2,option.rect.width()-5, option.rect.height()-5);
    50. painter->restore();
    51. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by NoRulez; 17th October 2009 at 11:54.

  9. #9
    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: QColorDialog always return 0 as a result

    Quote Originally Posted by NoRulez View Post
    @wysota: Why not?
    Because createEditor() is to return a persistent widget that takes place of the item for the time of editing. This is not the case here thus you are using the mechanism incorrectly.

    Instead connect to the view's doubleClicked() signal and upon that signal call QColorDialog::getColor() and afterwards set the model's data using QAbstractItemModel::setData().
    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.


Similar Threads

  1. Replies: 12
    Last Post: 22nd March 2009, 11:22
  2. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  3. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  4. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  5. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18

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.