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.
Related
This is a very simple problem to reproduce. Create a new project with Qt Creator with a QMainWindow. Using designer add a single widget (it doesn't matter which one) and then use the right button to set a layout (any layout as with a single widget the results are always the same).
There will be a gray border around the widget. I would like to remove this border so that the widget occupies the entirety of the main window area (which Qt always names centralWidget). Basically the single widget is all I want to see.
Is this possible? If so, how?
Yes, it's possible. All you need is setting margins of that centralWidget to 0. (note that you should first set up a layout)
In properties panel set the following properties to 0.
layoutLeftMargin
layoutTopMargin
layoutRightMargin
layoutBottomMargin
Also note that using a QWidget (not QMainWindow) as your main app window is perfectly valid, so if your app has only one widget, you won't need a QMainWindow at all. It's enough to show that widget.
The answer above is the right answer but I also wanted to share what I've found out. I basically wanted to create a QGraphcisView that occupies the entire screen. and I thought of the above method, which Hi I'm Frogatto answered. Another way is to create a simple app with this code:
#include <QCoreApplication>
#include <QApplication>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
QApplication app(argc, argv);
QGraphicsView *view = new QGraphicsView();
view->showFullScreen();
return app.exec();
}
And this .pro file:
QT += core gui widgets
CONFIG += c++11
TARGET = Test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
This also does what I wanted!. Maybe it can help someone.
QtCreator's designer allows you to edit user interface graphically. I was just trying to make some sense of it - what I wanted was a header text centered in the middle and some widget under it, like this:
But my results look like this, when using vertical layout:
I placed QLabel on top and QOpenGLWidget on bottom - I only used QOpenGlWidget because it has black background on screenshot. What I really plan on doing is using another QWidget. I used vertical layout then. So how do I get the result on the first image, using QLabel and QWidget?
Qt's use of XML is not a replacement for HTML.
HTML is designed for marking up web pages. Qt's widgets are not web pages!
It appears that you're looking at the box with the text 'content' and thinking of a generic widget. I see that and see a QLabel, which is derived from QWidget after all.
It's probably easier to explain in code how I would go about this, which you can then translate to doing the same in Qt Creator.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a root widget (this could also be a QMainWindow, or any other widget)
QWidget* pWidget = new QWidget;
// Layout to arrange the widgets vertically
QVBoxLayout* pBoxLayout = new QVBoxLayout;
pWidget->setLayout(pBoxLayout);
// The header widget
QLabel* pHeader = new QLabel("Header");
pHeader->setAlignment(Qt::AlignCenter);
pHeader->setMinimumSize(200, 20);
pHeader->setMaximumSize(200, 20);
QFont font = pHeader->font();
font.setBold(true);
font.setPixelSize(16);
pHeader->setFont(font);
// the content widget
QLabel* plabel = new QLabel("content");
plabel->setMinimumSize(200, 200);
plabel->setMaximumSize(200, 200);
plabel->setStyleSheet("background-color: rgb(182, 182, 182); border: 5px solid black;");
plabel->setAlignment(Qt::AlignCenter);
pBoxLayout->addWidget(pHeader);
pBoxLayout->addWidget(plabel);
pWidget->show();
return a.exec();
}
As you can see here, I've styled the content widget with the use of a Style Sheet. This is really the easiest method, after a bit of practice with them.
The resulting code produces a widget which looks like this: -
You can play with dimensions and fonts to match your original image exactly.
using qt 5.5 on linux (ubuntu), I have very simple code (in main.cpp):
//QCoreApplication a(argc, argv);
QApplication a(argc, argv);
// Test if QLabel can show the icon
QLabel *label = new QLabel();
label->setPixmap(QPixmap("icon16.png"));
label->show();
// Do the same for QSystemTray
QSystemTrayIcon i;
QIcon icon(QPixmap("icon16.png"));
i.setIcon(icon);
i.show();
// i.showMessage("hey dude!", "this is my message");
qDebug("done\n\n");
So here I am trying to dislpay a system tray icon... which works, but the icon is a red circle with a cross through it (showing that no icon is available). The commented out "showMessage()" function also works fine.
I know that the icon itself can be loaded and displayed by qt because it works for the QLabel.
The problem is that the QSystemTrayIcon is not displayed.
I have been through this post Another Persons Issue With QSystemTrayIcon, but he has a different issue to me in the end.
I can't figure out what the problem is here :(
edit
There are these links that suggest this is broke:
here
and here
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.
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();
}