I have an application with a Splash Screen. I need to show the splash screen to appear before the main window appears and wait 3.5 seconds. Once it finished now I need to show the main window.
Here is the code what I have tried,
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include "game.h"
#include "player.h"
#include <QSplashScreen>
#include <QObject>
Game *game;
int main(int argc, char *argv[])
{
//Splash screen
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/images/Images/1.JPG"));
splash->show();
//main window
game = new Game();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
game->displayHome();
return a.exec();
}
Game Class
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0,0,800,600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800,600);
show();
qDebug() << "yoyoyoy";
score = new Score();
health = new Health();
connect(this->health,SIGNAL(crash()),this,SLOT(gameover3()));
}
void Game::displayHome()
{
logo = new Logo();
scene->addItem(logo);
playButton = new Menubuttons(QString("Play"));
int bxPos = this->width()/2 - playButton->boundingRect().width()/2;
int byPos = 275;
playButton->setPos(bxPos,byPos);
connect(playButton,SIGNAL(clicked()),this,SLOT(startGame()));
scene->addItem(playButton);
instructions = new Menubuttons(QString("Instructions"));
int axPos = this->width()/2 - instructions->boundingRect().width()/2;
int ayPos = 350;
instructions->setPos(axPos,ayPos);
connect(instructions,SIGNAL(clicked()),this,SLOT(instruct()));
scene->addItem(instructions);
quitButton = new Menubuttons(QString("Quit"));
int qxPos = this->width()/2 - quitButton->boundingRect().width()/2;
int qyPos = 425;
quitButton->setPos(qxPos,qyPos);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
scene->removeItem(inst);
}
But it appears both at the same time. How can I fix that?
The behavior you describe is not reproduced by the complete example below
#include <QtWidgets/QApplication>
#include <qmainwindow.h>
#include <qwidget.h>
#include <qtimer.h>
#include <qsplashscreen.h>
QMainWindow* game;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/StackOverflow/SplashScreen"));
splash->show();
game = new QMainWindow();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
return a.exec();
}
Running this displays a splash screen for 3.5 seconds followed by the main window. The issue may be your implementation for the Game class or the member function displayHome().
Edit
After your edit with the Game class definition and implementation it is clear the problem is calling show() at the end of the Game::Game() constructor. This causes game to display immediately upon construction, the subsequent call to show() in QTimer::singleShot(3500, game, SLOT(show())) is redundant upon the already visible object. To fix this simply remove show() from the Game constructor, i.e. it should be
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 800, 600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800, 600);
}
Related
I'm trying to play music when the user is clicking a key. But music plays only once when I click first time. Help please.
#include <QApplication>
#include <QGraphicsView>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QKeyEvent>
#include <QDebug>
class Rect : public QGraphicsRectItem
{
public:
Rect()
{
player = new QMediaPlayer();
output = new QAudioOutput();
player->setAudioOutput(output);
output->setVolume(50);
player->setSource(QUrl("qrc:/sounds/gun.mp3"));
player->play();
}
void keyPressEvent(QKeyEvent* ev)
{
player->play();
}
private:
QMediaPlayer* player;
QAudioOutput* output;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene* scene= new QGraphicsScene();
Rect* rect = new Rect();
rect->setFlags(QGraphicsItem::ItemIsFocusable);
rect->setFocus();
rect->setRect(0, 0, 300, 400);
scene->addItem(rect);
QGraphicsView* view = new QGraphicsView(scene);
view->show();
return a.exec();
delete view;
delete scene;
delete rect;
}
........................................................................................................................................
Qt has an official media player example. link
Note: the load of the media is asynchronous. play/stop/pause will control the position of the stream so you can replay it by controlling it. Please check the example project.
I have a QTreeView and I can't find a way of making it fill the whole dialog window and resize with the window when it is resized.
Something like this:
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include <QTreeView>
class MyDialog: public QDialog
{
public:
MyDialog()
{
QHBoxLayout* l = new QHBoxLayout(this);
setLayout(l);
QTreeView* v = new QTreeView(this);
l->addWidget(v);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog d;
d.exec();
return a.exec();
}
Alright so is there any way to make this program randomly change the variables x and y every time the button is clicked i am new to programming...
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>
#include <QWidget>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
QPushButton *MainInter = new QPushButton("Push me!",window);
QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
animation->setDuration(0);
animation->setEndValue(QPoint(x,y));
Object::connect(MainInter,SIGNAL(released()),animation,SLOT(start()));
window->resize(900,500);
window->show();
return a.exec();
}
What you can do is, instead of connecting the released() signal of your button directly to your animations start() SLOT, you would create your own custom SLOT. Then you connect the button to it, handle the action, and call the animation.
First read up on how to create a custom QWidget, instead of creating top level object in your main(). Simple example here
A custom widget might look like this:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPushButton;
class QPropertyAnimation;
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
QPushButton *button;
QPropertyAnimation *animation;
public slots:
void randomizeAnim();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <ctime>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
button = new QPushButton("Push me!", this);
animation = new QPropertyAnimation(button, "pos");
animation->setDuration(0);
QObject::connect(button, SIGNAL(released()), this, SLOT(randomizeAnim()));
}
void MyWidget::randomizeAnim()
{
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
animation->setEndValue(QPoint(x,y));
animation->start();
}
And now your main.cpp can be reduced to the boilerplate code:
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new MyWidget;
window->resize(900,500);
window->show();
return a.exec();
}
Every time you click, your custom slot will handle the action and do the animation.
I am getting an error while trying to run this Application ... the error message is:
main.cpp(11): error: expression must have class type
int r = dialog.exec(); and I am not sure why!!!
I am using qmake to generate the make file... I have added the necessary files to the *.pro file since Dialog is inherited from QDialog I should have access to the function exec!
#include <QtGui>
#include <QDialog>
#include <QtUtil.h>
#include <Mathematics.h>
#include <Pair.h>
#include "View.h"
class QMesseageBox;
class QAction;
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTextEdit;
class Dialog : public QDialog {
Q_OBJECT
public:
Dialog() {
QHBoxLayout *layout = new QHBoxLayout;
// prevent left vertical box from growing when main window resized
layout->addStretch(1);
QLabel* lab_Layers = new QLabel(tr("Layers"));
d_inline = new QLineEdit;
d_inline->setText("50");
scene = new QGraphicsScene(0, 0, 500, 500);
view = new View;
layout->addWidget(view);
view->setScene(scene);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(layout);
setLayout(mainLayout);
setWindowTitle(tr("VI Smooth 0.4"));
}
private slots:
// scroll the "after" window when "before" one is scrolled (so they
// remain in sync)
private:
QAction* exitAction;
QtUtil qt;
QLineEdit* d_inline;
QGraphicsScene* scene;
QGraphicsView* view;
};
main class
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog();
// dialog.showMaximized();
int r = dialog.exec();
return 0;
}
It should look something like this. If you create a Dialog object, you need to call show(). And you also need to return app.exec() in main().
#include <QApplication>
#include <QMessageBox>
#include "Dialog.h"
int
main(int argc, char **argv) {
QApplication app(argc, argv);
argv++;
Dialog dialog;
dialog.show()
return app.exec(argc, argv);
}
I'm new to C++ and Qt, and I'm having a bit of trouble getting off the ground. I'd really appreciate some help. I want to add a menubar that will remain constant for all app screens. If I add this to the main function, the menubar shows up fine, but I know this shouldn't be in my main function:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "form.h"
#include "menu.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
QMenuBar *menu = new QMenuBar(0);
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
menu->addMenu(menu1);
menu->addMenu(menu2);
w.show();
return a.exec();
}
If I create a class for the menu bar, it does not appear. The only difference is that I've put the menu code in the constructor of the menu class and then instantiated the menu class from main.cpp:
main.cpp
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "form.h"
#include "menu.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Menu m;
m.show();
w.show();
return a.exec();
}
menu.h
#ifndef MENU_H
#define MENU_H
#include <QMenuBar>
class Menu : public QMenuBar
{
Q_OBJECT
public:
Menu(QMenuBar *parent = 0);
};
#endif // MENU_H
menu.cpp
#include "menu.h"
Menu::Menu(QMenuBar *parent)
{
QMenuBar *menu = new QMenuBar(0);
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
menu->addMenu(menu1);
menu->addMenu(menu2);
}
What am I doing wrong?
Your Menu class is derived from QMenuBar, but you don't call any methods on it except show(). All addMenu() calls are made for the local variable menu in the constructor, which are allocated and then forgotten. You should call them on this instead:
#include "menu.h"
Menu::Menu(QMenuBar *parent)
{
QMenu* menu1 = new QMenu("MENU1");
menu1->addMenu(new QMenu("menu1_SubMenu"));
QMenu* menu2 = new QMenu("MENU2");
menu2->addMenu(new QMenu("menu2_SubMenu"));
this->addMenu(menu1);
this->addMenu(menu2);
}