Results 1 to 13 of 13

Thread: How to handle QTableView's behaviour

  1. #1
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to handle QTableView's behaviour

    Hello,

    I have a QTableWidget item and I'm using delegates with it. One particular case, that proves useful to demonstrate my problem, is with ComboboxDelegate. If I assign a combobox delegate to a certain column of the grid, it goes nice and dandy but on runtime, I need to double click it to start behaving like a combobox.

    To better show my problem, here is a picture;
    aGL6e.pnghttp://i.imgur.com/aGL6e.png

    So, what I want to happen is that, right when I load that dialog window, for the QTableWidget to make that particular column have a dropDown frame (with the arrow-ey button).

    I've tried with
    Qt Code:
    1. this->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::SelectedClicked);
    To copy to clipboard, switch view to plain text mode 
    and while it makes it appear as soon as I highlight it, it still doesn't quite feels right.
    Last edited by alitoh; 11th May 2011 at 14:54.

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

    Default Re: How to handle QTableView's behaviour

    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
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to handle QTableView's behaviour

    So ... I was finally able to fix that AND create a proper Colorlist delegate for my QTableView.

    Now I want to fix an issue that the QT demos also have; once the delegate's editor closes, the only thing that remains in the cell is the colour's name, not the square with the colour.

    As always, I attack a picture to explain it better than I can:

    beforeafter.png
    I managed to figure out the fault is with
    Qt Code:
    1. void ColorlistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. QItemDelegate::paint(painter, option, index);
    4. }
    To copy to clipboard, switch view to plain text mode 
    , but I can't quite grasp how to figure out the cells' ( X , Y ) position in relation to the QTableView.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to handle QTableView's behaviour

    Quote Originally Posted by alitoh View Post
    but I can't quite grasp how to figure out the cells' ( X , Y ) position in relation to the QTableView.
    See QStyleOptionViewItem::rect

    In your paint function, get the color name from your model, convert it to a QColor and call QPainter::fillRect().
    Last edited by norobro; 13th May 2011 at 03:06. Reason: typo

  5. #5
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to handle QTableView's behaviour

    I can get that much done, but now I can't get the cell to change colour/colour name past the initial setting (whichever it might be). In the previous picture, for example, if I select anything other than Aliceblue, I still get aliceblue on the cell, regardless of my choice in the editor. This is because I changed the default
    Qt Code:
    1. QItemDelegate::paint(painter, option, index);
    To copy to clipboard, switch view to plain text mode 
    with something like this
    Qt Code:
    1. QColor color = index.data().Color;
    2.  
    3. painter->setBrush(Qt::SolidPattern);
    4. painter->setBrush(color);
    5. painter->setPen(thisPen);
    6. painter->drawRect(option.rect.x() + 5, option.rect.y() + 5, 20, 20);
    7. painter->drawText(option.rect.x() + 27, option.rect.y() + 20, color.);
    To copy to clipboard, switch view to plain text mode 
    What does QItemDelegate::paint do in order to handle parameters like it does? I can only find a qitemdelegate.h, but not the implementation.
    Last edited by alitoh; 13th May 2011 at 13:43.

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

    Default Re: How to handle QTableView's behaviour

    You need to implement setEditorData() in the delegate if you want the editor to update its contents on model data change.
    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.


  7. #7
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to handle QTableView's behaviour

    Quote Originally Posted by wysota View Post
    You need to implement setEditorData() in the delegate if you want the editor to update its contents on model data change.
    I already have ColorlistDelegate::setEditorData in my delegate.

    my 3 functions are:

    Qt Code:
    1. void ColorlistDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. //If you remove this line and comment everything else, you'll get the same result as the demo examples. If you dont, you get a colored rect, but not updating accordingly
    4. //QItemDelegate::paint(painter, option, index);
    5. QPen thisPen;
    6. QColor color = index.data().Color;
    7.  
    8. painter->setBrush(Qt::SolidPattern);
    9. painter->setBrush(color);
    10. painter->setPen(thisPen);
    11. painter->drawRect(option.rect.x() + 5, option.rect.y() + 5, 20, 20);
    12. painter->drawText(option.rect.x() + 27, option.rect.y() + 20, color.name());
    13. }
    14.  
    15. QWidget *ColorlistDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    16. {
    17. QComboBox *combo = new QComboBox(parent);
    18. QString cadena = index.data().toString();
    19.  
    20. QStringList colorNames = QColor::colorNames();
    21. foreach (QString name, colorNames)
    22. {
    23. QPixmap pix(24, 24);
    24. pix.fill(QColor(name));
    25. combo->addItem(QIcon(pix), name, QColor(name));
    26. }
    27.  
    28. int indice = combo->findText(cadena);
    29. if (indice >= 0)
    30. {
    31. combo->setCurrentIndex(indice);
    32. }
    33.  
    34. // colorName. cadena;
    35. return combo;
    36. }
    37.  
    38. void ColorlistDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    39. {
    40. QString cadena = index.model()->data(index, Qt::DisplayRole).toString();
    41.  
    42. QComboBox *comboBox = qobject_cast<QComboBox *>(editor);
    43. int indice = comboBox->findText(cadena);
    44. if (indice >= 0)
    45. comboBox->setItemText(indice, cadena);
    46. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to handle QTableView's behaviour

    Do any of them get executed when the data in the model changes?
    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.


  9. #9
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to handle QTableView's behaviour

    Yes, they do. I just threw a bunch of breakpoints in the signatures and they all were processed.

    Also, QItemDelegate::paint(painter, option, index); DOES update properly, inside ColorlistDelegate::paint, so it must have something to do with me not doing something right in the paint function.

  10. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to handle QTableView's behaviour

    In paint() try:
    Qt Code:
    1. QColor color = index.data().value<QColor>();
    To copy to clipboard, switch view to plain text mode 

  11. The following user says thank you to norobro for this useful post:

    alitoh (13th May 2011)

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

    Default Re: How to handle QTableView's behaviour

    What's wrong is probably you querying the model for the default role when looking for the colour. Based on the rest of your code I can assume you use DisplayRole for keeping something else than the colour.
    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.


  13. #12
    Join Date
    Mar 2011
    Posts
    82
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to handle QTableView's behaviour

    Quote Originally Posted by norobro View Post
    In paint() try:
    Qt Code:
    1. QColor color = index.data().value<QColor>();
    To copy to clipboard, switch view to plain text mode 
    Great. Thanks a lot.

    Now the updating works, but the name is kinda messed up, for it shows it's hex value instead of the proper noun. Still, this is quite an improvement.
    Care to explain a little more what's going on in that line, because I'm barely keeping up with delegates, to be honest.

  14. #13
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to handle QTableView's behaviour

    Quote Originally Posted by alitoh View Post
    . . . I'm barely keeping up with delegates, to be honest.
    Looks to me like you are handling delegates pretty well

    Take a look at QVariant::value() in the docs. As long as the color name in your cell is recognized by Qt that statement will work. To see the list of Qt recognized color names use:
    Qt Code:
    1. qDebug() << QColor::colorNames();
    To copy to clipboard, switch view to plain text mode 
    My Debian system lists 148 colors.

    Regarding the text, use:
    Qt Code:
    1. painter->drawText(option.rect.x() + 27, option.rect.y() + 20, index.data().toString());
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. [QTableView] Custom selection behaviour
    By Rid in forum Qt Programming
    Replies: 3
    Last Post: 27th April 2012, 07:17
  2. QLineEdit behaviour
    By zgulser in forum Qt Programming
    Replies: 0
    Last Post: 9th July 2010, 14:37
  3. Handle KeyRelease or KeyPress on any row in QTableView
    By AbuYusuf in forum Qt Programming
    Replies: 2
    Last Post: 11th February 2010, 20:04
  4. Different behaviour of QApplication!!
    By Raajesh in forum Qt Programming
    Replies: 2
    Last Post: 30th June 2008, 17:00
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

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
  •  
Qt is a trademark of The Qt Company.