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. Semi-transparent QFrame

Semi-transparent QFrame

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 5.6k 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.
  • M Offline
    M Offline
    Morris
    wrote on last edited by
    #1

    I need to make a QFrame semi-transparent when the mouse is not hovering on it. And make it opaque when the mouse moves on it.
    I have try QWidget::setWindowOpacity() which doesn't work. Obviously, the setWindowOpacity() works on a widget which has a Qt::Window flag.
    So, how can I make the QFrame semi-transparent.
    PS: I have set the css of the QFrame object, it has a png as its background.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on last edited by
      #2

      Hi,

      it's not a full solution, but I think that it can be an idea for you.

      I've written a sample code, that is a widget, with a custom paint method.

      @

      class Widget : public QWidget
      {
      Q_OBJECT

      bool _over;
      bool _prevOver;

      protected:
      void paintEvent(QPaintEvent *event);
      void mouseMoveEvent(QMouseEvent *event);

      public:
      Widget(QWidget *parent = 0);
      };
      @

      @

      Widget::Widget(QWidget *parent)
      : QWidget(parent),
      _over(false),
      _prevOver(false)
      {
      setMouseTracking(true);
      }

      void Widget::mouseMoveEvent(QMouseEvent *event)
      {
      _over = QRect(rect().center()-QPoint(100,100),rect().center()+QPoint(100,100)).contains(event->pos());
      QWidget::mouseMoveEvent(event);
      if (_over != _prevOver)
      {
      _prevOver = _over;
      update();
      }
      }

      void Widget::paintEvent(QPaintEvent *event)
      {
      QPainter p(this);

      p.drawText(rect(),"Hello world!",QTextOption(Qt::AlignCenter | Qt::AlignVCenter));
      p.fillRect(rect(),QColor(255,0,0,(_over)? 255 : 100));
      }
      @

      If you move the cursor out of a 100x100 pixel square from the center, you can see "Hello world!" covered with a semi-transparent color (so you can see it).
      If you move in the square, the color fully covers the text.

      So, you should modify the alpha of the png in a temporary QPixmap, and paint it, or something similar, if the transparent condition happens.

      Tony.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Morris
        wrote on last edited by
        #3

        Thanks for the example. So I need to override the paintEvent.

        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