How to play a sound before splash in QT? - c++

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.

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

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

Disable resizing of a form

I want to disable resizing my form - here is what I have tried.
I have changed the resize policy of the form to the following
HorizontalPolicy:Fixed
VerticalPloicy: Fixed
I have also tried the following
Form *w = new Form();
w->setFixedSize(w->size());
w->show();
But the form still gets resized by dragging the corners. Any suggestions ?
It definitely has to be possible.
Firstly you should know, that before window is actually shown it has no information of it's size - so size will probably return 0 (or invalid; or anything ;) ) at this point - it would probably mess up entire sizing and is therefore silently rejected. I would try
Form *w = new Form();
//w->ensurePolished();
w->setFixedSize(w->sizeHint());
w->show();
Size hint should have correct value no matter what. QWidget::ensurePolished() might be necessary here, but I recommend trying first without it - if it works, why complicate things?
If it still doesn't work, then you can simply try overriding resizeEvent() and setting the only right size for your widget if user resizes it to anything else. This will still give user an illusion of resize-ability (cursors changes on the edges and so on), so it's really the last option.
EDIT:
#include <QtGui/QApplication>
#include <QWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;
w.setFixedSize(500,500);
w.show();
return a.exec();
}
Result: widget cannot be resized. So it is not change to QMainWindow that helped - oat least on my system a simple widget can do it too ;)

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.

Is it possible to set the opacity of qt widgets?

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();
}