Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to set color

How to set color

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • shantoS Offline
    shantoS Offline
    shanto
    wrote on last edited by
    #1

    I have a list of cars, which have different colors. When the user enters a color I want all cars with the same color to be highlighted with that.
    I have tried something like this, but it doesn't work.
    string color = colorEdt->text().toStdString();
    ....
    item->setData(Qt::BackgroundRole,QColor::setNamedColor(color));
    Could you give me some tips?

    item->setData(Qt::BackgroundRole,QColor::setNamedColor(color));

    kshegunovK 1 Reply Last reply
    0
    • shantoS shanto

      I have a list of cars, which have different colors. When the user enters a color I want all cars with the same color to be highlighted with that.
      I have tried something like this, but it doesn't work.
      string color = colorEdt->text().toStdString();
      ....
      item->setData(Qt::BackgroundRole,QColor::setNamedColor(color));
      Could you give me some tips?

      item->setData(Qt::BackgroundRole,QColor::setNamedColor(color));

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @shanto
      Hello,
      We would need some more code. What kind of list is that, is it a QListWidget? Where do you call item->setData(Qt::BackgroundRole,QColor::setNamedColor(color)), is it connected to a signal?
      And finally a note:

      string color = colorEdt->text().toStdString();
      

      Don't convert to std::string without any good reason, use Qt's QString instead, it's also is implicitly shared (which std::string is not) so there won't be unnecessary copying involved.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      shantoS 1 Reply Last reply
      0
      • kshegunovK kshegunov

        @shanto
        Hello,
        We would need some more code. What kind of list is that, is it a QListWidget? Where do you call item->setData(Qt::BackgroundRole,QColor::setNamedColor(color)), is it connected to a signal?
        And finally a note:

        string color = colorEdt->text().toStdString();
        

        Don't convert to std::string without any good reason, use Qt's QString instead, it's also is implicitly shared (which std::string is not) so there won't be unnecessary copying involved.

        Kind regards.

        shantoS Offline
        shantoS Offline
        shanto
        wrote on last edited by
        #3

        @kshegunov Hi! So here's what I'm trying to do:

        void UI::colorCars()
        {
            //I get the color from a QLineEdit
            string color = colorEdt->text().toStdString(); 
        
            //list contains the current cars in repository and guiList is a QListWidget
            std::vector<Car> llist = carCon.getCarsController();
            guiList->clear();
        
            for(Car& car : list)
            {
                QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(car.getNrMatr()),guiList);
                item->setData(Qt::UserRole,QVariant::fromValue(car.getId()));
                
                //I compare each car in the list with "color" and if it is the same I have to color the item with it(blue,green,yellow etc.)
                if(car.getColor() == color)
                {
                    item->setData(Qt::BackgroundRole,QColor(Qt::color));
                }
            }
        }
        
        kshegunovK 1 Reply Last reply
        0
        • shantoS shanto

          @kshegunov Hi! So here's what I'm trying to do:

          void UI::colorCars()
          {
              //I get the color from a QLineEdit
              string color = colorEdt->text().toStdString(); 
          
              //list contains the current cars in repository and guiList is a QListWidget
              std::vector<Car> llist = carCon.getCarsController();
              guiList->clear();
          
              for(Car& car : list)
              {
                  QListWidgetItem* item = new QListWidgetItem(QString::fromStdString(car.getNrMatr()),guiList);
                  item->setData(Qt::UserRole,QVariant::fromValue(car.getId()));
                  
                  //I compare each car in the list with "color" and if it is the same I have to color the item with it(blue,green,yellow etc.)
                  if(car.getColor() == color)
                  {
                      item->setData(Qt::BackgroundRole,QColor(Qt::color));
                  }
              }
          }
          
          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          @shanto
          Hello,

          item->setData(Qt::BackgroundRole, QColor(Qt::color));
          

          This doesn't look correct. I'd try something like this:

          item->setData(Qt::BackgroundRole, QColor(QString::fromStdString(color)));
          

          But the color name should be parse-able, otherwise you'd an invalid color.

          Read and abide by the Qt Code of Conduct

          shantoS 1 Reply Last reply
          1
          • kshegunovK kshegunov

            @shanto
            Hello,

            item->setData(Qt::BackgroundRole, QColor(Qt::color));
            

            This doesn't look correct. I'd try something like this:

            item->setData(Qt::BackgroundRole, QColor(QString::fromStdString(color)));
            

            But the color name should be parse-able, otherwise you'd an invalid color.

            shantoS Offline
            shantoS Offline
            shanto
            wrote on last edited by
            #5

            @kshegunov Thanks a lot! It works now. :)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved