QML application - c++

My program has two modes GUI(on qml) and no GUI(command line).What code I must write for changing mode by passing argument in cmd
for example if I pass nameofapp.exe -no-gui must be work no-gui version
for simple qt/c++ application we have this code
QCoreApplication* createApplication(int &argc, char *argv[])
{
for (int i = 1; i < argc; ++i)
if (!qstrcmp(argv[i], "-no-gui"))
return new QCoreApplication(argc, argv);
return new QApplication(argc, argv);
}
int main(int argc, char* argv[])
{
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
if (qobject_cast<QApplication *>(app.data())) {
// start GUI version...
} else {
// start non-GUI version...
}
return app->exec();
}
It's from documentation,I want such of this for qml
OS is windows 7 ultimate but I don't think that it's depends on OS

You need at least one top level QObject per application type which will "make things happen" in the main event loop.
Assuming that you have made a clear separation of concern between the view and the backend (eg MVC), you will have 3 classes (which are QObject) :
MyAppBackend which does the actual work
MyGUIMainWindow which manage GUI input\outpout.
MyCLIMainObject which manage command line input\outpout
Each one should have a method to start doing work. For instance just showing a widget is how simple Qt apps start.
int main()
{
QApplication app;
MyGUIMainWindow window;
window.show();
return app.exec();
}
window.show() will queue an event which will be processed when app.exec(). is executed.
You need to do the same with a function which either post an event or emit a signal.
int main(int argc, char* argv[])
{
QScopedPointer<QCoreApplication> app(createApplication(argc, argv));
MyAppBackend backend;
backend.startDeffered();
if (qobject_cast<QApplication *>(app.data())) {
// start GUI version...
MyGUIMainWindow* window = new MyGUIMainWindow(0,...);
window->show();
} else {
// start non-GUI version...
MyCLIMainObject* cliobj = new MyCLIMainObject(0,...);
cliobj->showCLi();
}
return app->exec();
}

Related

How to auto restart a Qt application when it crashes, within the same program?

