Promote QWidget to QMainWindow or add QMainWindow to QWidget from Qt Designer
Asked Answered
D

1

6

My problem:

I want to customize the way the title bar works and looks for my application.

My idea:

I created a new QWidget form in Qt Designer and added a QWidget to it. I added the following code in constructor:

setAttribute(Qt::WA_TranslucentBackground);
setWindowFlags(Qt::FramelessWindowHint);

QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(20);
effect->setXOffset(0);
effect->setYOffset(0);
setGraphicsEffect(effect);

which makes the outer widget transparent and adds shadow to my inner widget. From this on I can create a custom title bar widget which I can implement however I want.

This is the result:

enter image description here

My issue

I want to make this usable from the designer as a main window and the QWidget doesn't allow me to add FROM THE DESIGNER tool bars, menu bar and status bar.

What I thought about was adding a QMainWindow widget as a child widget for the outer QWidget(which is transparent and acts as support for my shadow(the shadow is drawn on it)). I did this successfully but only from code:

QMainWindow *centralwidget = new QMainWindow();
centralwidget->setStyleSheet("background-color: lightgray;");
centralwidget->setGeometry(0, 0, 50, 20);
centralwidget->setWindowFlags(Qt::Widget);
this->layout()->addWidget(centralwidget);

QMenuBar *menuBar = new QMenuBar(centralwidget);
menuBar->addAction("Action");

QStatusBar *statusBar = new QStatusBar;
statusBar->showMessage("Status bar here");

centralwidget->addToolBar("tool bar");
centralwidget->setMenuBar(menuBar);
centralwidget->setStatusBar(statusBar);

This is the result:

enter image description here

My question:

How can I achieve this result from Qt Designer? Is it possible to promote a QWidget to QMainWindow? I cannot think to another way of doing it... It is really important for me to make it usable from Qt Designer because I intend to make it a template widget and be able to create e.g. a new QCustomMainWindow form Qt Creator just like you can create a QWidget or a QMainWindow.

Please help!

Drandell answered 30/8, 2014 at 16:0 Comment(2)
Start with a QMainWindow, and then apply the appropriate flags to it. QMainWindow is a subclass of QWidget. If it can't be done easily in the designer, it is pretty painless to do in code. Do it in your constructor right after the ui->setup() call.Dispense
@Dispense Can you be more specific in the apply the appropriate flags part? As you can see, in order to have the shadow I need to paint the QMainWindow on top of a QWidget while still keeping the Qt Designer features in place. This is what I don't know.Drandell
D
3

Here is another SO question similar to yours: Qt4: Placing QMainWindow instance inside other QWidget/QMainWindow

Just adding on to my original comment:

Start with a QMainWindow, and then apply the appropriate flags to it. QMainWindow is a subclass of QWidget. If it can't be done easily in the designer, it is pretty painless to do in code. Do it in your constructor right after the ui->setup() call.

Start with QMainWindow

Project Selection Dialog

Form Selection Dialog

Customize Window Flags

So in the constructor in mainwindow.cpp, you put

http://qt-project.org/doc/qt-5/qt.html#WindowType-enum

this->setWindowFlags(Qt::Widget);

This is the default type for QWidget. Widgets of this type are child widgets if they have a parent, and independent windows if they have no parent. See also Qt::Window and Qt::SubWindow.

// or if you want to apply more than one you, "or" it together, like so:
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);

Try out a couple of those and see what you like.

Customize Widget Attributes

There are also Widget Attributes, that give you strong control over how your widgets look like and behave.

http://qt-project.org/doc/qt-5/qt.html#WidgetAttribute-enum

Qt Style Sheets

In addition to all the flags and attributes above, you can also modify a ton of it with stylesheets:

http://qt-project.org/doc/qt-5/stylesheet-reference.html

this->setStyleSheet("background: #000000;");

Qt Designer Custom Widgets

And also if you are interested in making this a reusable thing in Qt Designer, you can make it into a Qt Designer plugin, or custom widgets.

http://qt-project.org/doc/qt-4.8/designer-using-custom-widgets.html

http://qt-project.org/doc/qt-4.8/designer-creating-custom-widgets.html

QMdiArea and QMdiWindow

Another path to look into besides using QMainWindow is QMdiSubWindow

http://qt-project.org/doc/qt-5/QMdiSubWindow.html

Dispense answered 8/9, 2014 at 17:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.