How to pass argc and argv to MainWindow object
-
I'm working in a project related to ROS. I've modified CMake and it is working fine. ROS requires to call this function
ros::init(argc,argv, std::string)
to initialize the library. Qt also needs this QApplication. To solve this issue, I've modified theMainWindow(QWidget *parent=nullptr)
toMainWindow(QWidget *parent=nullptr, int argc=0,char** argv="")
but it yields the following errorerror: cannot intialize a parameter of type 'char** with an lvalue of type 'const char[3]' note: passing argument to parameter 'argv' here
In the main function. I need this
QApplication a(argc,argv); MainWindow w(argc,argv);
Any suggestions!
-
@CroCo
As said, I don't know why you are wanting to callros::init()
fromMainWindow
anyway. When it's simple and easy to call it frommain()
.In fact if you read http://wiki.ros.org/roscpp/Overview/Initialization and Shutdown you see stuff like
As mentioned above, calling
ros::init()
withargc
andargv
will remove ROS arguments from the command lineIt certainly seems to belong next to
QApplication(argc, argv)
. And it might even be better to put it before that line in some ways. -
@CroCo said in How to pass argc and argv to MainWindow object:
MainWindow(QWidget parent=nullptr, int argc=0,char* argv="")
MainWindow w(argc,argv);
And where is your
QWidget *parent
parameter?Also you should take a look at QCoreApplication::arguments() instead passing plain argc/argv
-
@Christian-Ehrlicher QApplication::arguments().at(0).toInt() solves the first argument but I got stuck with argv. How to capture the argument as
char**
? -
Again: the signature is
QWidget *, int,char**
, you passint, char**
- how should this work? You have to pass a proper QWidget pointer (or a nullptr) or change the signature - basic C stuff. -
@CroCo said in How to pass argc and argv to MainWindow object:
ros::init(argc,argv, std::string)
Can you not do this in the
main(int argc, char **argv)
function, which is where those variables are available? Next to where you haveQApplication(argc, argv)
.MainWindow
isn't even the same thing asmain()
, it doesn't have anything to do with initialisation so why is yourros::init()
(which looks like a one-time program start-up thing to me) inside it?As far as I know, you cannot guarantee to access what is passed as
argc
,argv
to a C++main()
anywhere else, without passing those variables on. C++ does not make them available, it may have done its own processing on whatever the OS passed from its "command line" or other invocation method before passing asmain()
parameters. -
@Christian-Ehrlicher I wasn't aware of QApplication::arguments(). Now I've changed the MainWindow to its original form. Now I need to access arguments inside MainWindow constructor so I can initiate ROS. No problem with the first argument but the second one I don't know which function that convert QString to char**. I've tried
.c_str()
with no luck. -
@CroCo
As said, I don't know why you are wanting to callros::init()
fromMainWindow
anyway. When it's simple and easy to call it frommain()
.In fact if you read http://wiki.ros.org/roscpp/Overview/Initialization and Shutdown you see stuff like
As mentioned above, calling
ros::init()
withargc
andargv
will remove ROS arguments from the command lineIt certainly seems to belong next to
QApplication(argc, argv)
. And it might even be better to put it before that line in some ways. -
How about looking at the QString documentation? QString::toLatin1(), QString::toUtf8() for example.
And why do you need a char** at all? What are you trying to do?