Can QComboBox items text consist of 2 colors?
For example for string "Elon Musk":
- "Elon" text color is red;
- "Musk" text color is green;
Thanks in advance for any help you can provide
c++ qt qcombobox
add a comment |
For example for string "Elon Musk":
- "Elon" text color is red;
- "Musk" text color is green;
Thanks in advance for any help you can provide
c++ qt qcombobox
1
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39
add a comment |
For example for string "Elon Musk":
- "Elon" text color is red;
- "Musk" text color is green;
Thanks in advance for any help you can provide
c++ qt qcombobox
For example for string "Elon Musk":
- "Elon" text color is red;
- "Musk" text color is green;
Thanks in advance for any help you can provide
c++ qt qcombobox
c++ qt qcombobox
asked Nov 23 '18 at 13:49
IgorIgor
283
283
1
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39
add a comment |
1
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39
1
1
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39
add a comment |
3 Answers
3
active
oldest
votes
As an alternative approach to using delegates I would use a QLabel with rich text (HTML encoded) to color the combo box item text. I also need to implement an event filter to handle clicking (selecting) "custom" items. The following example demonstrates how to do it:
class Filter : public QObject
{
public:
Filter(QComboBox *combo)
:
m_combo(combo)
{}
protected:
bool eventFilter(QObject *watched, QEvent * event) override
{
auto lbl = qobject_cast<QLabel *>(watched);
if (lbl && event->type() == QEvent::MouseButtonRelease)
{
// Set the current index
auto model = m_combo->model();
for (int r = 0; r < model->rowCount(); ++r)
{
if (m_combo->view()->indexWidget(model->index(r, 0)) == lbl)
{
m_combo->setCurrentIndex(r);
break;
}
}
m_combo->hidePopup();
}
return false;
}
private:
QComboBox *m_combo;
};
And here is how to add "colored" items into combo box and handle them:
QComboBox box;
box.setEditable(true);
Filter filter(&box);
// Add two items: regular and colored.
box.addItem("A regular item");
box.addItem("Elon Musk");
// Handle the colored item. Color strings using HTML tags.
QLabel lbl("<font color="red">Elon </font><font color="green">Musk</font>", &box);
lbl.setAutoFillBackground(true);
lbl.installEventFilter(&filter);
box.view()->setIndexWidget(box.model()->index(1, 0), &lbl);
box.show();
add a comment |
Yes, it can. In fact you can do whatever you want in there using QItemDelegate. Inside the delegate you can do all the crazy stuff you want, which not only includes coloring, but also buttons and other controls.
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegateis recommended by the doc instead ofQItemDelegate.
– ymoreau
Nov 23 '18 at 15:30
add a comment |
You can play with Qt::ItemDataRole to provide customisations. For this particular case -
#include <QApplication>
#include <QComboBox>
#include <QColor>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
QComboBox box;
box.addItem("Elon");
box.addItem("Musk");
box.setItemData(0, QColor(Qt::red), Qt::ForegroundRole);
box.setItemData(1, QColor(Qt::green), Qt::ForegroundRole);
box.show();
return a.exec();
}
Screenshots for reference -


