PDA

View Full Version : how to use arguments in Qt?



mr_kazoodle
23rd January 2011, 14:39
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:

#include <QtGui/QApplication>
#include "verticalbox.h"

int main(int argc, char *argv[])
{ QApplication app(argc, argv);
VerticalBox window;
window.show();
return app.exec();}

#ifndef VERTICALBOX_H
#define VERTICALBOX_H
#include <QWidget>

class VerticalBox : public QWidget
{ public:
VerticalBox(QWidget *parent = 0);};

#endif

#include <QApplication>
#include "verticalbox.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>

VerticalBox::VerticalBox(QWidget *parent) : QWidget(parent)
{
QVBoxLayout *vbox = new QVBoxLayout(this);
QHBoxLayout *contentBox = new QHBoxLayout();
QHBoxLayout *buttonBox = new QHBoxLayout();

//HBox contentBox containing the QLabel and QLineEdit:
QLabel *name = new QLabel("Name: ", this);
QLineEdit *name_in = new QLineEdit(this);
//name_in->setText(/*the part where I want the name of the file in the arguments*/);
contentBox->addWidget(name);
contentBox->addWidget(name_in);

//HBox buttonBox with the ok and cancel buttons
QPushButton *ok = new QPushButton("OK", this);
QPushButton *cancel = new QPushButton("Cancel", this);
connect(cancel, SIGNAL(clicked()), qApp, SLOT(quit()));
buttonBox->addWidget(ok, 1, Qt::AlignRight);
buttonBox->addWidget(cancel, 0, Qt::AlignRight);

//VBox vbox, which contains the two HBoxes:
vbox->addLayout(contentBox);
vbox->addLayout(buttonBox);
}
http://img97.imageshack.us/img97/1667/screenshotugl.png (http://img97.imageshack.us/i/screenshotugl.png/)
Uploaded with ImageShack.us (http://imageshack.us)

stampede
23rd January 2011, 15:06
For example, add a second parameter in VerticalBox constructor:

VerticalBox( const QString& fileName = QString(), QWidget * parent = NULL )
And use it to setup lineEdit in constructor:

VerticalBox::VerticalBox(const QString& fileName,QWidget *parent) : QWidget(parent)
{
QVBoxLayout *vbox = new QVBoxLayout(this);
QHBoxLayout *contentBox = new QHBoxLayout();
QHBoxLayout *buttonBox = new QHBoxLayout();

//HBox contentBox containing the QLabel and QLineEdit:
QLabel *name = new QLabel("Name: ", this);
QLineEdit *name_in = new QLineEdit(this);
name_in->setText(fileName);
contentBox->addWidget(name);
contentBox->addWidget(name_in);
...

In order to pass the "fileName" argument you can use argv array:


...
QApplication app(argc, argv);
const QString toPass = argc > 1 ? QString(argv[1]) : QString();
VerticalBox window( toPass );

argv[0] is usually name of the program, argv[1], argv[2], ... are parameters passed.

You can also use QCoreApplication::arguments()

mr_kazoodle
23rd January 2011, 15:45
Thank you very much. This was exactly what I needed :D

mr_kazoodle
24th January 2011, 17:26
...
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 :D)

wysota
24th January 2011, 17:57
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.

mr_kazoodle
26th January 2011, 20:04
thanks again, I tried it and got it working as well :)

QString toPass;
const QStringList arguments = QCoreApplication::arguments();
if(arguments.count()==1) toPass = "";
else toPass = arguments.at(1);

VerticalBox window(toPass);