Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't use painter->fillRect with custom color
Forum Updated to NodeBB v4.3 + New Features

Can't use painter->fillRect with custom color

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.7k Views 1 Watching
  • 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.
  • NiagarerN Offline
    NiagarerN Offline
    Niagarer
    wrote on last edited by
    #1

    Hey,
    I want to paint a rectangle in the drawBackground function of a QGraphicsView.
    But I want to use a custom color.
    So I tried:

        QColor *myColor = new QColor(71, 71, 71, 255);
        painter->fillRect(rect.intersected(sceneRect), myColor);
    

    Then I get this error in the second line:

    error: invalid conversion from 'const QColor*' to 'QRgb {aka unsigned int}' [-fpermissive]
    

    i really don't understand that, when I try:

        QColor myColor = new QColor(71, 71, 71, 255); // no pointer!
        painter->fillRect(rect.intersected(sceneRect), myColor);
    

    I get this error in the first line:

    error: invalid conversion from 'QColor*' to 'QRgb {aka unsigned int}' [-fpermissive]
         QColor myColor = new QColor(71, 71, 71, 255);
    

    I have no Idea what this means, I even do not use any QRgb here so I don't know what is wrong with this code
    What should I write instead and what does the error mean?
    Thanks for answers

    1 Reply Last reply
    0
    • NiagarerN Offline
      NiagarerN Offline
      Niagarer
      wrote on last edited by Niagarer
      #2

      Found it!
      just had to use

          painter->fillRect(rect.intersected(sceneRect), myColor->rgb());
      

      But I still don't know why it says

      invalid conversion
      

      in the first line, when I do not use myColor->rgb()
      There was nothing wrong with the first line...

      T 1 Reply Last reply
      0
      • NiagarerN Niagarer

        Found it!
        just had to use

            painter->fillRect(rect.intersected(sceneRect), myColor->rgb());
        

        But I still don't know why it says

        invalid conversion
        

        in the first line, when I do not use myColor->rgb()
        There was nothing wrong with the first line...

        T Offline
        T Offline
        tomma
        wrote on last edited by
        #3

        QPainter::fillRect expects QBrush or QColor and you are trying to call it with QColor pointer.

        QColor *myColor = new QColor(71, 71, 71, 255);
        painter->fillRect(rect.intersected(sceneRect), myColor);
        
        QColor myColor = new QColor(71, 71, 71, 255);
        

        These both try to create QColor using QColor* value and QColor(QRgb) seems to be closest constructor for this conversion.

        painter->fillRect(rect.intersected(sceneRect), myColor->rgb());
        

        And you create QColor using QRgb.

        1 Reply Last reply
        2
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You are creating a memory leak here. There's not need to create your QColour object on the heap. Just use the stack.

          const QColor myColor(71, 71, 71, 255);
          painter->fillRect(rect.intersected(sceneRect), myColor);
          

          Or even shorter:

          painter->fillRect(rect.intersected(sceneRect), QColor(71, 71, 71, 255));
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          NiagarerN 1 Reply Last reply
          2
          • SGaistS SGaist

            Hi,

            You are creating a memory leak here. There's not need to create your QColour object on the heap. Just use the stack.

            const QColor myColor(71, 71, 71, 255);
            painter->fillRect(rect.intersected(sceneRect), myColor);
            

            Or even shorter:

            painter->fillRect(rect.intersected(sceneRect), QColor(71, 71, 71, 255));
            
            NiagarerN Offline
            NiagarerN Offline
            Niagarer
            wrote on last edited by
            #5

            @SGaist
            Ok, thanks!
            Will I also be able to keep this method when I add the possibility in the program to change this color at runtime?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Yes, just make your myColor variable a class member and update it as you want it.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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