So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447944%2fcan-qcombobox-items-text-consist-of-2-colors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
As an alternative approach to using delegates I would use a QLabel with rich text (HTML encoded) to color the combo box item text. I also need to implement an event filter to handle clicking (selecting) "custom" items. The following example demonstrates how to do it:
class Filter : public QObject
{
public:
Filter(QComboBox *combo)
:
m_combo(combo)
{}
protected:
bool eventFilter(QObject *watched, QEvent * event) override
{
auto lbl = qobject_cast<QLabel *>(watched);
if (lbl && event->type() == QEvent::MouseButtonRelease)
{
// Set the current index
auto model = m_combo->model();
for (int r = 0; r < model->rowCount(); ++r)
{
if (m_combo->view()->indexWidget(model->index(r, 0)) == lbl)
{
m_combo->setCurrentIndex(r);
break;
}
}
m_combo->hidePopup();
}
return false;
}
private:
QComboBox *m_combo;
};
And here is how to add "colored" items into combo box and handle them:
QComboBox box;
box.setEditable(true);
Filter filter(&box);
// Add two items: regular and colored.
box.addItem("A regular item");
box.addItem("Elon Musk");
// Handle the colored item. Color strings using HTML tags.
QLabel lbl("<font color="red">Elon </font><font color="green">Musk</font>", &box);
lbl.setAutoFillBackground(true);
lbl.installEventFilter(&filter);
box.view()->setIndexWidget(box.model()->index(1, 0), &lbl);
box.show();
add a comment |
As an alternative approach to using delegates I would use a QLabel with rich text (HTML encoded) to color the combo box item text. I also need to implement an event filter to handle clicking (selecting) "custom" items. The following example demonstrates how to do it:
class Filter : public QObject
{
public:
Filter(QComboBox *combo)
:
m_combo(combo)
{}
protected:
bool eventFilter(QObject *watched, QEvent * event) override
{
auto lbl = qobject_cast<QLabel *>(watched);
if (lbl && event->type() == QEvent::MouseButtonRelease)
{
// Set the current index
auto model = m_combo->model();
for (int r = 0; r < model->rowCount(); ++r)
{
if (m_combo->view()->indexWidget(model->index(r, 0)) == lbl)
{
m_combo->setCurrentIndex(r);
break;
}
}
m_combo->hidePopup();
}
return false;
}
private:
QComboBox *m_combo;
};
And here is how to add "colored" items into combo box and handle them:
QComboBox box;
box.setEditable(true);
Filter filter(&box);
// Add two items: regular and colored.
box.addItem("A regular item");
box.addItem("Elon Musk");
// Handle the colored item. Color strings using HTML tags.
QLabel lbl("<font color="red">Elon </font><font color="green">Musk</font>", &box);
lbl.setAutoFillBackground(true);
lbl.installEventFilter(&filter);
box.view()->setIndexWidget(box.model()->index(1, 0), &lbl);
box.show();
add a comment |
As an alternative approach to using delegates I would use a QLabel with rich text (HTML encoded) to color the combo box item text. I also need to implement an event filter to handle clicking (selecting) "custom" items. The following example demonstrates how to do it:
class Filter : public QObject
{
public:
Filter(QComboBox *combo)
:
m_combo(combo)
{}
protected:
bool eventFilter(QObject *watched, QEvent * event) override
{
auto lbl = qobject_cast<QLabel *>(watched);
if (lbl && event->type() == QEvent::MouseButtonRelease)
{
// Set the current index
auto model = m_combo->model();
for (int r = 0; r < model->rowCount(); ++r)
{
if (m_combo->view()->indexWidget(model->index(r, 0)) == lbl)
{
m_combo->setCurrentIndex(r);
break;
}
}
m_combo->hidePopup();
}
return false;
}
private:
QComboBox *m_combo;
};
And here is how to add "colored" items into combo box and handle them:
QComboBox box;
box.setEditable(true);
Filter filter(&box);
// Add two items: regular and colored.
box.addItem("A regular item");
box.addItem("Elon Musk");
// Handle the colored item. Color strings using HTML tags.
QLabel lbl("<font color="red">Elon </font><font color="green">Musk</font>", &box);
lbl.setAutoFillBackground(true);
lbl.installEventFilter(&filter);
box.view()->setIndexWidget(box.model()->index(1, 0), &lbl);
box.show();
As an alternative approach to using delegates I would use a QLabel with rich text (HTML encoded) to color the combo box item text. I also need to implement an event filter to handle clicking (selecting) "custom" items. The following example demonstrates how to do it:
class Filter : public QObject
{
public:
Filter(QComboBox *combo)
:
m_combo(combo)
{}
protected:
bool eventFilter(QObject *watched, QEvent * event) override
{
auto lbl = qobject_cast<QLabel *>(watched);
if (lbl && event->type() == QEvent::MouseButtonRelease)
{
// Set the current index
auto model = m_combo->model();
for (int r = 0; r < model->rowCount(); ++r)
{
if (m_combo->view()->indexWidget(model->index(r, 0)) == lbl)
{
m_combo->setCurrentIndex(r);
break;
}
}
m_combo->hidePopup();
}
return false;
}
private:
QComboBox *m_combo;
};
And here is how to add "colored" items into combo box and handle them:
QComboBox box;
box.setEditable(true);
Filter filter(&box);
// Add two items: regular and colored.
box.addItem("A regular item");
box.addItem("Elon Musk");
// Handle the colored item. Color strings using HTML tags.
QLabel lbl("<font color="red">Elon </font><font color="green">Musk</font>", &box);
lbl.setAutoFillBackground(true);
lbl.installEventFilter(&filter);
box.view()->setIndexWidget(box.model()->index(1, 0), &lbl);
box.show();
answered Nov 23 '18 at 15:38
vahanchovahancho
15.4k32430
15.4k32430
add a comment |
add a comment |
Yes, it can. In fact you can do whatever you want in there using QItemDelegate. Inside the delegate you can do all the crazy stuff you want, which not only includes coloring, but also buttons and other controls.
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegateis recommended by the doc instead ofQItemDelegate.
– ymoreau
Nov 23 '18 at 15:30
add a comment |
Yes, it can. In fact you can do whatever you want in there using QItemDelegate. Inside the delegate you can do all the crazy stuff you want, which not only includes coloring, but also buttons and other controls.
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegateis recommended by the doc instead ofQItemDelegate.
– ymoreau
Nov 23 '18 at 15:30
add a comment |
Yes, it can. In fact you can do whatever you want in there using QItemDelegate. Inside the delegate you can do all the crazy stuff you want, which not only includes coloring, but also buttons and other controls.
Yes, it can. In fact you can do whatever you want in there using QItemDelegate. Inside the delegate you can do all the crazy stuff you want, which not only includes coloring, but also buttons and other controls.
answered Nov 23 '18 at 14:22
The Quantum PhysicistThe Quantum Physicist
11.5k64596
11.5k64596
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegateis recommended by the doc instead ofQItemDelegate.
– ymoreau
Nov 23 '18 at 15:30
add a comment |
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegateis recommended by the doc instead ofQItemDelegate.
– ymoreau
Nov 23 '18 at 15:30
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
Thank you for response. I'm not sure where exactly to change colors, but I will be searching in direction of sub-classing of QStyledItemDelegate and reimplementing of paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) method. So changing "option" I hope to solve my problem
– Igor
Nov 23 '18 at 14:41
QStyledItemDelegate is recommended by the doc instead of QItemDelegate.– ymoreau
Nov 23 '18 at 15:30
QStyledItemDelegate is recommended by the doc instead of QItemDelegate.– ymoreau
Nov 23 '18 at 15:30
add a comment |
You can play with Qt::ItemDataRole to provide customisations. For this particular case -
#include <QApplication>
#include <QComboBox>
#include <QColor>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
QComboBox box;
box.addItem("Elon");
box.addItem("Musk");
box.setItemData(0, QColor(Qt::red), Qt::ForegroundRole);
box.setItemData(1, QColor(Qt::green), Qt::ForegroundRole);
box.show();
return a.exec();
}
Screenshots for reference -


