How to maximize QDialog to full screen? - qt

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

Related

How to make Transparent QT Dock Widget

On Windows ,I am trying to create Qt application with transparent DOCKWIDGETS, where background of dock widget is transparent when it is floated. So we can see through dock widget.
Currently it looks black as below.
Code as below
QDockWidget * dock3 = new QDockWidget(tr("DOCK3 TranslucentBackground"),
textEdit,Qt::FramelessWindowHint);
dock3->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
//dock3->setWindowFlags(dock2->windowFlags()|Qt::FramelessWindowHint);
dock3->setAttribute(Qt::WA_TranslucentBackground);
//dock3->setAttribute(Qt::WA_NoSystemBackground);
{
QWidget* WindowRect = new QWidget(dock3);
QWidget* titleRect = new QLabel ("Title",WindowRect);
titleRect->setFixedSize(QSize(30,60));
titleRect->setStyleSheet("background:rgb(0,0,255);");
QWidget* ContentRect = new QLabel("Content",WindowRect);
ContentRect->setFixedSize(QSize(60,30));
ContentRect->setStyleSheet("background:rgb(0,255,0);");
QVBoxLayout* layout = new QVBoxLayout(WindowRect);
layout->addWidget(titleRect);
layout->addWidget(ContentRect);
dock3->setWidget(WindowRect);
}
One way is to use setWindowOpacity(qreal) of the QDockWidget.
But keep in mind that this will apply the opacity to all children of the QDockWidget.
For reference: https://doc.qt.io/qt-5/qwidget.html#windowOpacity-prop
Another way is to use style sheets:
setStyleSheet("background-color: transparent;");. Unfortunately this doesn't work for top level widgets until you set the attribute WA_TranslucentBackground of the base widget.
For reference:
https://doc.qt.io/qt-5/stylesheet.html
https://doc.qt.io/qt-5/qwidget.html#styleSheet-prop
Try with this article:
Qt tip & Trick: Masking Widgets
You can do it with:
setStyleSheet("background-color: rgba(0,0,0,0)");
You can try to to it in the drawin customisation by changing the style of your widget like:
MyCustomWidget {background-color: none;}
It should work
I understand that you want to see through the docking bar only when it is floating. When it's not (docked), it makes no sense because there's nothing behind to be shown.
Using setAttribute(Qt::WA_TranslucentBackground) does the trick. I'm under Linux, hopefully, it also works for Windows (I found some posts where people additionally set setAttribute(Qt::WA_NoSystemBackground), it made no difference for me under Linux, if Qt::WA_TranslucentBackground is not enough for you, give it a try with both).
#include <QMainWindow>
#include <QApplication>
#include <QDockWidget>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
w.setCentralWidget( new QWidget() );
w.centralWidget()->setStyleSheet("background-color: green");
QDockWidget* dock = new QDockWidget();
dock->setWidget( new QLabel("Hello World",dock) );
// make docking bar transparent!
dock->setAttribute(Qt::WA_TranslucentBackground);
w.addDockWidget(Qt::BottomDockWidgetArea,dock, Qt::Horizontal);
w.show();
return a.exec();
}
When docked, it looks like this:
When floating, it looks like this:
You can see the central widget (green), can be visible through the docking bar.
Reference: Make QWidget transparent

QMainWindow: Minimize to dash board on Ubuntu 18.04

I want to minimize a QMainWindow to an icon on the dash board on the left side of the screen on Ubuntu 18.04.
When the "show()" function is called and the QMainWindow is visible I see the icon for my application on the left side dash.
However, when the "hide()" function is called on QMainWindow the application is no longer visible which is fine but the dashboard icon also disappears. I want to minimize so that the application is no longer visible but there is still a dash board icon.
Edited:
I am now able to see the icon when minimizing.
So there is still one small problem. My application has been using a QSystemTrayIcon to show/hide the main window. When the Main Window is visible I have it minimizing to an icon now. The problem is in order to have the Main Window display again I need to click on the Dash Board minimized icon now. The QSystemTrayIcon is not responsive when the Main Window is in minimized state. I would like the Main Window to come up with either Dash Board icon or QSystemTrayIcon.
Here's my code:
connect(m_pTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(onActivated(QSystemTrayIcon::ActivationReason)));
// Slot to handle tray icon activated signal
void MyMainWindow::onActivated(QSystemTrayIcon::ActivationReason r)
{
if (r == QSystemTrayIcon::Trigger)
{
if (!this->isVisible() || this->isMinimized())
{
this->show();
}
else
{
this->showMinimized();
}
}
}
Use should use setWindowState. Check out doc setWindowState
Below is code.
#include <QApplication>
#include <QPushButton>
#include <QWidget>
int main(int argc, char **argv) {
QApplication a(argc, argv);
QWidget w;
QPushButton b("hideme", &w);
w.show();
QObject::connect(&b, &QPushButton::clicked,
[&w]() { w.setWindowState(Qt::WindowMinimized); });
return a.exec();
}

