Results 1 to 5 of 5

Thread: Default value for a QStringlist

  1. #1
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Default value for a QStringlist

    Is there any way for a QStringList (or any list) to have a default value in a function or class declaration?

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Default value for a QStringlist

    Yes, why not. Just create another QStringList (or any list) and initialise the your QStringList with it.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2011
    Location
    Living in Spain 100km North of Valencia
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Default value for a QStringlist

    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:

    Qt Code:
    1. #ifndef MYINPUTDLG_H
    2. #define MYINPUTDLG_H
    3.  
    4. #include "myinputdlg_global.h"
    5. #include <utilities.h>
    6. #include <Dialog.h>
    7.  
    8. class MYINPUTDLGSHARED_EXPORT MyinputDlg
    9. {
    10.  
    11. public:
    12. MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = "", QString def = "");
    13.  
    14. errorNumbers error;
    15. QVariant retVal;
    16. private:
    17. QString m_title, m_lblText, m_def;
    18. };
    19.  
    20. #endif // MYINPUTDLG_H
    To copy to clipboard, switch view to plain text mode 

    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

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Default value for a QStringlist

    Qt Code:
    1. MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "");
    To copy to clipboard, switch view to plain text mode 
    and You have an empty list as the default value.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Default value for a QStringlist

    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:

    Qt Code:
    1. MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "");
    2.  
    3. // and
    4.  
    5. MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts, QString def);
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. MyinputDlg(QString title = "My Input Dialogue", QStringList lblTexts = QStringList(), QString def = "Something non-default");
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    GrahamB (13th May 2017)

Similar Threads

  1. Help QStringList
    By pcoliver in forum Qt Programming
    Replies: 0
    Last Post: 15th September 2011, 10:15
  2. Plz help me,I met a bug about QStringList
    By study_c in forum Qt Programming
    Replies: 1
    Last Post: 16th October 2010, 15:56
  3. QStringList
    By jaca in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2008, 10:12
  4. QStringList
    By dragon in forum Newbie
    Replies: 2
    Last Post: 8th June 2007, 17:26
  5. Replies: 7
    Last Post: 2nd June 2006, 12:48

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.