Results 1 to 9 of 9

Thread: Problem applying setWindowOpacity to a custom Widget

  1. #1
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Problem applying setWindowOpacity to a custom Widget

    Hi everybody !

    When I apply the setWindowOpacity method to the main window object of my application it works pretty well but not when I apply it to my custom widget, why ?

    Both main window and custom widget inherits from QWidget no ? So why is it "effectless" on my widget ?

    Shoul I do something specific in the paintEvent method of my widget ?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    Did you try setting the WFlags argument to WType_TopLevel (or some other flag type) when you create the QWidget object? I'm not sure if that will help, but it may get the QWidget object to behave more like the QMainWindow object.

  3. #3
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    I have just tried to modify the flags at QWidget objet creation ... some changes happen

    Now, my widget does not display anymore.

    I have tryed with the following flags :
    Qt Code:
    1. WStyle_Customize | WType_TopLevel
    To copy to clipboard, switch view to plain text mode 

    Perhaps your tips is the solution but I must find the good flags. I'm not sure if a top level window can owns a widget which is also top level ?

  4. #4
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    Actually, I just tried setting the opacity of a QWidget in one of my projects and it worked just fine (even with the flags set to 0, which is the QWidget default).

    Just to double-check, are you making sure that the opacity value you're using is between 0.0 and 1.0?

  5. #5
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    I checked the opacity value and it is valid because equal to 0.5.

    Here are some more details about my application :
    * it is an SDI application generated with the wizard
    * I implement the init slot in which I set the palette background pixmap, I create a timer and connect its timeout signal to a custom slot, finally I create my custom widget and set its opacity to 0.5
    * in the slot connected to the timer timeout signal I call the update method of my widget

    Here is the specific code of my application :
    Qt Code:
    1. void TransparentWidgetMainWindow::init()
    2. {
    3. // ...
    4. setPaletteBackgroundPixmap(QPixmap("E:\\pix.png"));
    5. resize(QSize(800, 480));
    6.  
    7. // ...
    8. pWidget = new CMyWidget(this, "toto");
    9. pWidget->setGeometry(10, 10, 140, 200);
    10. pWidget->setFocusPolicy(QWidget::StrongFocus);
    11. pWidget->setWindowOpacity(0.2);
    12.  
    13. // ...
    14. pTimer = new QTimer(this);
    15. connect(pTimer, SIGNAL(timeout()), this, SLOT(processTimerTimeout()));
    16. pTimer->start(100);
    17. }
    18.  
    19. void TransparentWidgetMainWindow::processTimerTimeout()
    20. {
    21. pWidget->update();
    22. }
    To copy to clipboard, switch view to plain text mode 

    and here is the implementation code of my custom widget :
    Qt Code:
    1. CMyWidget::CMyWidget(QWidget* parent, const char* name, WFlags f):
    2. QWidget(parent, name, f),
    3. m_pPixmap(0)
    4. {
    5. // ...
    6. if( !m_pPixmap )
    7. m_pPixmap = new QPixmap(QImage("E:\\\\anotherpix.png"));
    8. }
    9.  
    10. CMyWidget::~CMyWidget()
    11. {
    12. if( m_pPixmap )
    13. {
    14. delete m_pPixmap;
    15. m_pPixmap = 0;
    16. }
    17. }
    18.  
    19. void CMyWidget::paintEvent(QPaintEvent* ev)
    20. {
    21. QPainter p(this);
    22.  
    23. p.drawPixmap(ev->rect().x(), ev->rect().y(), *m_pPixmap, ev->rect().x(), ev->rect().y(), 140, 200);
    24. }
    To copy to clipboard, switch view to plain text mode 

    The aim of this widget is not (for the moment) to be well designed, it is to be functional ... but in fact it is not yet functional.

    As proposed in a previous answer of this post, I tried to set specific flag values passed in my widget constructor in order to configure it as a main window could be (it was proposed because a call setWindowOpacity has an effect on a main window), but settings my widget as a top level widget (as is my main window) does not solve my problem ... in fact I notice that the paintEvent method of my widget is never called.

    I hope thoose details could help.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    I am not very good at Qt... but seeing ur code I can just hint at one thing...

    in ur CWidget contructor, u are passing 3 arguments to the QWidget class... while in the documentation I saw that QWidget has only 2 parameters... so u r passing name as window flags for the QWidget class
    I am not sure, but can u chk if thats the problem ??

  7. #7
    Join Date
    Oct 2006
    Location
    Austin, Texas, USA
    Posts
    18
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    Quote Originally Posted by aamer4yu View Post
    I am not very good at Qt... but seeing ur code I can just hint at one thing...

    in ur CWidget contructor, u are passing 3 arguments to the QWidget class... while in the documentation I saw that QWidget has only 2 parameters... so u r passing name as window flags for the QWidget class
    I am not sure, but can u chk if thats the problem ??
    Actually, the constructor for QWidget can accept 3 arguments:
    http://doc.trolltech.com/3.3/qwidget.html#QWidget

    EDIT: aamer4yu, I just noticed that in Qt 4.0 you are correct - QWidget only takes 2 parameters:
    http://doc.trolltech.com/4.2/qwidget.html#QWidget

    I believe yellowmat's issue is dealing with Qt 3, correct? (or at least that's what the title of the thread points to)


    My question for yellowmat is what is the CMyWidget class defaulting the WFlags f parameter to? In the header file for the class, do you have WFlags f set to a default value (like 0)? If not, it could be getting some random value and giving the object bogus flags to use.

    Other than that, I don't see anything wrong with the code you've written...
    Last edited by Claymore; 1st November 2006 at 04:29.

  8. #8
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    The version of Qt librairies I use is 3.3.3 (or Qt 3 as specified in the post).

    I give the header of my class and you can see I use default values for each parameter of my constructor.
    Qt Code:
    1. class CMyWidget : public QWidget
    2. {
    3. public:
    4. // ...
    5. CMyWidget(QWidget* parent=0, const char* name=0, WFlags f=0);
    6.  
    7. // ...
    8. ~CMyWidget();
    9.  
    10. // ...
    11. void paintEvent(QPaintEvent*);
    12. private:
    13. QPixmap* m_pPixmap;
    14. };
    To copy to clipboard, switch view to plain text mode 

    I don't know why tunning opacity has no effect on my (a) widget ... I don't what to do, so if someone has a clue it would be great.

  9. #9
    Join Date
    Jan 2006
    Posts
    162
    Thanks
    9
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Problem applying setWindowOpacity to a custom Widget

    So ... I have seen a post of someone having the problem. He said he inherits is custom widget class from QDialog instead of QWidget and since this change he can control the opacity behaviour.

    The problem is that since the custom inherits from QDialog instead of QWidget, it is not possible to manage it as a classic widget.

    Is it possible to manage a dialog as a widget, for example integrating it in a layout and so on ?

Similar Threads

  1. Replies: 3
    Last Post: 12th April 2006, 08:20
  2. Problem reimplementing QSpinBox in a custom widget
    By hvengel in forum Qt Programming
    Replies: 1
    Last Post: 30th March 2006, 08:12
  3. Replies: 4
    Last Post: 24th March 2006, 22:50
  4. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 11:55
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.