QT: Hide the title bar of a dialog/window - qt

I have a parent window in which a push-button's click event function has the following lines:
SplashScreenDialog *splScrDlg = new SplashScreenDialog(this);
splScrDlg->show();
What I want is I want to remove the maximize button, minimize button, close button and also the title bar from the dialog(or window). [Actually it is for a splash screen, it would contain an image for a while and then would exit automatically and opens the main window, you are welcome with other ideas for showing splash screen]

Why not using QSplashScreen?
Example extracted from the assistant:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPixmap pixmap(":/splash.png");
QSplashScreen splash(pixmap);
splash.show();
app.processEvents();
...
QMainWindow window;
window.show();
splash.finish(&window);
return app.exec();
}

Why not use QSplashScreen for this? Anyway, you can set window flags to remove the window decoration. See the documentation for QWidget::setWindowFlags and Qt::WindowFlags.

Related

How to maximize QDialog to full screen?

I have an application in full screen mode when taskbar (placed on bottom of screen) is invisible. In this application it is possible to display a dialog, but if I maximize it (using maximize button or programmatically), the dialog will not appear to full screen. It just appears like the taskbar was there.
So is it possible to maximize dialog to full screen? But it is necessary to keep its titlebar with buttons for minimizing, maximizing and closing.
Edit:
I didn`t add any code, because I found nothing useful. But ok, here is code which does not work:
// main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.showFullScreen();
return a.exec();
}
// mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QDialog *dialog = new QDialog(this);
// only for better visibility of the dialog
dialog->setStyleSheet("background: green");
// This does not work because the dialog is displayed without a titlebar
// and buttons for minimizing, maximizing and closing.
// dialog->setWindowState(Qt::WindowFullScreen);
// dialog->show();
// or (it is the same)
// dialog->showFullScreen();
// This does not display the dialog on the full screen.
dialog->showMaximized();
}
// mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow() {}
};
It depends on how your taskbar settings are set.
fi. on windows10:
if I use "Automaticaly hide the taskbar in desktop mode" ON
then Maximizing will take only the part of the screen above the taskbar.
If the above setting is off
then Maximizing will take the whole screen.
You can use QScreen to query the available space and steer the maximum size in code also:
Qt Docs QScreen

Qt remove title bar from all widget and dialog in application

I know how to remove title bar from widget or QDialog, But I want to know is there any way to omit the title bar from all of the widget in application with single code,like QApplication.setWindowFlags(Qt::CustomizeWindowHint); (for example)
I usually I'm in need to do so, I usually do it with
Qt::Window | Qt::FramelessWindowHint
It should be used in the very top of your main function so you should end with something like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WindowName window;
window.setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
window.show();
}

Qt how to put QLabel without any layout

Can someone tell me, is it possible to use QWidget without any Layout. I have QSplashScreen with image as a background and I want to add one more QLabel with another image inside of my splash screen, but because splash screen is not resizable and there is no reason to do this, I haven't want use any Layout. I want just add QLabel with image and set its geometry.
There's nothing to it: just add your widget as a child of the splash screen, and manually set its position, and perhaps size as well.
int main(int argc, char ** argv) {
QApplication a{argc, argv};
QSplashScreen splash;
QLabel image{&splash};
image.move(50, 50);
...
splash.show();
return a.exec();
}
Much the same as Kuba Ober's above code with some minor but necessary additions.
QPixmap pixmap(":/splash.png"); //from resources
QSplashScreen splash(pixmap);
QLabel label(&splash);
label.setPixmap(pixmap); //you can use different image for label
label.setScaledContents(true);
label.setGeometry(50,50,50,50);
splash.show();

How to set dock widget below widget

I want to set dock widget at place below a widget as in figure
But when I am setting geometry as start point of Y to equivalent the gap size but it is not working.
I think position of QDockWidget should be handled different way, then setting its geometry.
This example shows how QDockWidgets are positioned as you want + one more dock widget tabbed together with another one.
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Main window
QMainWindow *window = new QMainWindow;
window->setDockNestingEnabled(true);
// Anything to add as central widget
QWidget *widget = new QWidget;
window->setCentralWidget(widget);
// First dock in the left top corner
QDockWidget *dockWidget = new QDockWidget("--- 1 ---");
window->addDockWidget(Qt::LeftDockWidgetArea, dockWidget);
// Second dock below first one
QDockWidget *dockWidget2 = new QDockWidget("--- 2 ---");
window->addDockWidget(Qt::LeftDockWidgetArea, dockWidget2);
// Third in tab with second one
QDockWidget *dockWidget3 = new QDockWidget("--- 3 ---");
window->tabifyDockWidget(dockWidget2, dockWidget3);
window->show();
return a.exec();
}
QMainWindow Has several functions how to handle spliting, moving or tabbing QDockWidgets

How to assign a signal to a QMenu instead of QAction in Qt?

I would like to have a menu item without children. I have a QMenubar, and in this menubar there is a QMenu. To use it as a menu, I have to put QActions in it, and they will appear if I click on the QMenu. How is it possible, to have only the menu, without any QActions, to do an action just as if it were a QAction?
A workaround would be to have a toolbox and disguise it as a menu, but it raises several problems:
It's not the cleanest solution
I have to manually care about highlighting it on mouse hover, and it will not be platform- and user setting independent.
I can't use it together in a menubar with normal menus with children.
So you want a menu bar that triggers actions without opening sub-menus?
Try to directly add QActions to the menubar, instead of having a QMenu inbetween:
#include <QtWidgets>
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QMainWindow *wnd = new QMainWindow;
QMenuBar *m = new QMenuBar;
QAction *a = new QAction("DoIt");
QObject::connect(a, &QAction::triggered, [wnd](){
QMessageBox::information(wnd, "DoIt", "DoIt");
});
m->addAction(a);
wnd->setMenuBar(m);
wnd->show();
return app.exec();
}
Alternatively, you could subclass the QMenu and handle the mousePressEvent method to emit a signal

Resources