Can QComboBox items text consist of 2 colors?












4















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










share|improve this question


















  • 1





    You can have a look at this answer : stackoverflow.com/a/2039745/6165833

    – ymoreau
    Nov 23 '18 at 15:39
















4















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










share|improve this question


















  • 1





    You can have a look at this answer : stackoverflow.com/a/2039745/6165833

    – ymoreau
    Nov 23 '18 at 15:39














4












4








4








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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












3 Answers
3






active

oldest

votes


















1














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();





share|improve this answer































    1














    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.






    share|improve this answer
























    • 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



















    1














    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 -



    enter image description hereenter image description here






    share|improve this answer


























    • 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











    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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();





    share|improve this answer




























      1














      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();





      share|improve this answer


























        1












        1








        1







        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();





        share|improve this answer













        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();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 15:38









        vahanchovahancho

        15.4k32430




        15.4k32430

























            1














            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.






            share|improve this answer
























            • 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
















            1














            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.






            share|improve this answer
























            • 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














            1












            1








            1







            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.






            share|improve this answer













            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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            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











            • QStyledItemDelegate is recommended by the doc instead of QItemDelegate.

              – 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











            • QStyledItemDelegate is recommended by the doc instead of QItemDelegate.

              – 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











            1














            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 -



            enter image description hereenter image description here






            share|improve this answer


























            • 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
















            1














            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 -



            enter image description hereenter image description here






            share|improve this answer


























            • 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














            1












            1








            1







            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 -



            enter image description hereenter image description here






            share|improve this answer















            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 -



            enter image description hereenter image description here







            share|improve this answer














            share|improve this answer



            share|improve this answer








            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



















            • 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


















            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Langue