Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVED] How to use main args

    General and Desktop
    3
    7
    3391
    Loading More Posts
    • 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.
    • R
      roseicollis last edited by

      Hi,

      I'm making a Qt Gui application so the main its like:

      @
      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv); //Maybe I can do something with that?
      MainWindow w;
      w.show();

      return a.exec();
      

      }
      @

      I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there? The only solution I can think of is to create a new class which receives them, put them in global variables and then inherite mainwindow from the new class but I don't really like it and I suposse there is an easer way to do it.
      I've been searching the QApplication behaviour but nothing found

      Thanks!

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        You are looking for "this":http://doc.qt.io/qt-5/qcoreapplication.html#accessing-command-line-arguments part of the documentation.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 0
        • R
          roseicollis last edited by

          Yeah! That's it!

          @QStringList ArgsList = QApplication::arguments(); @

          Next time I'll say: Hi Sgaist I think XDDD Thank you for all :D

          1 Reply Last reply Reply Quote 0
          • F
            Francknos last edited by

            [quote author="roseicollis" date="1419334108"]Hi,

            I'm making a Qt Gui application so the main its like:

            @
            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv); //Maybe I can do something with that?
            MainWindow w;
            w.show();

            return a.exec();
            

            }
            @

            I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there? The only solution I can think of is to create a new class which receives them, put them in global variables and then inherite mainwindow from the new class but I don't really like it and I suposse there is an easer way to do it.
            I've been searching the QApplication behaviour but nothing found

            Thanks![/quote]

            You can do this

            @
            int main(int argc, char *argv[])
            {
            QApplication a(argc, argv); //Maybe I can do something with that?
            MainWindow w(argc, argv); // And use it in your constructor of MainW
            w.show();

            return a.exec();
            

            }
            @

            1 Reply Last reply Reply Quote 0
            • R
              roseicollis last edited by

              Hi Francknos,

              Sorry but if I do that ( I already tried it before psoting here), you can't use them in mainwindow as int a = argc; or something like that.

              Anyway as the title says, it has been already solved so thanks :)

              1 Reply Last reply Reply Quote 0
              • F
                Francknos last edited by

                Ok no problem.
                But why you want modify the argc, argv in your app ?

                1 Reply Last reply Reply Quote 0
                • R
                  roseicollis last edited by

                  I didnt want to modify them, just to use them :)

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post