Results 1 to 15 of 15

Thread: Color ComboBox

  1. #1
    Join Date
    Apr 2008
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Color ComboBox

    Hi everybody,

    I would like to build a combobox for selecting colors, like the one used in MS Excel when you select for example the color of a line. I have already tested wysota's QwwColorComboBox. However, I would prefer the color to cover the whole width of the combobox and only a small part of it. Any hints?

    Thanks in advance...

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color ComboBox

    take a look at this example. I think it should help you.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2008
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox

    Thanks for the reply. However, in this case the color only occupies a small rectangle in the left side of the combobox. I would like the color to occupy the whole of the combobox.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color ComboBox

    try this
    Qt Code:
    1. QComboBox *cb = new QComboBox(this);
    2. const QStringList colorNames = QColor::colorNames();
    3. int index = 0;
    4. foreach (const QString &colorName, colorNames) {
    5. const QColor color(colorName);
    6. cb->addItem(colorName, color);
    7. const QModelIndex idx = cb->model()->index(index++, 0);
    8. cb->model()->setData(idx, color, Qt::BackgroundColorRole);
    9. }
    To copy to clipboard, switch view to plain text mode 
    hope you've got idea.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Color ComboBox

    Quote Originally Posted by ioannis View Post
    I would like to build a combobox for selecting colors, like the one used in MS Excel when you select for example the color of a line. I have already tested wysota's QwwColorComboBox. However, I would prefer the color to cover the whole width of the combobox and only a small part of it. Any hints?
    It's only a matter of using another item delegate. There is also an implementation of a colour combobox as a Qt Solution but I don't know if it covers the whole area of the widget or not.
    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.


  6. #6
    Join Date
    Apr 2008
    Posts
    12
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox

    Thanks wysota. Can you give me a minimal example?

    Thanks in advance.

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

    Default Re: Color ComboBox

    Something like:
    Qt Code:
    1. class Delegate : public QItemDelegate {
    2. //...
    3.  
    4. void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
    5. painter->fillRect(option.rect, qvariant_cast<QColor>(index.data(Qt::DecorationRole)));
    6. }
    7. };
    To copy to clipboard, switch view to plain text mode 
    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
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color ComboBox

    so, what is difference between your code and my? in both case current line have own background color.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    Default Re: Color ComboBox

    I don't modify the model I didn't say my code was better than yours, I have only shown how to make my color combobox display colors in a different way. Many ways may lead to the same result, there is no point in discussing the differences.
    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.


  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Color ComboBox

    Quote Originally Posted by wysota View Post
    I don't modify the model I didn't say my code was better than yours, I have only shown how to make my color combobox display colors in a different way. Many ways may lead to the same result, there is no point in discussing the differences.
    but any way you have to get colors from model
    ok, agree with this "Many ways may lead to the same result".
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #11
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox

    Hi,

    I tried the spirit's idea. It works fine for the menu but the selected option does not display the background color.
    How can I put the color in the background of the actual index?

    Thank you very much!

  12. #12
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox

    When the index changes I need to set the base with the color of the current index:

    Qt Code:
    1. QColor color = qvariant_cast<QColor>(model()->index(index, 0).data(Qt::BackgroundColorRole));
    2.  
    3. QPalette pal = palette();
    4. pal.setColor(QPalette::Base, color);
    5. setPalette(pal);
    To copy to clipboard, switch view to plain text mode 

    This works very well, I have the correct color in the right place... but, the highlight mess it up. I don't need the highlight since I'm working with colors.
    Is there a way to simple remove the highlight?

    I had tried this:

    Qt Code:
    1. pal.setColor(QPalette::Highlight, Qt::transparent);
    To copy to clipboard, switch view to plain text mode 

    but it's not transparent, it's white.

  13. #13
    Join Date
    Oct 2012
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox

    I did a workaround. The idea was make the highlight with the same color of the item.

    Connect the highlighted signal from QComboBox:

    Qt Code:
    1. ComboBoxColor( QWidget *parent = 0 ) : QComboBox( parent )
    2. {
    3. connect( this, SIGNAL(highlighted(int)), this, SLOT(slotHighlight(int)) );
    4. }
    To copy to clipboard, switch view to plain text mode 

    The slot will set the correct color for the current item:

    Qt Code:
    1. void ComboBoxColor::slotHighlight(int index)
    2. {
    3. const QStringList colorNames = QColor::colorNames();
    4. QColor color(colorNames.at(index));
    5.  
    6. QPalette palette = this->palette();
    7. palette.setColor(QPalette::Highlight, color);
    8. setPalette(palette);
    9. }
    To copy to clipboard, switch view to plain text mode 

  14. #14
    Join Date
    Mar 2016
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Color ComboBox

    spirit

    Default Re: Color ComboBox
    take a look at this example. I think it should help you.

    plzzzzzzzzzzz any body sent me that code iam in deep need to it

    whin i open the above link it dosnt open

  15. #15
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    474
    Thanks
    10
    Thanked 62 Times in 60 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Color ComboBox


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.