how to use qt4 and irrlicht3d - c++

i want to write a code for irrlicht begin scene in a qt4 window that have a button for exit i tried many times but every tim that's open two windows and i want only to run th scene and the button in one window
my code
#include <QApplication>
#include <QLabel>
#include <irrlicht.h>
#include <QApplication>
#include <QPushButton>
using namespace irr;
using namespace core;
using namespace video;
int main(int argc, char *argv[])
{
IrrlichtDevice* device = createDevice(EDT_OPENGL,dimension2d<u32>(640, 480), 16,false, false, false, 0);
if (!device)
return 1;
IVideoDriver* driver = device->getVideoDriver();
//qt4code
QApplication app(argc, argv);
QPushButton *button = new QPushButton(QString( "Qt/Irrlicht" ));
QObject::connect(button, SIGNAL(clicked()),&app, SLOT(quit()));
button->show();
app.exec();
//endcode
while (device->run())
{
driver->beginScene(true, true, SColor(255, 255, 255,255));
driver->endScene();
}
device->drop();
return 0;
}

Related

Show main windows after the Splash Screen in C++ - QT

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

QDialog expanding base on the QLabel content

I'd like to show and then close a dialog after 5 seconds. The dialog needs to be automatically resized (horizontally and vertically) based on the content of a label. Here is my code:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.adjustSize();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
It does not not expand the dialog based on the label size. Here is how it looks:
How can I fix it? (Please also let me know if there is better way of doing this.)
I am using Qt5 in Linux.
Since you have not used a QLayout the QLabel will be displayed as large as you can, a possible request is to change the size of QDialog to the recommended size of QLabel with sizeHint():
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
void notify (int intTime=1000)
{
QDialog notify;
notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel(&notify);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
notify.resize(lbl->sizeHint());
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}
The other possible solution is to use a QLayout:
#include <QApplication>
#include <QDialog>
#include <QLabel>
#include <QTimer>
#include <QVBoxLayout>
void notify (int intTime=1000)
{
QDialog notify;
QVBoxLayout *lay = new QVBoxLayout(&notify);
//notify.setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
notify.setWindowFlag(Qt::FramelessWindowHint);
QLabel *lbl = new QLabel;
lay->addWidget(lbl);
lbl->setText("This is a test This is a test This is a test This is a test This is a test This is a test This is a test");
QApplication::processEvents();
QTimer::singleShot(intTime, &notify, SLOT(close()));
notify.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify(5000);
exit(0);
// return a.exec();
}

Running Urho3D and Qt from main

I am using the Urho3D engine with Qt for an application. The problem is that both Urho3D and QApplication require to be ran from main(). For now I am using it in separate processes but IPC makes it complicated.
Is there any way to solve this issue? Thanks
My platform is Urho3D 1.5, Qt 4.71 and Windows 7 x64 and VS2015 (C++)
I'm new to both c++ and Urho3D, but I've successfully achieved it.
Simple code, haven't had further test:
awidget.h:
#ifndef AWIDGET_H
#define AWIDGET_H
#include <QWidget>
#include <QPushButton>
#include <Urho3D/Engine/Application.h>
class aWidget : public QWidget
{
Q_OBJECT
public:
explicit aWidget(QWidget *parent = 0)
{
QPushButton *button = new QPushButton(this);
connect(button, SIGNAL(clicked()), this, SLOT(pressed()));
}
public slots:
void pressed()
{
Urho3D::Context* context = new Urho3D::Context();
Urho3D::Application *application = new Urho3D::Application(context);
application->Run();
}
};
#endif // AWIDGET_H
main.cpp:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
aWidget *widget = new aWidget();
widget->show();
return app.exec();
}
By the way, I'm using Qt 5.9.0
So the answer is quite simple. Instead of running QApplication by calling
app->exec();
it is needed to manually and regularily call this from your main loop:
app->processEvents();
This will take care that all events used by Qt are processed and QApplication will respond accordingly.
Example:
#include <QApplication>
#include <awidget.h>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
bool shallrun = true;
aWidget *widget = new aWidget();
widget->show();
while (shallrun)
{
app->processEvents();
...
}
...
}

How to hide application on taskbar?

I am trying to hide my QT application from taskbar? I cannot find anything in Google so I asking here.
Solution from Qt Hide Taskbar Item (Qt Hide Taskbar Item) and this->hide() is not helping.
main.cpp
#include "status_bar.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
status_bar w;
w.show();
return a.exec();
}
status_bar.cpp:
#include "status_bar.h"
#include "ui_status_bar.h"
#include <stdlib.h>
#include <QTime>
#include <QTimer>
#include <QApplication>
#include <QDesktopWidget>
status_bar::status_bar(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::status_bar)
{
ui->setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
resize(QApplication::desktop()->width(),36);
ui->time->move(QApplication::desktop()->width()-ui->time->size().width(),10);
ui->username->setText(getenv("USER"));
timeupdate = new QTimer(this);
connect(timeupdate, SIGNAL(timeout()),
this, SLOT(UpdateClock()));
timeupdate->start(100);
}
void status_bar::UpdateClock()
{
ui->time->setText(QTime::currentTime().toString("HH:mm"));
}
status_bar::~status_bar()
{
delete ui;
}
EDIT:
With code like this window is empty.
class MyWindowWidget : public QWidget
{
public:
MyWindowWidget(QWidget *parent)
: QWidget(parent, Qt::Dialog)
{
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
status_bar window;
MyWindowWidget widget(&window);
widget.show();
return app.exec();
}
Solved by using Qt::Tool flag.
Qt::Tool flag has other problems for me, like this widget/windows is hidden when its state becomes inactive.
I would recommend you to use Qt::ToolTip

alternating row colors in a QComboBox in mac and linux

I want to alternate the colors of a QComboBox. In Windows I have no problem using the view().setAlternatingRowColors(true) function. In Linux and Mac it looks like impossible. I tried also using style sheet (see following code) but I had the same kind of results (all rows with the same background color). Can you explain me what is my error?
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyleSheet("QComboBox QAbstractItemView{qproperty-alternatingRowColors: true;alternate-background-color: blue;background: red;}");
QComboBox b;
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
bool tt = false;
tt = b.view()->alternatingRowColors();
b.show();
return a.exec();
}
At least on my box it appears that QPalette::Base and QPalette::AlternateBase are the same color :) Changing QPalette::AlternateBase to some other color makes this code work fine:
#include <QtGui/QApplication>
#include <QComboBox>
#include <QAbstractItemView>
#include <QPalette>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QComboBox b;
b.view()->setAlternatingRowColors(true);
QPalette p = b.palette();
p.setColor(QPalette::AlternateBase, Qt::red);
b.setPalette(p);
b.addItem("MM_NONE");
b.addItem("MM_VERT");
b.addItem("MM_FACE");
b.addItem("MM_EDGE");
b.show();
return a.exec();
}