Results 1 to 15 of 15

Thread: A shortcut please ;)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default A shortcut please ;)

    Hi, I've got code like
    Qt Code:
    1. QString *line = new QString[2];
    2. line[0] = setting1;
    3. line[1] = setting2;
    4. FillData(line);
    To copy to clipboard, switch view to plain text mode 
    what is the correct syntax to shorten this, something like bellow, but can't get it right:
    Qt Code:
    1. FillData(new QString[2]() << setting1 << setting2);
    To copy to clipboard, switch view to plain text mode 
    PS: I know I can uset QStringList, but just curious, thanks.

  2. #2
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A shortcut please ;)

    This won't pass. But it will pass if line (and the argument of FillData()) is a QStringList. In the later case:
    Qt Code:
    1. FillData(new QStringList() << setting1 << setting2);
    To copy to clipboard, switch view to plain text mode 
    See QStringList documentation. Also decide, what you will do with the allocated anonymous QStringList passed to FillData(). It needs to be deleted in the end and the FillData() should be a bit secure and foolproof.

  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Quote Originally Posted by Radek View Post
    This won't pass. But it will pass if line (and the argument of FillData()) is a QStringList. In the later case:
    Qt Code:
    1. FillData(new QStringList() << setting1 << setting2);
    To copy to clipboard, switch view to plain text mode 
    See QStringList documentation. Also decide, what you will do with the allocated anonymous QStringList passed to FillData(). It needs to be deleted in the end and the FillData() should be a bit secure and foolproof.
    Thanks Radek. So if the parameter FillData is just not QStringList I can't use this type of syntax? Definition of FillData is like
    Qt Code:
    1. void MainWindow::FillData(QString settingLine[]) {
    2. //some code
    3. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A shortcut please ;)

    You cannot because the operation << isn't defined for pointers to a QString (and an array of QStrings is, in fact, a pointer to a Qstring).

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Quote Originally Posted by arcull View Post
    Definition of FillData is like
    Qt Code:
    1. void MainWindow::FillData(QString settingLine[]) {
    2. //some code
    3. }
    To copy to clipboard, switch view to plain text mode 
    That signature is missing the length argument, i.e. how long the array is.
    One of the advantages of using a container is that this bit of information is always accessible because it is something the container instance knowns.

    Cheers,
    _

  6. #6
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    That signature is missing the length argument, i.e. how long the array is.
    One of the advantages of using a container is that this bit of information is always accessible because it is something the container instance knowns.
    It's true, but it works, since I know the number of elements in array is fixed. However the answer to the original question is still a bit foggy... If I don't use QstringList, can I use syntax similar to suggested firstplace?

  7. #7
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Hello,

    did you consider using some c++11 features? You could rewrite your code using something like
    Qt Code:
    1. void FillData(const QVector<QString>& strings)
    2. {
    3. for(const auto& string : strings)
    4. {
    5. std::cout << string.toStdString() << std::endl;
    6. }
    7. }
    8.  
    9. ...
    10.  
    11. FillData({"string1", "string2", "string3"});
    To copy to clipboard, switch view to plain text mode 

    or better using an initializer list:
    Qt Code:
    1. void FillData1(std::initializer_list<QString> strings)
    2. {
    3. for(const auto& string : strings)
    4. {
    5. std::cout << string.toStdString() << std::endl;
    6. }
    7. }
    8.  
    9. ...
    10.  
    11. QString setting1 = "string1";
    12. QString setting2 = "string2";
    13. QString setting3 = "string3";
    14. FillData1({setting1, setting2, setting3});
    To copy to clipboard, switch view to plain text mode 

    If you want to limit the number of settings to at most 2, you could also use
    Qt Code:
    1. void FillData2(const std::array<QString,2>& strings)
    2. {
    3. for(const auto& string : strings)
    4. {
    5. std::cout << string.toStdString() << std::endl;
    6. }
    7. }
    8.  
    9. ...
    10.  
    11. FillData2({setting1});
    12. FillData2({setting1, setting2});
    13. FillData2({setting1, setting2, setting3}); // Error: too many arguments! (only 2 allowed)
    To copy to clipboard, switch view to plain text mode 
    To use it with Qt5 add
    Qt Code:
    1. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 
    to your .pro file.

    With c++11 initializer lists supported by Qt5 containers I personally prefer using initializer lists instead something like
    Qt Code:
    1. QStringList() << "string1" << "string2" << ...;
    To copy to clipboard, switch view to plain text mode 

    Best regards
    ars
    Last edited by ars; 22nd August 2015 at 15:43.

  8. #8
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    ars, thanks for nice code snippets

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

    Default Re: A shortcut please ;)

    Quote Originally Posted by arcull View Post
    It's true, but it works, since I know the number of elements in array is fixed. However the answer to the original question is still a bit foggy... If I don't use QstringList, can I use syntax similar to suggested firstplace?
    Passing a bare C array of QString instances is a very questionable approach as far as c++ is concerned. There is no practical reason to be doing that.
    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.


  10. #10
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Quote Originally Posted by wysota View Post
    Passing a bare C array of QString instances is a very questionable approach as far as c++ is concerned. There is no practical reason to be doing that.
    Ok so the definition like following woud be ok?
    Qt Code:
    1. void MainWindow::FillData(QString settingLine[2]) {
    2. //some code
    3. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A shortcut please ;)

    Qt Code:
    1. void MainWindow::FillData( QStringList& settingLine )
    2. {
    3. code
    4. }
    To copy to clipboard, switch view to plain text mode 
    or const QStrinList& settingLine if settingLine won't be modified.

  12. #12
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Quote Originally Posted by Radek View Post
    Qt Code:
    1. void MainWindow::FillData( QStringList& settingLine )
    2. {
    3. //code
    4. }
    To copy to clipboard, switch view to plain text mode 
    or const QStrinList& settingLine if settingLine won't be modified.
    Is the apersend sign ("&") a must? I will be calling function passing parameters "by value", but I can add a const qualifier. So it would look like this:
    Qt Code:
    1. void MainWindow::FillData(const QStringList settingLine )
    2. {
    3. code
    4. }
    5.  
    6. list << "A" << "B";
    7. FillData(list);
    To copy to clipboard, switch view to plain text mode 
    How does it look

  13. #13
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: A shortcut please ;)

    No, it is not. It only saves stack and time. If you call "by value", the settinLine will be copied on the stack and deleted from the stack on return. If you call "by reference" (the ampersand), the pointer to an existing object will be placed on the stack. You need to delete the referenced object yourself.

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

    Default Re: A shortcut please ;)

    Quote Originally Posted by arcull View Post
    Is the apersend sign ("&") a must? I will be calling function passing parameters "by value", but I can add a const qualifier. So it would look like this:
    Qt Code:
    1. void MainWindow::FillData(const QStringList settingLine )
    2. {
    3. code
    4. }
    5.  
    6. list << "A" << "B";
    7. FillData(list);
    To copy to clipboard, switch view to plain text mode 
    How does it look
    Better use "const QStringList& settingLine".

    With C++11 you can also use:

    Qt Code:
    1. FillData({"A", "B"});
    To copy to clipboard, switch view to plain text mode 
    which should construct the string list on the fly. This is the shortest code you can write to make it work.
    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.


  15. #15
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A shortcut please ;)

    Quote Originally Posted by Radek View Post
    No, it is not. It only saves stack and time. If you call "by value", the settinLine will be copied on the stack and deleted from the stack on return. If you call "by reference" (the ampersand), the pointer to an existing object will be placed on the stack. You need to delete the referenced object yourself.
    That is what I want, I mean call "by value", thanks for making it clear.


    Added after 6 minutes:


    Quote Originally Posted by Radek View Post
    No, it is not. It only saves stack and time. If you call "by value", the settinLine will be copied on the stack and deleted from the stack on return. If you call "by reference" (the ampersand), the pointer to an existing object will be placed on the stack. You need to delete the referenced object yourself.
    That is what I want, I mean call "by value", thanks for making it clear.


    Added after 8 minutes:


    Quote Originally Posted by wysota View Post
    Better use "const QStringList& settingLine".

    With C++11 you can also use:

    Qt Code:
    1. FillData({"A", "B"});
    To copy to clipboard, switch view to plain text mode 
    which should construct the string list on the fly. This is the shortest code you can write to make it work.
    Ok thanks for suggestion.
    Last edited by arcull; 26th August 2015 at 09:03.

Similar Threads

  1. F1 shortcut
    By lsbts1291 in forum Qt Programming
    Replies: 0
    Last Post: 30th January 2014, 12:52
  2. Shortcut of Qt application.exe
    By Ketan Shah in forum Newbie
    Replies: 6
    Last Post: 3rd June 2011, 08:17
  3. Shortcut
    By Voldemort in forum Qt Programming
    Replies: 16
    Last Post: 2nd May 2007, 20:51
  4. shortcut with more than one key
    By ChasW in forum Qt Programming
    Replies: 1
    Last Post: 26th January 2007, 06:38
  5. Trying to set shortcut
    By mikro in forum Newbie
    Replies: 2
    Last Post: 30th November 2006, 09:55

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
  •  
Qt is a trademark of The Qt Company.