Results 1 to 3 of 3

Thread: QWidget over parent QMainwindow

  1. #1
    Join Date
    Oct 2016
    Posts
    5
    Qt products
    Qt5
    Platforms
    Windows

    Question QWidget over parent QMainwindow

    I have a mainWindow and a widget that will be a child of that main window. Below is a code example created with QTCreator.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. //create test widget
    11. QWidget* blue_widget = new QWidget(); //no parent
    12. blue_widget->setFixedSize(100,100);
    13. blue_widget->setStyleSheet("background-color:blue;");
    14. blue_widget->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool );
    15. blue_widget->setVisible(true);
    16. //create test widget END
    17.  
    18. blue_widget->setParent(this);
    19. blue_widget->move(-50,-10);
    20.  
    21. show();
    22. }
    23.  
    24. MainWindow::~MainWindow()
    25. {
    26. delete ui;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Right now I can only see the part of the blue_widget that is inside the boundaries of the main window. I would like to be able to have the totality of the blue_widget shown. How can I prevent the main window cutting the blue_widget like that ? I really need to have the blue_widget to be a child of the main window.

    Thanks for the help.

  2. #2
    Join Date
    Mar 2014
    Posts
    23
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows Android

    Default Re: QWidget over parent QMainwindow

    Short answer: you can't.

    If the widget is a logical child of the window it will always be clipped at the parent's boundaries, there's no way around this. If you want to kinda overlap both elements you have to remove the blue widget from the parent's children stack and treat it as a separate window.

    What is the purpose of this, what's the goal?

    Btw, if you call setParent() later down the line you could have just passed the parent to the constructor of the widget ;-)

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QWidget over parent QMainwindow

    Quote Originally Posted by Killian View Post
    If the widget is a logical child of the window it will always be clipped at the parent's boundaries, there's no way around this. If you want to kinda overlap both elements you have to remove the blue widget from the parent's children stack and treat it as a separate window.
    While this is generally true if the child is a plain widget, it does not apply to widgets that are windows, e.g. dialogs.
    E.g. a dialog can be a child of another widget but still not be restricted by the parent's boundaries since it is its own top level window.

    Now, the widget here uses the Tool window flag, which should make it a window.

    So things to try are:
    1) without the frameless hint
    2) passing parent and flags to the constructor
    3) using Qt::Dialog instead of Qt::Tool just to see if the Qt::Popup bit of Tool messes things up

    Cheers,
    _

Similar Threads

  1. Same Width of Qt's Bottom Drawer and its parent QMainWindow
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2014, 23:41
  2. Replies: 1
    Last Post: 12th June 2014, 06:40
  3. Open QMainWindow to use xterm window as parent
    By muenalan in forum Qt Programming
    Replies: 0
    Last Post: 1st July 2010, 08:43
  4. Promoting the parent QWidget of a QWidget form
    By extrakun in forum Qt Tools
    Replies: 6
    Last Post: 16th April 2010, 14:19
  5. QWidget *parent = 0 question
    By radek.z in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2009, 09:32

Tags for this Thread

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.