Results 1 to 6 of 6

Thread: how to use arguments in Qt?

  1. #1
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to use arguments in Qt?

    Hi,

    I want to make a little program to make archives.
    I'm currently at the stage that I have written the GUI and I want to display the name of the file that has been passed as an argument in a QLineEdit field so that it can be changed.
    I tried to use argument() and also argc and argv, both attempts were unsuccessful.
    Maybe you can show me what needs to be done to show the name of a file?
    (or point me to a tutorial with a good explanation. Because the reason that I ask this question is that I couldn't find a good example)
    Example:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "verticalbox.h"
    3.  
    4. int main(int argc, char *argv[])
    5. { QApplication app(argc, argv);
    6. VerticalBox window;
    7. window.show();
    8. return app.exec();}
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #ifndef VERTICALBOX_H
    2. #define VERTICALBOX_H
    3. #include <QWidget>
    4.  
    5. class VerticalBox : public QWidget
    6. { public:
    7. VerticalBox(QWidget *parent = 0);};
    8.  
    9. #endif
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include <QApplication>
    2. #include "verticalbox.h"
    3.  
    4. #include <QVBoxLayout>
    5. #include <QHBoxLayout>
    6. #include <QPushButton>
    7. #include <QLabel>
    8. #include <QLineEdit>
    9.  
    10. VerticalBox::VerticalBox(QWidget *parent) : QWidget(parent)
    11. {
    12. QVBoxLayout *vbox = new QVBoxLayout(this);
    13. QHBoxLayout *contentBox = new QHBoxLayout();
    14. QHBoxLayout *buttonBox = new QHBoxLayout();
    15.  
    16. //HBox contentBox containing the QLabel and QLineEdit:
    17. QLabel *name = new QLabel("Name: ", this);
    18. QLineEdit *name_in = new QLineEdit(this);
    19. //name_in->setText(/*the part where I want the name of the file in the arguments*/);
    20. contentBox->addWidget(name);
    21. contentBox->addWidget(name_in);
    22.  
    23. //HBox buttonBox with the ok and cancel buttons
    24. QPushButton *ok = new QPushButton("OK", this);
    25. QPushButton *cancel = new QPushButton("Cancel", this);
    26. connect(cancel, SIGNAL(clicked()), qApp, SLOT(quit()));
    27. buttonBox->addWidget(ok, 1, Qt::AlignRight);
    28. buttonBox->addWidget(cancel, 0, Qt::AlignRight);
    29.  
    30. //VBox vbox, which contains the two HBoxes:
    31. vbox->addLayout(contentBox);
    32. vbox->addLayout(buttonBox);
    33. }
    To copy to clipboard, switch view to plain text mode 

    Uploaded with ImageShack.us

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to use arguments in Qt?

    For example, add a second parameter in VerticalBox constructor:
    Qt Code:
    1. VerticalBox( const QString& fileName = QString(), QWidget * parent = NULL )
    To copy to clipboard, switch view to plain text mode 
    And use it to setup lineEdit in constructor:
    Qt Code:
    1. VerticalBox::VerticalBox(const QString& fileName,QWidget *parent) : QWidget(parent)
    2. {
    3. QVBoxLayout *vbox = new QVBoxLayout(this);
    4. QHBoxLayout *contentBox = new QHBoxLayout();
    5. QHBoxLayout *buttonBox = new QHBoxLayout();
    6.  
    7. //HBox contentBox containing the QLabel and QLineEdit:
    8. QLabel *name = new QLabel("Name: ", this);
    9. QLineEdit *name_in = new QLineEdit(this);
    10. name_in->setText(fileName);
    11. contentBox->addWidget(name);
    12. contentBox->addWidget(name_in);
    13. ...
    To copy to clipboard, switch view to plain text mode 

    In order to pass the "fileName" argument you can use argv array:
    Qt Code:
    1. ...
    2. QApplication app(argc, argv);
    3. const QString toPass = argc > 1 ? QString(argv[1]) : QString();
    4. VerticalBox window( toPass );
    To copy to clipboard, switch view to plain text mode 
    argv[0] is usually name of the program, argv[1], argv[2], ... are parameters passed.

    You can also use QCoreApplication::arguments()
    Last edited by stampede; 23rd January 2011 at 15:11.

  3. The following user says thank you to stampede for this useful post:

    mr_kazoodle (23rd January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use arguments in Qt?

    Thank you very much. This was exactly what I needed

  5. #4
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use arguments in Qt?

    ...
    You can also use QCoreApplication::arguments()
    I'm afraid that I can't because QCoreApplication is meant to be used by non-GUI applications.
    (don't worry I have successfully implemented the other way you showed in the example, though a little bit different )

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to use arguments in Qt?

    Quote Originally Posted by mr_kazoodle View Post
    I'm afraid that I can't because QCoreApplication is meant to be used by non-GUI applications.
    Sure you can. If it makes you more comfortable you can call QApplication::arguments(). Since QApplication inherits QCoreApplication, QCoreApplication::arguments() will be called anyway.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use arguments in Qt?

    thanks again, I tried it and got it working as well
    Qt Code:
    1. QString toPass;
    2. const QStringList arguments = QCoreApplication::arguments();
    3. if(arguments.count()==1) toPass = "";
    4. else toPass = arguments.at(1);
    5.  
    6. VerticalBox window(toPass);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QProcess Arguments
    By doggrant in forum Qt Programming
    Replies: 0
    Last Post: 4th November 2010, 16:45
  2. qmake conditionals/arguments
    By tbscope in forum Qt Tools
    Replies: 2
    Last Post: 10th August 2010, 07:21
  3. Using Qlibrary on a function that uses arguments
    By schall_l in forum Qt Programming
    Replies: 8
    Last Post: 9th September 2008, 21:58
  4. QVector arguments
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 4th December 2007, 10:47
  5. Signal arguments limit to ?
    By ucntcme in forum Qt Programming
    Replies: 5
    Last Post: 1st March 2006, 17:37

Tags for this Thread

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.