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. Odd behaviour with QPen/QColor
Forum Updated to NodeBB v4.3 + New Features

Odd behaviour with QPen/QColor

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 156 Views 2 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.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by
    #1

    I have a widget that is displayed on top of another widget (imageView), and has the following attributes set in the ctor:

            setAttribute(Qt::WA_TransparentForMouseEvents);
            setAttribute(Qt::WA_NoSystemBackground);
            setAttribute(Qt::WA_WState_ExplicitShowHide);
    

    This code in its paintEvent:

            QPainter painter(this);
            QColor colour{ QColorConstants::Cyan };
            colour.setAlpha(128);
            QPen pen(colour, 1.0);
    
            if (pt1_ != pt2_)
            {
                QPointF p1 = imageView->imageToScreen(pt1_);
                if (p1.x() < 0.5) p1.rx() = 0.5;
                if (p1.y() < 0.5) p1.rx() = 0.5;
                QPointF p2 = imageView->imageToScreen(pt2_);
                if (p2.x() < 0.5) p2.rx() = 0.5;
                p2.ry()--;
                QPointF p3 = imageView->imageToScreen(pt3_);
                p3.rx()--;  p3.ry()--;
                QPointF p4 = imageView->imageToScreen(pt4_);
                if (p4.x() < 0.5) p4.rx() = 0.5;
                p4.ry()--;
                painter.drawLine(p1, p2);
                painter.drawLine(p2, p3);
                painter.drawLine(p3, p4);
                painter.drawLine(p4, p1);
            }
    

    mostly works fine except that the line drawn is white and not semi-transparent???

    ea507303-18a0-4fa4-9903-a28765e12c3b-image.png

    Am I missing something obvious here?

    Thanks
    David

    jsulmJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      I have a widget that is displayed on top of another widget (imageView), and has the following attributes set in the ctor:

              setAttribute(Qt::WA_TransparentForMouseEvents);
              setAttribute(Qt::WA_NoSystemBackground);
              setAttribute(Qt::WA_WState_ExplicitShowHide);
      

      This code in its paintEvent:

              QPainter painter(this);
              QColor colour{ QColorConstants::Cyan };
              colour.setAlpha(128);
              QPen pen(colour, 1.0);
      
              if (pt1_ != pt2_)
              {
                  QPointF p1 = imageView->imageToScreen(pt1_);
                  if (p1.x() < 0.5) p1.rx() = 0.5;
                  if (p1.y() < 0.5) p1.rx() = 0.5;
                  QPointF p2 = imageView->imageToScreen(pt2_);
                  if (p2.x() < 0.5) p2.rx() = 0.5;
                  p2.ry()--;
                  QPointF p3 = imageView->imageToScreen(pt3_);
                  p3.rx()--;  p3.ry()--;
                  QPointF p4 = imageView->imageToScreen(pt4_);
                  if (p4.x() < 0.5) p4.rx() = 0.5;
                  p4.ry()--;
                  painter.drawLine(p1, p2);
                  painter.drawLine(p2, p3);
                  painter.drawLine(p3, p4);
                  painter.drawLine(p4, p1);
              }
      

      mostly works fine except that the line drawn is white and not semi-transparent???

      ea507303-18a0-4fa4-9903-a28765e12c3b-image.png

      Am I missing something obvious here?

      Thanks
      David

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Perdrix said in Odd behaviour with QPen/QColor:

      QPen pen(colour, 1.0);

      You're not using pen anywhere as far as I can see...

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      PerdrixP 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Perdrix said in Odd behaviour with QPen/QColor:

        QPen pen(colour, 1.0);

        You're not using pen anywhere as far as I can see...

        PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by Perdrix
        #3

        @jsulm Yep you're right I did miss the obvious

        Doh! Slap head!

        painter.setPen(pen) would help a lot!

        1 Reply Last reply
        0
        • SGaistS SGaist has marked this topic as solved on

        • Login

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