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:
Code:
#include <QtGui/QApplication>
#include "verticalbox.h"
int main(int argc, char *argv[])
VerticalBox window;
window.show();
return app.exec();}
Code:
#ifndef VERTICALBOX_H
#define VERTICALBOX_H
#include <QWidget>
{ public:
VerticalBox
(QWidget *parent
= 0);
};
#endif
Code:
#include <QApplication>
#include "verticalbox.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
{
//HBox contentBox containing the QLabel and QLineEdit:
//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
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
Uploaded with ImageShack.us
Re: how to use arguments in Qt?
For example, add a second parameter in VerticalBox constructor:
Code:
VerticalBox
( const QString
& fileName
= QString(),
QWidget * parent
= NULL )
And use it to setup lineEdit in constructor:
Code:
VerticalBox
::VerticalBox(const QString
& fileName,
QWidget *parent
) : QWidget(parent
){
//HBox contentBox containing the QLabel and QLineEdit:
name_in->setText(fileName);
contentBox->addWidget(name);
contentBox->addWidget(name_in);
...
In order to pass the "fileName" argument you can use argv array:
Code:
...
VerticalBox window( toPass );
argv[0] is usually name of the program, argv[1], argv[2], ... are parameters passed.
You can also use QCoreApplication::arguments()
Re: how to use arguments in Qt?
Thank you very much. This was exactly what I needed :D
Re: how to use arguments in Qt?
Quote:
...
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)
Re: how to use arguments in Qt?
Quote:
Originally Posted by
mr_kazoodle
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.
Re: how to use arguments in Qt?
thanks again, I tried it and got it working as well :)
Code:
if(arguments.count()==1) toPass = "";
else toPass = arguments.at(1);
VerticalBox window(toPass);