Results 1 to 5 of 5

Thread: QStyle vs Qt::BackgroundColorRole

  1. #1
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Exclamation QStyle vs Qt::BackgroundColorRole

    I just make a my custom style. In my QStyle, I fillrec itemviewrow on drawPrimitive function. Oh, yes I use this style just for my QTreeView..

    Qt Code:
    1. model=new MyModel();
    2. ui->treeView->setModel(model);
    3. ui->treeView->setStyle(new TreeStyle());
    To copy to clipboard, switch view to plain text mode 

    In MyModel I tried set backgroud color for a row with Qt::BackgroundColorRole. But I don't understand why not work. If I remove setStyle from my treeView, Qt::BackgroundColorRole will work..

    Qt Code:
    1. //TReeStyle COde
    2. void TreeStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option,
    3. QPainter* painter, const QWidget* widget) const
    4. {
    5. if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
    6. {
    7. painter->fillRect(option->rect, option->palette.highlight());
    8. }
    9. .......................
    10. .......................
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //MyModel Code
    2. if (role == Qt::BackgroundColorRole)
    3. {
    4. .......................
    5. .......................
    6. return qVariantFromValue(QColor(255, 186, 186));
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    This is normal ? I can't use booth QStyle and Model for QTreeView ?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle vs Qt::BackgroundColorRole

    sure you can. sounds like a bug in your style implementation.

  3. #3
    Join Date
    May 2009
    Location
    Gorontalo
    Posts
    200
    Thanks
    20
    Thanked 5 Times in 5 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Question Re: QStyle vs Qt::BackgroundColorRole

    Oh....
    this is my code. Please helpme for fix it. Btw, I use this code for testing only...


    treestyle.cpp
    Qt Code:
    1. #include <QPainter>
    2. #include <QStyleOption>
    3. #include <QWidget>
    4. #include "treestyle.h"
    5.  
    6. TreeStyle::TreeStyle() : QProxyStyle()
    7. {
    8. }
    9.  
    10. TreeStyle::~TreeStyle()
    11. {
    12. }
    13.  
    14. void TreeStyle::drawPrimitive(PrimitiveElement element, const QStyleOption* option,
    15. QPainter* painter, const QWidget* widget) const
    16. {
    17. if (element == PE_PanelItemViewRow || element == PE_PanelItemViewItem)
    18. {
    19. painter->fillRect(option->rect, option->palette.highlight());
    20. }
    21. else
    22. {
    23. QProxyStyle::drawPrimitive(element, option, painter, widget);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 


    mymodel.cpp
    Qt Code:
    1. #include "mymodel.h"
    2.  
    3. MyModel::MyModel(QObject *parent)
    4. {
    5. }
    6.  
    7. QVariant MyModel::data(const QModelIndex &index, int role) const
    8. {
    9. if (role == Qt::BackgroundColorRole)
    10. {
    11. return qVariantFromValue(QColor(255, 186, 186));
    12. }
    13. return QStandardItemModel::data(index, role);
    14. }
    To copy to clipboard, switch view to plain text mode 


    dialog.cpp
    Qt Code:
    1. model=new MyModel();
    2. model->setColumnCount(2);
    3. ..........
    4. ............
    5. model->setHorizontalHeaderItem(0, new QStandardItem("Type"));
    6. model->setHorizontalHeaderItem(1, new QStandardItem("Uang"));
    7.  
    8. ui->treeView->setModel(model);
    9. ui->treeView->setAlternatingRowColors(true);
    10. ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    11. ui->treeView->header()->setMovable(false);
    12. ui->treeView->setStyle(new TreeStyle());
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QStyle vs Qt::BackgroundColorRole

    well, I don't know what you want your style to do, so hard to say what is wrong with it.

    A fix might be to first call the base class (or proxy style) and let it paint and then paint your stuff over it.

    (You can see in Qt's QCommonStyle implementation (the source is available, after all) that the code you override (and no longer execute) is responsible for painting the background in alternating row colors.
    see: src/gui/styles/qcommonstyle.cpp

    HTH

  5. #5
    Join Date
    Oct 2007
    Location
    Calgary area
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QStyle vs Qt::BackgroundColorRole

    The QColor() constructor doesn't return anything so qVariantFromValue() won't work. Try this instead:

    return QVariant( QColor(255, 186, 186) );

Similar Threads

  1. Replies: 0
    Last Post: 20th January 2010, 19:51
  2. ComboBox QStyle
    By deMarco in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2010, 17:18
  3. how to rewrite QStyle
    By trytoremeber963 in forum Newbie
    Replies: 3
    Last Post: 28th April 2009, 08:18
  4. name of current QStyle
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2007, 20:02
  5. Using QStyle and StyleSheet
    By informatics in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2007, 16:46

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.