PDA

View Full Version : Default value for a QStringlist



GrahamB
12th May 2017, 11:48
Is there any way for a QStringList (or any list) to have a default value in a function or class declaration?

Santosh Reddy
12th May 2017, 12:32
Yes, why not. Just create another QStringList (or any list) and initialise the your QStringList with it.

GrahamB
12th May 2017, 16:15
Thank you Santosh

I have not explained myself well. I want a DEFAULT value for a variable in a an arguments list. An example follows:



#ifndef MYINPUTDLG_H
#define MYINPUTDLG_H

#include "myinputdlg_global.h"
#include <utilities.h>
#include <Dialog.h>

class MYINPUTDLGSHARED_EXPORT MyinputDlg
{

public:
MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = "", QString def = "");

errorNumbers error;
QVariant retVal;
private:
QString m_title, m_lblText, m_def;
};

#endif // MYINPUTDLG_H


Obviously this does not compile because the lblTexts is a list NOT a QString. lblTexts = {} does nor work either.
Is there a way of declaring a default for a QStringList in a case like this?
Thank you again for you help.
Regards
G

Lesiok
12th May 2017, 17:50
MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "");
and You have an empty list as the default value.

d_stranz
12th May 2017, 19:03
Is there a way of declaring a default for a QStringList in a case like this?

QStringList and QString variables are always defaulted to an empty QStringList (same as assigning it to QStringList(), as Lesiok explains), and QString defaults to an empty string. So these two are exactly the same in their effect:



MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "");

// and

MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts, QString def);


The only time you need the first form is if the variable(s) with the default value(s) are not at the end of the list of variables:



MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "Something non-default");