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. I get the wrong color in paint function.
Forum Updated to NodeBB v4.3 + New Features

I get the wrong color in paint function.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 764 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.
  • joeQJ Offline
    joeQJ Offline
    joeQ
    wrote on last edited by
    #1
    //ShowImgScene
    void ShowImgScene::AddRect(QRect &rect, QColor color /*= QColor(Qt::red)*/)
    {
        QPen pen;
        QGraphicsRectItem * pRectItem = new GraphicsRectItem(rect); // GraphicsRectItem
        pen.setWidth(0);
        pen.setColor(color);
        pRectItem->setPen(pen); // at here i set the pen
    }
    

    I wan to get the color in GraphicsRectItem::paint function.

    //GraphicsRectItem
    void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        QGraphicsRectItem::paint(painter,option,widget);
    
        QColor color = painter->pen().color(); // but the color always is black
        if( option->state & QStyle::State_Selected ){
            painter->fillRect(option->rect,color);
        }
    }
    

    where is i wrong? and, how to get the color of before set in paint function?

    About this problem, i can use the setData and data. But, i think there must be a better way.

    Just do it!

    matthew.kuiashM 1 Reply Last reply
    0
    • joeQJ joeQ
      //ShowImgScene
      void ShowImgScene::AddRect(QRect &rect, QColor color /*= QColor(Qt::red)*/)
      {
          QPen pen;
          QGraphicsRectItem * pRectItem = new GraphicsRectItem(rect); // GraphicsRectItem
          pen.setWidth(0);
          pen.setColor(color);
          pRectItem->setPen(pen); // at here i set the pen
      }
      

      I wan to get the color in GraphicsRectItem::paint function.

      //GraphicsRectItem
      void GraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
      {
          QGraphicsRectItem::paint(painter,option,widget);
      
          QColor color = painter->pen().color(); // but the color always is black
          if( option->state & QStyle::State_Selected ){
              painter->fillRect(option->rect,color);
          }
      }
      

      where is i wrong? and, how to get the color of before set in paint function?

      About this problem, i can use the setData and data. But, i think there must be a better way.

      matthew.kuiashM Offline
      matthew.kuiashM Offline
      matthew.kuiash
      wrote on last edited by
      #2

      @joeQ I think you should use "this->pen()" to access the colour of the graphics item. You're accessing the last pen set on the painter instead.

      Please try this…

      QColor color = pen().color();
      

      (I'm working from memory, apologise if I'm wrong!)

      The legendary cellist Pablo Casals was asked why he continued to practice at age 90. "Because I think I'm making progress," he replied.

      joeQJ 1 Reply Last reply
      2
      • matthew.kuiashM matthew.kuiash

        @joeQ I think you should use "this->pen()" to access the colour of the graphics item. You're accessing the last pen set on the painter instead.

        Please try this…

        QColor color = pen().color();
        

        (I'm working from memory, apologise if I'm wrong!)

        joeQJ Offline
        joeQJ Offline
        joeQ
        wrote on last edited by
        #3

        @matthew.kuiash

        Excellent, Your memory is good! Thank u very much.

        Just do it!

        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