ubuntu sdk qml Quick view window close, minimize button not visible - c++

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.

Related

Attempting to display multiple png files in one Qt Window using CPP

I am using Qt5 in cpp, and I am attempting to display multiple png files in one window. All my attempts to date, will place one png image on top of the other
The png file names are passed to the program as arguments
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item1(QPixmap((char*) argv[1]));
scene.addItem(&item1);
QGraphicsPixmapItem item2(QPixmap((char*) argv[2]));
scene.addItem(&item2);
view.show();
a.exec();
Reading through the Qt documentation, I thought QGraphicsItemGroup might hand this for me. It did not make a difference.
QApplication a(argc, argv);
QGraphicsScene scene;
QGraphicsView view(&scene);
QGraphicsPixmapItem item2(QPixmap((char*) argv[1]));
QGraphicsPixmapItem item3(QPixmap((char*) argv[2]));
QGraphicsItemGroup grp;
grp.addToGroup(&item2);
grp.addToGroup(&item3);
scene.addItem(&grp);
view.show();
a.exec();
These are both examples both build, but both have the same error, one png is on top of the other. I am trying to get both png files to show in the same window.
Any suggestions would be appreciated. Thanks.
If you want the 2 images next to one another, you need to control their position yourself.
Have a look at QGraphicsAnchorLayout in
Simple Anchor Layout Example
and QGraphicsLayout in
Graphics View Flow Layout Example

QSlider with ticks not properly drawn

Enabling a ticks for QSlider seems to mess up the sizeHint of the slider itself.
Consider this simple code:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSlider ds{Qt::Horizontal};
ds.setRange(0, 100);
ds.setTickPosition(QSlider::TicksAbove);
ds.setTickInterval(20);
ds.show();
return a.exec();
}
and this is how is rendered:
notice how the slider is cropped below.
this behaviour is the same in a complex widget too of course:
Resizing the window of the first slider tick position does not follow the slider itself.
So the question is how have a QSlider rendered properly with ticks enabled?
I would recommend to add VerticalSpacers at the top and bottom, which are set to "expanding" So that the slider is sandwiched. So when you resize the whole widget, for vertical changes th slider stays minimumHeight, but for horizontal changes it expands (at least this is what I expect what you want to achieve).
The practical way I found to solve this is to set the minimum size manually to the bare minumum to avoid the incorrect render:
ds.setMinimumHeight(30);

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.

Qt window frame design

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.

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.