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

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

Related

How to play a sound before splash in QT?

I want to play a sound within the splash but with the following code I get the splash to show up and then, only when the splash is gone, the sound to play. Can someone help me out? Thank you very much.
QApplication a(argc, argv);
QMediaPlayer * splashSound = new QMediaPlayer;
splashSound->setMedia(QUrl("qrc:/sfx/splash_sound.wav"));
splashSound->play();
QSplashScreen * mainSplash = new QSplashScreen;
mainSplash->setPixmap(QPixmap(":/img/splash.png"));
mainSplash->show();
MainWindow w;
QTimer::singleShot(2500, mainSplash, SLOT(close()));
QThread::msleep(2500);
w.show();
return a.exec();
The problem is your call to QThread::msleep(2500); it prevents the Qt event loop from executing (because a.exec() can't run until after it returns), and that in turn prevents the audio from playing.
The easy fix is to remove that line and the call to w.show(), and replace them with something like this:
QTimer::singleShot(2500, &w, SLOT(show()));
... That will cause your MainWindow widget to appear at the same time the splash-image disappears.

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 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

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

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.

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.