So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
add a comment |
You can play with Qt::ItemDataRole to provide customisations. For this particular case -
#include <QApplication>
#include <QComboBox>
#include <QColor>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
QComboBox box;
box.addItem("Elon");
box.addItem("Musk");
box.setItemData(0, QColor(Qt::red), Qt::ForegroundRole);
box.setItemData(1, QColor(Qt::green), Qt::ForegroundRole);
box.show();
return a.exec();
}
Screenshots for reference -


So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
add a comment |
You can play with Qt::ItemDataRole to provide customisations. For this particular case -
#include <QApplication>
#include <QComboBox>
#include <QColor>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
QComboBox box;
box.addItem("Elon");
box.addItem("Musk");
box.setItemData(0, QColor(Qt::red), Qt::ForegroundRole);
box.setItemData(1, QColor(Qt::green), Qt::ForegroundRole);
box.show();
return a.exec();
}
Screenshots for reference -


You can play with Qt::ItemDataRole to provide customisations. For this particular case -
#include <QApplication>
#include <QComboBox>
#include <QColor>
int main(int argc, char *argv)
{
QApplication a(argc, argv);
QComboBox box;
box.addItem("Elon");
box.addItem("Musk");
box.setItemData(0, QColor(Qt::red), Qt::ForegroundRole);
box.setItemData(1, QColor(Qt::green), Qt::ForegroundRole);
box.show();
return a.exec();
}
Screenshots for reference -


edited Nov 23 '18 at 14:30
answered Nov 23 '18 at 14:23
Anmol GautamAnmol Gautam
4621517
4621517
So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
add a comment |
So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
So detailed answer, thanks a lot. But gave a confusing description of my question, sorry for that. I meant: "Elon Musk" is the one item (not two)
– Igor
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
But the question was about coloring strings in a single item. I.e. "Elon Musk" is the text in a single item, where "Elon (red) Musk (green)".
– vahancho
Nov 23 '18 at 14:36
1
1
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
@Igor you can have a look at stackoverflow.com/questions/2476581/…
– Anmol Gautam
Nov 23 '18 at 14:43
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53447944%2fcan-qcombobox-items-text-consist-of-2-colors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can have a look at this answer : stackoverflow.com/a/2039745/6165833
– ymoreau
Nov 23 '18 at 15:39