Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to pass argc and argv to MainWindow object
Forum Updated to NodeBB v4.3 + New Features

How to pass argc and argv to MainWindow object

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CroCo
    wrote on last edited by
    #1

    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 the MainWindow(QWidget *parent=nullptr) to MainWindow(QWidget *parent=nullptr, int argc=0,char** argv="") but it yields the following error

    error: 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!

    JonBJ 1 Reply Last reply
    0
    • C CroCo

      @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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #7

      @CroCo
      As said, I don't know why you are wanting to call ros::init() from MainWindow anyway. When it's simple and easy to call it from main().

      In fact if you read http://wiki.ros.org/roscpp/Overview/Initialization and Shutdown you see stuff like

      As mentioned above, calling ros::init() with argc and argv will remove ROS arguments from the command line

      It certainly seems to belong next to QApplication(argc, argv). And it might even be better to put it before that line in some ways.

      1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #2

        @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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        C 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          @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

          C Offline
          C Offline
          CroCo
          wrote on last edited by
          #3

          @Christian-Ehrlicher QApplication::arguments().at(0).toInt() solves the first argument but I got stuck with argv. How to capture the argument as char**?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #4

            Again: the signature is QWidget *, int,char**, you pass int, char** - how should this work? You have to pass a proper QWidget pointer (or a nullptr) or change the signature - basic C stuff.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            C 1 Reply Last reply
            1
            • C CroCo

              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 the MainWindow(QWidget *parent=nullptr) to MainWindow(QWidget *parent=nullptr, int argc=0,char** argv="") but it yields the following error

              error: 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!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @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 have QApplication(argc, argv). MainWindow isn't even the same thing as main(), it doesn't have anything to do with initialisation so why is your ros::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 as main() parameters.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Again: the signature is QWidget *, int,char**, you pass int, char** - how should this work? You have to pass a proper QWidget pointer (or a nullptr) or change the signature - basic C stuff.

                C Offline
                C Offline
                CroCo
                wrote on last edited by
                #6

                @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.

                JonBJ 1 Reply Last reply
                0
                • C CroCo

                  @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.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #7

                  @CroCo
                  As said, I don't know why you are wanting to call ros::init() from MainWindow anyway. When it's simple and easy to call it from main().

                  In fact if you read http://wiki.ros.org/roscpp/Overview/Initialization and Shutdown you see stuff like

                  As mentioned above, calling ros::init() with argc and argv will remove ROS arguments from the command line

                  It certainly seems to belong next to QApplication(argc, argv). And it might even be better to put it before that line in some ways.

                  1 Reply Last reply
                  2
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    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?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved