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. [SOLVED]Semi transparent widget
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Semi transparent widget

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

    I'm trying to implement a semitransparent widget. How should I make this? I've try with setWindowOpacity but didn'work. This is my code:

    @#include<QApplication>
    #include <QtGui>

    class ChildWidget : public QLabel
    {
    public:
    ChildWidget( QWidget* parent = 0 ) : QLabel( parent )
    {
    setWindowOpacity( 0.5 );
    setStyleSheet( " background-color:blue " );
    }
    };

    class ParentWidget : public QLabel
    {
    private:
    ChildWidget* childWidget;
    public:
    ParentWidget( QWidget* parent = 0 ) : QLabel( parent )
    {
    setStyleSheet( " background-color:red " );
    setFixedSize( 200, 200 );
    childWidget = new ChildWidget( this );
    childWidget->setGeometry( 0,0,100,200);
    }
    };

    int main( int argc, char* argv[] )
    {
    QApplication a(argc, argv);
    ParentWidget* parentWidget = new ParentWidget;
    parentWidget->show();

    return a.exec();
    }@

    There are only 10 types of people in the world: Those who understand binary, and those who don't

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      You must call setWindowOpacity(0.5) on your toplevel widget (ParentWidget), not on the ChildWidget.

      Also, you did not include the Q_OBJECT macros in your QLabel classes.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Franzk
        wrote on last edited by
        #3

        [quote author="Volker" date="1292861432"]Also, you did not include the Q_OBJECT macros in your QLabel classes.[/quote]
        No use of signals and slots or translation. No need to include those, really.

        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sorin
          wrote on last edited by
          #4

          I want only the ChildWidget to be semi transparent...

          There are only 10 types of people in the world: Those who understand binary, and those who don't

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            change things to this:

            @

            ChildWidget( QWidget* parent = 0 ) : QLabel( parent )
            {
                QPalette pal = palette();
                pal.setBrush(QPalette::Window, QColor(0, 0, 255, 128) );
                setPalette(pal);
                setAutoFillBackground(true);
            }
            
            
            
            ParentWidget( QWidget* parent = 0 ) : QLabel( parent )
            {
                QPalette pal = palette();
                pal.setBrush(QPalette::Window, Qt::red);
                setPalette(pal);
            
                setFixedSize( 200, 200 );
                setText("asdfa fladsf ldasfjdasfl asdflja sfljdsalfj");
                childWidget = new ChildWidget( this );
                childWidget->setGeometry( 0,0,100,200);
            }
            

            @

            Although this does not create a full red with a light blue on top. Since the transparency adds the red of the parent widget to the blue of the child widget the resulting color is kind of purple/violet.

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sorin
              wrote on last edited by
              #6

              Thanks, worked for my case, I just needed to see the ruler through my guide widget

              There are only 10 types of people in the world: Those who understand binary, and those who don't

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                As a rule of thumb: It's pretty hard if not impossible to mix widget stylesheets with regular styling (palette, etc.). One should stick to either the one or the other.

                http://www.catb.org/~esr/faqs/smart-questions.html

                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