[Solved] QStringList getting qApp->arguments() issues
-
Hello there !
I'm on a new project since few days and while rewriting the main() body, I have a really strange issue concerning QStringList and qApp->arguments. Actually, I have an "AppController" (It's not MVC, don't care about *Controller please ;) ) in the main which receives qApp->arguments() (if none given to the constructor) which then copy it to an internal QStringList. In the constructor, every QDebug work fine, in the method called next (::run2()) my internal QStringList seems to be empty... And sometimes cause EXC_BAD_ACCESS.
main.cpp
@#include <QApplication>
#include <QDebug>
#include <QIcon>#include "AgentController/AgentController.hpp" // Contains only declarations
#include "AppController/AppController.hpp"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
AppController *appController;a.setApplicationDisplayName("XXX"); a.setApplicationName("XXX"); a.setOrganizationDomain("XXX.com"); a.setOrganizationName("XXX"); a.addLibraryPath(a.applicationDirPath()+"/plugins"); a.setWindowIcon(QIcon(":/icons/svg")); a.setQuitOnLastWindowClosed(true); // Agent disabled for now appController = new AppController; appController->run2(); return a.exec();
}@
AppController.hpp:
@#ifndef APPCONTROLLER_HPPdefine APPCONTROLLER_HPP
include <QCoreApplication> // MAYBE... That could be the problem
include <QObject>
include <QSettings>
include <QStringList>
class AppController : public QObject
{
Q_OBJECTprivate: QSettings m_settings; QStringList const& m_argv; QStringList m_filesToBackup; public: AppController(QStringList const& argv = qApp->arguments(), QObject *parent = 0); signals: public slots: void run2(void); void quitting(void);
};
#endif // APPCONTROLLER_HPP@
AppController.cpp:
@#include <QDebug>
#include <QString>
#include <QUuid>#include <signal.h>
#include "AppController.hpp"
#include "../InitWidget/InitWidget.hpp" // Contains only declarations
#include "../AgentController/AgentController.hpp" // Contains only declarationsAppController::AppController(QStringList const& argv, QObject *parent)
: QObject(parent), m_argv(argv)
{
qDebug() << "Args:" << qApp->arguments();
qDebug() << "Args:" << argv;
qDebug() << "Args:" << this->m_argv;
if (signal(SIGUSR1, AgentController::signal_received) == SIG_ERR) {
qDebug() << "Cannot attach signal handler";
}connect(qApp, SIGNAL(aboutToQuit()), SLOT(quitting()));
}
// Signals / Slots
void AppController::run2(void)
{
// First start: Generate UUID
if (this->m_settings.value("general/uuid").toUuid().isNull()) {
this->m_settings.setValue("general/uuid", QUuid::createUuid());
}
// ---// Parameters checking // Help - Console qDebug() << "Internal argv ("+QString::number(this->m_argv.count())+"):"; // Raises EXC_BAD_ACCESS qDebug() << this->m_argv; if (this->m_argv.count() > 1 && this->m_argv.contains("--help")) { qDebug() << "Usage:"; qDebug() << this->m_argv.at(0) << "[options] [file1 ...]\n"; qDebug() << "Options:"; qDebug() << "--help This message"; qDebug() << "--agent Start in agent (background daemon) mode"; qDebug() << "--backup Start a backup process"; qDebug() << ""; exit(EXIT_SUCCESS); } // --- // [...]
}
void AppController::quitting(void)
{
qDebug() << "Quitting.";
AgentController::instance()->hide();
}
// ---
@qDebug() log:
@Args: ("XXX_PATH", "--backup", "/Users/Max13/Desktop/favicon.ico") // AppController.cpp:14 (Constructor)
Args: ("XXX_PATH", "--backup", "/Users/Max13/Desktop/favicon.ico") // AppController.cpp:15 (Constructor)
Args: ("XXX"_PATH, "--backup", "/Users/Max13/Desktop/favicon.ico") // AppController.cpp:16 (Constructor)
"Internal argv (3):" // AppController.cpp:35 (run2())
End of debug.@Can anyone tell me if I messed something up, or anything? Strangely, it raises EXC_BAD_ACCESS sometimes not everytime I start the app, debug mode or debugging mode. Now I'm writing, MAYBE it's because I'm including <QCoreApplication> instead of <QApplication> sometimes so MAYBE <QApplication> reimplements arguments() so calling it from qApp in a place or another won't give me the good behaviour.
Anyway, it's doesn't seem to the THE problem for me because I'm calling qApp->arguments() in my contructor and it works. I'm checking in case of.
Thank you for your help.
-
This does not really seem wrong in any way, apart from twisted argv declaration. Change it to the following:
@
const QStringList &argv
// instead of
QStringList const& argv
@ -
@
AppController(QStringList const& argv = qApp->arguments(),
QObject *parent = 0);
@try to change to:
@
AppController(const QStringList &a = QStringList(), ...)
{
if (a.isEmpty())
a = qApp()->arguments();
}@
-