Results 1 to 5 of 5

Thread: How to use main args

  1. #1
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to use main args

    Hi,

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

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv); //Maybe I can do something with this line?
    4. }
    To copy to clipboard, switch view to plain text mode 

    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.

    Thanks!
    Last edited by roseicollis; 23rd December 2014 at 11:19.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use main args

    First, you always need int main( int argc, char *argv[] ) because you need to pass argc and argv to QApplication. Next (seeing a Linux programmer), there are no __argc and __argv in Linux so that you need to catch argc and argv yourself when they pass by. Therefore:

    (1) Derive a class MyApplication from QApplication. Give MyApplication data items argc and argv.
    (2) In the MyApplication ctor, copy argc and argv to your data items. Now:
    Qt Code:
    1. int main( int argc, char *argv[] )
    2. {
    3. MyApplication app(argc, argv);
    4.  
    5. // etc...
    6. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use main args

    Hi!
    Yes, I'm running it on unix. Thank you for the answer.
    As I see the only way is to create an other class then.

    And then what should I do? Include of MyApplication on mainwindow.cpp or inherit from it like class MainWindow : public QMainWindow, MyApplycation ?? I can't see neither options hehe

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use main args

    Quote Originally Posted by roseicollis View Post
    I need to use those arguments (argc, argv[]) in the mainwindow. So my question is: How can I use them there?
    QCoreApplication::arguments(), QCommandLineParser.

    Cheers,
    _

  5. #5
    Join Date
    Dec 2014
    Posts
    82
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to use main args

    Yeah! That was anda_skoa, thank you!

    I let here the line for those who visit that thread after

    QStringList ArgsList = QApplication::arguments();

Similar Threads

  1. Replies: 4
    Last Post: 6th March 2019, 20:57
  2. Pass a QString as args for commandProcess
    By mikemakus in forum Newbie
    Replies: 2
    Last Post: 3rd December 2009, 23:03
  3. va_start used in function with fixed args
    By stinos in forum General Programming
    Replies: 5
    Last Post: 28th November 2008, 00:49
  4. Replies: 11
    Last Post: 11th August 2008, 09:14
  5. Bring va_list args; to a QStringList or a qmap
    By patrik08 in forum General Programming
    Replies: 8
    Last Post: 18th June 2006, 12:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.