How to apply the design of window frame?
Not Qt::FramelessWindowHint , but Windows 7 frame
edit:
How to create your own frame in QStyle?
If you talk about frame style, it will be good solution.
#include <QtGui/QApplication>
#include <QWindowsStyle>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle(new QWindowsStyle);
MainWindow w;
w.show();
return a.exec();
}
But Qt has many other styles - learn about QMotifStyle and QCleenlooksStyle... [link]
You can try Coffe or Pagefold styles.
You can set a style sheet on an individual child widget, on a whole window, or even on a whole application by calling QWidget::setStyleSheet() or QApplication::setStyleSheet().
Frames are usually the business of the windowing system and cannot be freely re-styled by the application. You'd probably need to create a frameless window with mentioned hint and paint your own title bar/frame inside the widget.
Related
I am working on developing a Qt 5 widgets desktop application where I want to give a common background for all windows and dialog boxes that pop up. The problem is that for each window I have to specify the same piece of code over and over again to load the same background. I am also using paint function override so as not to distort the background when window is resized. Here's my code:
SettingsDialog::SettingsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SettingsDialog)
{
ui->setupUi(this);
pixmapBg.load(":/images/google-material-design-wallpaper-10.jpg");
}
void SettingsDialog::paintEvent(QPaintEvent *pe)
{
QPixmap pixmapBgL = pixmapBg.scaled(this->size());
QPalette palette;
palette.setBrush(QPalette::Background, pixmapBgL);
this->setPalette(palette);
}
Is there a way to accommodate this in Qt using a single file rather than mentioning it for each window?
Yes, you can! You will have to provide your own stylesheet, or initialize your application by calling QApplication::setStyleSheet(styleName).
Follow up from comment: setStyleSheet is the quickest approach, i.e.
qApp->setStyleSheet("QDialog, QMessageBox {background-image: url(:/images/google-material-design-wallpaper-10.jpg);}");
assuming you have a valid QApplication reference qApp. Note that you can also refer to your custom subclasses as well, if you want to refine the scope of the stylesheet.
Here is some code using QApplication::setStyleSheet:
QString styleSheet = "QWidget{\
background-color: yellow\
}"//style sheet in CSS style
int main(int argc, char** argv){
QApplication app(argc, argv);
app.setStyleSheet(styleSheet);//will set all the QWidgets' background color to yellow
return app.exec();
}
There is actually a background-image property but I'm not sure about which widgets are supporting it so you can check right there.
Using Qt5.6.1, I am using QtWinMigrate to house a Qt widget in a parent window. When the parent app is scaled for a high-dpi monitor, the following line:
pApp = new QApplication( argc, argv );
kills the scaling, resizing the parent window so that 150% or 200% scaling apparently jumps down to 100%.
This is before I've even created my Qt window - just the constructor of QApplication does this.
I've tried various tricks. A qt.conf file like this:
[Platforms]
WindowsArguments = dpiawareness=0,1,2
or before creating the QApplication:
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setDesktopSettingsAware(true);
None of it seems to have an effect. How can I prevent this resizing?
In qt.conf we supposed to use one value for DPI Awareness setting:
[Platforms]
WindowsArguments = dpiawareness=2
# either 0 or 1 or 2
If you want to use this dpiawareness of qt.conf setting and prevent the Qt GUI from being automatically scaled, use value 2 which is Per Monitor DPI Aware and not handled by Qt. "Per-monitor DPI aware" used for allowing the system to send an event down to the window while being dragged from one monitor to another. It is just like the GUI app will behave with natural API, either handling or not handling that event. The rest of UI scaling is just disabled in this case and but either DPI Unaware (0) or System DPI Aware (1) will do the scaling. Qt High DPI Displays article describes these modes but apparently not sufficiently clear so I am telling you from my own experience.
You can also not use either qt.conf at all or not use dpiawareness attribute and that will also not scale the GUI.
Talking about QApplication constructor, it does watch the app DPI settings. The common pattern for the Qt app starter it:
#include "mywidget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// it is usually on the stack
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
.. so you don't have to do new QApplication( argc, argv ) or to allocate application object on the heap at all.
Fast question
In a QT C++ project, there are 2 main windows (focus can be set indiferently to any of them), and a tool window which shall be on top of the 2 main windows.
How to implement such feature?
Detailed question:
For one main window and one tool window, is quite easy to solve:
#include <QApplication>
#include <QWidget>
int main( int n, char* args[])
{
QApplication app(n, args);
QWidget mainWindow;
QWidget subWindow(&mainWindow);
subWindow.setWindowFlags(subWindow.windowFlags() | Qt::Tool);
mainWindow.show();
subWindow.show();
return app.exec();
}
The main window is always below the tool window.
The interaction with the main window is possible
minimizing/closing the main window will affect the tool window
Another application could cover the main window or both windows (they are not top-most)
I would like the same feature, but having 2 main window. Imagine a Video player in which the tool window provide "play/stop" control over both images:
Each main window has the same focus weight: focus could be given to any of them, which would cover the other, but never the tool window.
The tool is always on top of the two others.
You can raise() the tool window above the others.
This may help: void QWidget::raise() documentation
Also, see the "Note" on that function.
Add Qt::WindowStaysOnTopHint to the flags
This is my first time using QT and the Ubuntu SDK. In order to restrict the view size, i set a minimum and maximum height/width for the view:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setMaximumHeight((600));
view.setMaximumWidth((800));
view.setMinimumHeight((600));
view.setMinimumWidth((800));
view.show();
return app.exec();
}
However after adding the Max/min height/width attributes, the minimize and close buttons have disappeared from the application. Any way i can bring them back while maintaining the restriction of the view size? I have tried searching but couldn't find similar issue.
Thanks.
A quick workaround is to use setMaximumHeight/Width and set them to +1.
QQuickView view;
view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setMaximumHeight((601));
view.setMaximumWidth((801));
view.setMinimumHeight((600));
view.setMinimumWidth((800));
This way the window can't resize any more than that 1 pixel and at the same time, the minimize, close buttons don't disappear.
I know that there is a function QWidget::setWindowOpacity(qreal level) but as written in the documentation this does only work for windows.
Is there a way to make widgets that are lying inside layouts opaque too?
What I'm trying to do is an animation where widgets are fading in. I once did that with a preferences-dialog and there it worked.
So do you think there is a way or a work-around to achieve opacity for widgets inside layouts? How would you do that?
Thanks in advance!
Just use QGraphicsOpacityEffect in order to achieve this effect.
Qt4: http://doc.qt.io/qt-4.8/qgraphicsopacityeffect.html
Qt5: http://doc.qt.io/qt-5/qgraphicsopacityeffect.html
Qt6: https://doc.qt.io/qt-6/qgraphicsopacityeffect.html
Well for widgets inside mainwidow appear to have setAutoFillBackground(False) by default.
to make it fade in fadeout u need to to use QGraphicsOpacityEffect along with setAutoFillBackground(True)
a small example: write inside the widget which is called inside the mainwindow
op=QGraphicsOpacityEffect(self)
op.setOpacity(1.00) #0 to 1 will cause the fade effect to kick in
self.setGraphicsEffect(op)
self.setAutoFillBackground(True)
SetWindowOpacity works for me in Linux. I used code like this to change window opacity, (value is from 0 to 100):
setWindowOpacity(qreal(value)/100);
mywidget.setStyleSheet('background-color:rgba(r, g, b, alpha);')
works for me
In Qt5 you can use css to make widgets transparent
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDialog dialog;
dialog.setStyleSheet(QLatin1String("#LolButton{color: transparent; background-color: transparent;}"));
QPushButton button(&dialog);
button.setText("Button");
button.setObjectName(QStringLiteral("LolButton"));
QObject::connect(&button,&QPushButton::clicked,[](){
QMessageBox msg;
msg.setText("LolButton omg");
msg.exec();
});
dialog.show();
return a.exec();
}