PDA

View Full Version : QWidget over parent QMainwindow



jacobP
4th November 2016, 00:30
I have a mainWindow and a widget that will be a child of that main window. Below is a code example created with QTCreator.



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

//create test widget
QWidget* blue_widget = new QWidget(); //no parent
blue_widget->setFixedSize(100,100);
blue_widget->setStyleSheet("background-color:blue;");
blue_widget->setWindowFlags(Qt::FramelessWindowHint | Qt::Tool );
blue_widget->setVisible(true);
//create test widget END

blue_widget->setParent(this);
blue_widget->move(-50,-10);

show();
}

MainWindow::~MainWindow()
{
delete ui;
}


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.

Killian
4th November 2016, 10:21
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 ;-)

anda_skoa
4th November 2016, 11:22
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,
_