Quality of image deteriorates when used fitinview and Qt:KeepAspectRatio

I am using Qt and I want to display a image and I am displaying it in QGraphicsView and as the image is bigger than the dimensions of box of the QGraphicsView I used
ui->graphicsView->fitInView(viewraw->itemsBoundingRect() ,Qt::KeepAspectRatio);
where viewraw contains pixmap address to my image, my problem of oversized image was solved with this but my new problem now is the image after automatic resizing by Qt now shows with very very bad quality, it is very very blurry
What should I do?
Is there any way to display the image fit in the box and no quality loss?
Thank you!
You should try enabling antialiasing:
ui->graphicsView->setRenderHints(QPainter::Antialiasing
| QPainter::SmoothPixmapTransform
| QPainter::TextAntialiasing);
You can use QLayout::setSizeConstraint(QLayout::SetFixedSize) on the layout containing the view, to resize the parent widget to fit the view. The size will be the one returned by the view sceneRect() function.
For example, the following code will resize the QMainWindow (and lock the size) so that both items are visible:
#include <QtGui>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow mainWindow;
QGraphicsView *view = new QGraphicsView(&mainWindow);
QGraphicsScene *scene = new QGraphicsScene(view);
view->setScene(scene);
mainWindow.layout()->setSizeConstraint(QLayout::SetFixedSize);
mainWindow.setCentralWidget(view);
mainWindow.show();
scene->addRect(-100,-100,10,10, QPen(), Qt::red );
scene->addRect(600,450,10,10, QPen(), Qt::blue );
return a.exec();
}
Or, since you are using the designer,
in the widget tree, go to the parent item of the graphics view, it should have a layout,
if not (if the icon is this one: ), add one by left clicking on the item name and choose a layout in the "Lay Out" menu (if you only have one widget, the type of layout doesn't matter, just choose one),
then go to the last option in the property panel for the same item, which should be "layoutSizeConstraint" and select the option "SetFixedSize".

QDockWidget sized wrong when docked on right side of Main Window

I am new to Qt and I am trying to create a DockWidget that docks on the right of the window. I set a maximum and minimum width for the dock (as you will see in the code below). This works if the dock widget is added with Qt::LeftDockWidgetArea, but when it is added with Qt::RightDockWidgetArea, The dock is "padded" out to the center of the window, like this:
I am probably not sizing the dock in the correct way.. Here is the code for this window:
int main(int argv, char** args)
{
QApplication app(argv, args);
QMainWindow window;
QDesktopWidget* desktop = QApplication::desktop();
//Docks
QDockWidget* propertyDock = new QDockWidget("",&window);
QWidget* propertyDockContents = new QWidget;
//This sets the window in the center of the screen.
int wWidth = 800; int wHeight = 600;
window.setGeometry(QRect( (desktop->width()-wWidth)/2 , (desktop->height()-wHeight)/2 ,wWidth,wHeight));
propertyDock->setAllowedAreas(Qt::RightDockWidgetArea);
propertyDockContents->setMaximumWidth(200);
propertyDockContents->setMinimumWidth(20);
propertyDock->setWidget(propertyDockContents);
window.addDockWidget(Qt::RightDockWidgetArea,propertyDock);
window.show();
return app.exec();
}
Is there a "correct" way to do this?
As stated in the documentation:
Note: Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
Yes! You can't creating a main window without a central widget, But you can set central widget's height to zero.
MainWindow.cpp
centralWidget()->setMaximumHeight(0);

QT: Hide the title bar of a dialog/window

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.

Resources