Is there any relatively "standard" design to auto restart a Qt application program, when it crashes abnormally?
Specific to Windows, do I have to use any windows service?
Or if I have to write another program separately, then how to do that?
Here's how you might do it using a single application that can act either as a monitor or as business logic. It's akin to Jon Harper's answer, except in code, not prose :)
Of Note
The monitor should not instantiate a QApplication nor QGuiApplication: it has no UI. Otherwise, redundant running process indicators will appear on some platforms (i.e. OS X, Win 10).
The monitor/business logic selection is achieved via setting an environment variable in the called process.
Passing the monitor/business logic selection via command line arguments is problematic, as the command line switch would need to be filtered out -- doing that portably without running into corner cases is tricky.
The monitor process forwards the console I/O of the business logic process, as well as the return code.
// https://github.com/KubaO/stackoverflown/tree/master/questions/appmonitor-37524491
#include <QtWidgets>
#include <cstdlib>
#if defined(Q_OS_WIN32)
#include <windows.h>
#else
static void DebugBreak() { abort(); }
#endif
static int businessLogicMain(int &argc, char **argv) {
QApplication app{argc, argv};
qDebug() << __FUNCTION__ << app.arguments();
QWidget w;
QHBoxLayout layout{&w};
QPushButton crash{"Crash"}; // purposefully crash for testing
QPushButton quit{"Quit"}; // graceful exit, which doesn't need restart
layout.addWidget(&crash);
layout.addWidget(&quit);
w.show();
QObject::connect(&crash, &QPushButton::clicked, DebugBreak);
QObject::connect(&quit, &QPushButton::clicked, &QCoreApplication::quit);
return app.exec();
}
static char const kRunLogic[] = "run__business__logic";
static char const kRunLogicValue[] = "run__business__logic";
#if defined(Q_OS_WIN32)
static QString getWindowsCommandLineArguments() {
const wchar_t *args = GetCommandLine();
bool oddBackslash = false, quoted = false, whitespace = false;
// skip the executable name according to Windows command line parsing rules
while (auto c = *args) {
if (c == L'\\')
oddBackslash ^= 1;
else if (c == L'"')
quoted ^= !oddBackslash;
else if (c == L' ' || c == L'\t')
whitespace = !quoted;
else if (whitespace)
break;
else
oddBackslash = false;
args++;
}
return QString::fromRawData(reinterpret_cast<const QChar*>(args), lstrlen(args));
}
#endif
static int monitorMain(int &argc, char **argv) {
#if !defined(Q_OS_WIN32)
QStringList args;
args.reserve(argc-1);
for (int i = 1; i < argc; ++i)
args << QString::fromLocal8Bit(argv[i]);
#endif
QCoreApplication app{argc, argv};
QProcess proc;
auto onFinished = [&](int retcode, QProcess::ExitStatus status) {
qDebug() << status;
if (status == QProcess::CrashExit)
proc.start(); // restart the app if the app crashed
else
app.exit(retcode); // no restart required
};
QObject::connect(&proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), onFinished);
auto env = QProcessEnvironment::systemEnvironment();
env.insert(kRunLogic, kRunLogicValue);
proc.setProgram(app.applicationFilePath()); // logic and monitor are the same executable
#if defined(Q_OS_WIN32)
SetErrorMode(SEM_NOGPFAULTERRORBOX); // disable Windows error reporting
proc.setNativeArguments(getWindowsCommandLineArguments()); // pass command line arguments natively
env.insert("QT_LOGGING_TO_CONSOLE", "1"); // ensure that the debug output gets passed along
#else
proc.setArguments(args);
#endif
proc.setProcessEnvironment(env);
proc.setProcessChannelMode(QProcess::ForwardedChannels);
proc.start();
return app.exec();
}
int main(int argc, char **argv) {
if (qgetenv(kRunLogic) != kRunLogicValue)
return monitorMain(argc, argv);
else
return qunsetenv(kRunLogic), businessLogicMain(argc, argv);
}
If an application crashes, it's done.
Your monitor idea is a good one, and can be achieved using QProcess. Use the "monitor" to bootstrap your actual application. To do this, implement a monitoring object with a QProcess member. In pseudocode:
class MonitorObject : public QObject
{
...
public Q_SLOTS:
void onStarted();
void onFinished(int, QProcess::ExitStatus);
...
private:
QProcess m_process;
}
Then in main:
Create a QCoreApplication and a monitoring object on the stack.
Send a queued signal to your monitor object so it knows when the main event loop starts. You can achieve this using QMetaObject::invoke with a Qt::QueuedConnection:
int main(...)
{
QCoreApplication app;
MonitorObject monitor;
... // other initialization code here
QMetaObject::invoke(&monitor, "onStarted", Qt::QueuedConnection);
return app.exec();
}
And in your MonitorObject:
Connect QProcess's finished signal to onFinished.
When MonitorObject::onStarted is called, start the process.
When the QProcess::finished signal fires, either restart the offending program or exit, depending on the exitCode argument in the emitted signal.
I don't know of any standard Qt method for restarting apps when they crash. But there is a nice class available which makes writing a supervisor/monitor class very easy. It is called QProcess.
You can start the process like this:
monitorClass::startProcess(QString commandLine) // e.g. "c:\mytestapp.exe param1 param2"
{
mp_Process = new QProcess(this);
mp_Process->start(commandLine);
mp_Process->waitForStarted();
// Start a timer
mp_Timer->start(1000);
}
Then when the timer expires (every second - or whatever)
void monitorClass::TimerExpired(void)
{
switch (mp_Process->state())
{
default:
case QProcess::NotRunning:
{
qDebug("Process has stopped un-expectedly\n");
// Tell the supervisor that the process has terminated
// restart the process
startProcess("c:\mytestapp.exe param1 param2"); // just an example
break;
}
case QProcess::Starting:
case QProcess::Running:
{
qDebug("Process is running ok\n");
break;
}
}
}
Note
This is really pseudo code, its not a compilable example - it is just to show you roughly the how easy it is to do this with QProcess...

maximizing the already running instance of a single instance app in qt

I have made an application to run only single instance using shared memory in qt.My code looks like this
int main(int argc, char *argv[])
{
QSharedMemory sharedMemory;
sharedMemory.setKey("4da7b8a28a378e7fa9004e7a95cf257f");
if(!sharedMemory.create(1))
{
return 1; // Exit already a process running
}
QApplication a(argc, argv);
Encoder *encoder = Encoder::instance();
encoder->show();
return a.exec();
}
Now, I need to show the already running instance to the user (Maximize the window), when they try to run another instance. How can I achieve this?
There is an easy setup using QtSingleApplication instead :
QtSingleApplication app("myapp",argc, argv);
if (app.isRunning()) {
QListIterator<QString> it(messagestosend);
QString rep("Another instance is running, so I will exit.");
bool sentok = false;
while(it.hasNext()){
sentok = app.sendMessage(it.next(),1000);
if(!sentok)
break;
}
rep += sentok ? " Message sent ok." : " Message sending failed; the other instance may be frozen.";
return 0;
}
To receive this message you should listen with your desired slot to the signal
void QtSingleApplication::messageReceived(const QString&)

how to restart an application in qt?

