Results 1 to 11 of 11

Thread: QMainWindow or QDialog show() - before displaying, the window area is painted white

  1. #1
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QMainWindow or QDialog show() - before displaying, the window area is painted white

    QMainWindow or QDialog show() - before displaying, the window area is painted white. You may not notice this on fast computers. How to avoid this ?
    Qt Code:
    1. auto*w=new Dialog1();
    2. w->show();
    To copy to clipboard, switch view to plain text mode 
    If I use
    Qt Code:
    1. auto*w=new Dialog1();
    2. w->setWindowFlags(Qt::FramelessWindowHint);
    3. w->show();
    To copy to clipboard, switch view to plain text mode 
    then there is no white paint, but there is no window header


    Windows 10. Desktop Qt 5.15.0 MinGW 64-bit

    Link to project https://drive.google.com/file/d/1vpu...e5SZAX46Z/view

    Link to Video - https://drive.google.com/file/d/1-gs...ew?usp=sharing

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    The paint handling clears the window contents and fills it with the background color before calling the actual paintEvent() for the widget. If you don't want this, then set the QWidget::autoFillBackground() property to false when you construct the widget. This means that if there is anything already drawn in the window, it won't be erased and new drawing will happen on top of it. If nothing has changed since the last repaint, this will be fine, but if there have been changes then either you clear the background yourself to start with a clean background or you get an overlaid mess.

    I don't know why you think setting a FramelessWindowHint would have anything to do with clearing the background.

    But if what you -really- want is a window with a transparent background, then you need to call QWidget::setAttribute() with Qt::WA_TranslucentBackground set to true alnog with Qt::WA_FramelessWindowHint. If you don't want a frameless window, then you could alternatively call QWidget::setWindowOpacity() with a value of 0.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    1.
    Quote Originally Posted by d_stranz View Post
    I don't know why you think setting a FramelessWindowHint would have anything to do with clearing the background
    I just showed an example where there is no problem

    2.
    Quote Originally Posted by d_stranz View Post
    The paint handling clears the window contents and fills it with the background color before calling the actual paintEvent() for the widget. If you don't want this, then set the QWidget::autoFillBackground() property to false when you construct the widget
    Qt Code:
    1. auto*w=new Dialog1();
    2. w->setAutoFillBackground(false);
    3. w->show();
    To copy to clipboard, switch view to plain text mode 
    This doesn't solve the problem

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    I just showed an example where there is no problem
    And you complained because the window was frameless.

    This doesn't solve the problem
    Have you read the documentation for QWidget::setAutoFillBackground()? There may be other requirements in order for this flag to have any effect.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Quote Originally Posted by d_stranz View Post
    Have you read the documentation for QWidget::setAutoFillBackground()? There may be other requirements in order for this flag to have any effect.
    Thank you for the link. But it didn't help me. Can you provide a working code ?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Can you provide a working code ?
    Sorry, no. I have never considered clearing a window before painting to be a problem and have never written code to try to work around that. I don't have any time to investigate it. Maybe someone else who follows this forum will have a solution or another suggestion.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Not sure why a eye-bleeding red background is desirable, but tastes vary.

    The doc for QWidget::setAutoFillBackground carries this warning that applies directly to your situation:
    Warning: Use this property with caution in conjunction with Qt Style Sheets. When a widget has a style sheet with a valid background or a border-image, this property is automatically disabled.
    So, this property was already disabled.

    Those docs also provide this:
    In addition, Windows are always filled with QPalette::Window, unless the WA_OpaquePaintEvent or WA_NoSystemBackground attributes are set.
    You could try setAttribute(Qt::WA_OpaquePaintEvent, true) in your dialog constructor. I do not have your platform to test one: it is not quite right on mine.

    Depending on exactly what you are trying to achieve: remove the style sheet from Dialog1.ui and modify the palette in Designer so that QPalette::Window is Red.

  8. #8
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Quote Originally Posted by ChrisW67 View Post
    Not sure why a eye-bleeding red background is desirable, but tastes vary
    The word "red" is shorter than others. Any colors are allowed in the training project

    Quote Originally Posted by ChrisW67 View Post
    remove the style sheet from Dialog1.ui and modify the palette in Designer so that QPalette::Window is Red.
    I removed the style sheet from Dialog1.ui

    Quote Originally Posted by ChrisW67 View Post
    You could try setAttribute(Qt::WA_OpaquePaintEvent, true) in your dialog constructor
    Qt Code:
    1. Dialog1::Dialog1(QWidget *parent) :
    2. QDialog(parent),ui(new Ui::Dialog1)
    3. {
    4. ui->setupUi(this);
    5.  
    6. QDialog::setAttribute(Qt::WA_OpaquePaintEvent,true);
    7.  
    8. QPalette pal = palette();
    9. pal.setColor(QPalette::Background, Qt::red);
    10. setPalette(pal);
    11. }
    To copy to clipboard, switch view to plain text mode 
    The result is in the video. The code is visible also there https://drive.google.com/file/d/1d3_...ew?usp=sharing

    if
    Qt Code:
    1. setAttribute(Qt::WA_OpaquePaintEvent, false)
    To copy to clipboard, switch view to plain text mode 
    the window is painted red

    It's not just my problem - https://forum.qt.io/topic/68655/turn...ill-on-qdialog
    Last edited by VVS; 17th October 2020 at 17:44.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Compare what I wrote:
    remove the style sheet from Dialog1.ui and modify the palette in Designer so that QPalette::Window is Red.
    to what you did in your code:
    Qt Code:
    1. QPalette pal = palette();
    2. pal.setColor(QPalette::Background, Qt::red);
    To copy to clipboard, switch view to plain text mode 

    You can actually make that palette adjustment to QPalette::Window in Designer if you wish. The QPalette::Window comes from the docs linked earlier.

    The other thing I did in your project was apply a layout to everything in your Dialog1. I do not think this will make a difference to the painting only the layout and usability.

  10. #10
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    Quote Originally Posted by ChrisW67 View Post
    You can actually make that palette adjustment to QPalette::Window in Designer
    I changed the color of the palette-Window and others in the designer. But it didn't help

  11. #11
    Join Date
    Oct 2020
    Posts
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QMainWindow or QDialog show() - before displaying, the window area is painted whi

    I solved this problem.

    Qt Code:
    1. class Dialog1 : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Dialog1(QWidget *parent = nullptr);
    7. ~Dialog1();
    8.  
    9. private:
    10. Ui::Dialog1 *ui;
    11. protected:
    12. bool event(QEvent*e);
    13. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Dialog1::Dialog1(QWidget *parent) :
    2. QDialog(parent),ui(new Ui::Dialog1)
    3. {
    4. ui->setupUi(this);
    5.  
    6. setWindowFlags(Qt::Window);
    7. setWindowOpacity(0);
    8. }
    9.  
    10. bool Dialog1::event(QEvent*e)
    11. {
    12. if(e->type()==QEvent::WindowActivate)
    13. {
    14. QTimer::singleShot(0,NULL,[this](){this->setWindowOpacity(1);});
    15. return true;
    16. }
    17. if(e->type()==QEvent::Hide)
    18. {
    19. setWindowOpacity(0);
    20. return true;
    21. }
    22. return QDialog::event(e);
    23. }
    To copy to clipboard, switch view to plain text mode 
    This is not a Qt problem, this is a Windows problem. I looked at my WPF and Windows Forms projects and saw that they have this problem. You can also see it by running Windows Explorer or Windows Task Manager. Another question is whether this is considered a problem. I think so, Yes. This flickering hits my eyes
    Last edited by VVS; 27th October 2020 at 12:41.

  12. The following user says thank you to VVS for this useful post:

    d_stranz (27th October 2020)

Similar Threads

  1. Replies: 0
    Last Post: 13th October 2020, 14:42
  2. Replies: 2
    Last Post: 20th March 2015, 12:38
  3. 8bit greyscale raw picture find painted area
    By dazedly in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2012, 10:00
  4. Replies: 4
    Last Post: 15th July 2011, 19:31
  5. QDialog::show() not displaying child
    By Wasabi in forum Newbie
    Replies: 3
    Last Post: 14th September 2010, 02:38

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.