[SOLVED]Semi transparent widget



  • 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();
    }@



  • 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.



  • [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.



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



  • 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.



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.