I do this works for restarting my game but program has error .I want to show a QDialog when user losses .In this QDilag i put two pushbutton for retry and exit.also i have a QDialog for beginning of game.Where is my mistake? (I read similar questions and do according these but yet i have problem)
extern int const EXIT_CODE_REBOOT;
mydialog_end::mydialog_end(QWidget *parent) :
QDialog(parent
{
retry=new QPushButton(this);
exit=new QPushButton(this);
retry->setText("RETRY");
exit->setText("EXIT");
connect(retry,SIGNAL(clicked()),this,SLOT(on_retry_clicked()));
connect(exit,SIGNAL(clicked()),this,SLOT(on_exit_clicked()));
}
void mydialog_end::on_retry_clicked()
{
qApp->exit(EXIT_CODE_REBOOT);
accept();
}
void mydialog_end::on_exit_clicked()
{
//what do i do for end of game?
reject();
}
//////////////in class myenemy///////
public slots:
void loss();
void Myenemy1::loss()
{
if(this->collidesWithItem(_mario))
{
//do something....
mydialog_end dialog;
dialog.exec();
}
}
//////////////in main////////////
extern int const RESTART_CODE;
int main(int argc, char *argv[])
{
Mydialogstart dlg;//a dialog for beginning game
int state= dlg.exec();
int return_from_event_loop_code=0;
do
{
QApplication a(argc, argv);
MainWindow w;
if( state==QDialog::Accepted)
{
w.show();
qDebug()<<"accepted";
}
else if(state==QDialog::Rejected)
{
qDebug()<<"rejected";
dlg.close();
return 0;
}
return_from_event_loop_code = a.exec();
} while(return_from_event_loop_code==RESTART_CODE);
return return_from_event_loop_code;
}
You can use QProcess::startDetached to run an instance of your application in a new process and detach from it. After that you should exit the application :
QProcess process;
process.startDetached("myApp",QStringList());
qApp->quit();
Here myApp is the name of the executable file of the application. On Windows it can be myApp.exe.
On this one, I would make a little inception... let's say your main application is called A then you should run A in a global B application. When A crashes, B throws the QDialog. If the use click on Retry then kill the old instance of A and start a new one.
There is a Qt Wiki entry that explains what you need to do in quite a lot of detail.
As it seems you have at least partially taken inspiration from there, but from what you post here, you seem to have never initialized the values for EXIT_CODE_REBOOT and RESTART_CODE in your code sample, or at least linked them to one another (which I would expect you'd want to do in some way)

Qt process stays in memory after application closes

i have simple application that start QDialog from its main like this :
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(resources);
QApplication app(argc, argv);
QCoreApplication::setApplicationName(APP_NAME);
QCoreApplication::setApplicationVersion(APP_VERISON);
QCoreApplication::setOrganizationDomain(APP_DOMAIN);
app.setStyle("WindowsXP");
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
AuthenticationDialogContainer *pAuthenticationDialogContainer = new AuthenticationDialogContainer();
if(pAuthenticationDialogContainer->exec() != QDialog::Accepted ) {
return 0;
}
return app.exec();
}
when its pass the end of the application that is after app.exec() and the application doing what is suppose to do . when i open the windows xp task manager i see that the process is still in memory and i need manually kill it . how can i prevent it from happening ?
QDialog::exec is a blocking call: this code show and close the dialog before the QApplication start.
You can use QDialog::show and handle the return code in QDialog::accept method.

Constructing / destructing QApplication causes QWebView to mess up rendering of HTML

We need to create & destroy instances of QApplication, as we want to use Qt in a plug-in to an existing host application.
void multiQT()
{
int argc = 0;
QApplication app(argc, NULL);
QWebView view;
view.setHtml("<html><head><title>Title</title></head><body><h1>Hello World</h1></body></html>");
view.show();
app.exec();
}
main(int argc, char** argv)
{
// First call works fine, QWebView renders the HTML just fine
multiQT();
// Second call fails, QWebView strips HTML tags from HTML text and
// and renders "TitleHello World"
multiQT();
}
When showing the QWebView the second time, it does not render the HTML properly. Do we need to do some additional (re-)initializations in QApplication or QWebView?
You might have run into something that has been very lightly tested, the QApplication object among others creates/holds some of the rendering context information of widgets, I don't think it was ever planned for people to take it down and put it back up again. There might be some static content that does not get reinitialised correctly when somebody tries what you are trying to do.
You are supposed to have only 1 QApplication object, and only 1 call to exec(). Maybe you should try this.
QWebView * multiQT()
{
QWebView *view = new QWebView;
view->setHtml("<html><head><title>Title</title></head><body><h1>Hello World</h1></body></html>");
view->show();
return view;
}
main(int argc, char** argv)
{
QApplication app(argc, NULL);
QWebView * web0 = multiQT();
QWebView * web1 = multiQT();
app.exec();
}