How to specify title icon and dock icon separately in Qt - c++

I'm using the code below to specify the app icon. But that would change both the title and dock icons.
QApplication app(argc, argv);
QIcon icon(":res/image/my-icon.png");
app.setWindowIcon(icon);
Is there any way to specify them separately?

Related

What is the rule for choosing "central widget" in QMainWindow? and why is it important?

I understand setCentralWidget is required in QMainWindow implementation, and at first sight, it seemed extremely self-explaining what central widget means. But is there a more strict definition of "central"?
Say, I have several equally important widgets located in the central area of the window, should I always find a way to group them together and set the group to be the central widget? or I could just randomly choose one?
More importantly, what happens to the non-central widgets? Are there certain differences between central and non-central widgets that might impact their behaviors later?
The Qt documentation says nothing about this, other than simply stating central widget is important, which is not very helpful.
The central word in the setCentralWidget() method has nothing to do with the importance but if you check the layout that has the QMainWindow we will see that it is in the central position:
should I always find a way to group them together and set the group to be the central widget? Or I could just randomly choose one? There can only be one centralwidget, so if you want to have several widget in the central position you must create a new widget that is the container and set the other widget through the layouts.
what happens to the non-central widgets? Are there certain differences between central and non-central widgets that might impact their behaviors later? There's no difference.
Say in your central widget box above, I place two QLabels symmetrically with respect to the exact central point. In this case, which QLabel should be the central widget? Either one is fine?
You do not have to choose, you can the 2 QLabels be part of the centralWigdet, centralwidget only refers to the central position, for example:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QLabel left_label("left");
left_label.setAlignment(Qt::AlignCenter);
QLabel right_label("rigth");
right_label.setAlignment(Qt::AlignCenter);
QWidget *central_widget = new QWidget;
QHBoxLayout *lay = new QHBoxLayout(central_widget);
lay->addWidget(&left_label);
lay->addWidget(&right_label);
w.setCentralWidget(central_widget);
w.show();
return a.exec();
}
If either widget is fine, why is setting central widget so critically required by QMainWindow?
You do not necessarily have to set a centralwidget, but QMainWindow unlike other widget already has a certain layout, so if you want to place the widgets you must use that method.
The centralwidget refers to a relative position but it is not exactly a central position:
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
QLabel *central_widget = new QLabel("Central Widget");
central_widget->setAlignment(Qt::AlignCenter);
w.setCentralWidget(central_widget);
QDockWidget *dock = new QDockWidget("left");
w.addDockWidget(Qt::LeftDockWidgetArea, dock);
dock->setWidget(new QTextEdit);
w.show();
return a.exec();
}
Usually you don't need a QMainWindow!
If you don't need docking nor MDI, then don't use QMainWindow: a QWidget or a QDialog will do instead.
Central means "in the center", as in "in the middle", not as in "important!". QMainWindow's provides docking and multiple document interface (MDI) functionality, and there the notion of a central widget is useful. There can only be one central widget, so "which of many" should be made central is moot: you can't have more than one. One of the fundamental aspects of Qt object design is that QObject is a container of other objects, and so is a QWidget: it's a canvas that you can place other widgets on. So, if you absolutely need docking and/or MDI, then the central widget will be any QWidget that you put other non-docking widgets on. That' all.

Qt set common background for all dialog boxes

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.

Is it possible to remove borders a single QWidget around a QMainWindow?

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.

Qt QSystemTrayIcon::setIcon or QIcon() not working

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 can i resize QVBoxLayout to fill Form without space?

How can i resize QVBoxLayout to fill Form without space?
with space:
http://0000.2.img98.net/out.php/i25106_with-space.png
without space:
http://0000.2.img98.net/out.php/i25107_without-space.png
You have to set margins for layout
example:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QVBoxLayout* l = new QVBoxLayout();
l->setMargin(0); ///m
QWidget w; w.setFixedSize(300,300);
w.setLayout(l);
l->addWidget(new QPushButton("fd"));
w.show();
return app.exec();
}
You need to adjust the size policy of the widgets contained in the layout.
For example, if you want a widget to take up all the available vertical space in a layout, set the widget's size policy to Expanding.
Go to the project folder and find the xml file which you cannot edit under Qt Creator. You will find the margin xml tag. Increase or decrease the value as you need. The xml file cannot be edited under Qt Creator.