Results 1 to 7 of 7

Thread: Get a QStringList out of a function.

  1. #1
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Get a QStringList out of a function.

    How do you get a QStringList created in a function out so it can be used by other functions.

    I have a function that gets the filenames from a pre-selected directory. I need to use this info in a number of functions. Here is my function:

    Qt Code:
    1. void ImagePreview::getFiles(const QString &dir)
    2. {
    3. QString filename = "*.TIF";
    4. QDir directory = QDir(dir);
    5. files = directory.entryList(QStringList(filename),
    6. QDir::Files | QDir::NoSymLinks);
    7.  
    8. loadImage();
    9. }
    To copy to clipboard, switch view to plain text mode 

    How do I get this out of this function.

    Thanks,

  2. #2
    Join Date
    Jan 2006
    Location
    Paris, France
    Posts
    227
    Thanks
    3
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get a QStringList out of a function.


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

    Default Re: Get a QStringList out of a function.

    In short:
    Qt Code:
    1. QStringList XX::getFiles(const QString &dir){
    2. //...
    3. return directory.entryList(...);
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get a QStringList out of a function.

    Thanks guys...but I'm not sure if this would work for me.

    I guess I should explain what I am trying to do.

    I have a program that lets the user choose a directory that contains a series of Tiff files and view each one, one at a time.

    I declared an int called "filenum" and set = 0.

    The user will choose a browse button that SIGNALS the SLOT(browse())
    Once the user chooses the directory, a lineedit is updated with the path, and the getfiles funtion is called using the path from browse.

    In the get function, a list of Tiff files is created. This has to occur only once, but I would like to immediately preview the first Tiff image. Hence, immediately call the loadimage function. (This is of course where my QStringList is created)

    The loadimage function (using the directory path and files from QStringList and the filenum would load the firstimage.

    A "next" button would increment filenum++ and call loadimage again.

    Here is my code:
    Qt Code:
    1. void ImagePreview::browse()
    2.  
    3. {
    4.  
    5. QString directory = QFileDialog::getExistingDirectory(this,
    6.  
    7. tr("Set Directory"),QDir::currentPath());
    8.  
    9. lineEdit->setText(directory);
    10.  
    11. getFiles(directory);
    12.  
    13. }
    14.  
    15.  
    16.  
    17. void ImagePreview::getFiles(const QString &dir)
    18.  
    19. {
    20.  
    21. QString filename = "*.TIF";
    22.  
    23. QDir directory = QDir(dir);
    24.  
    25. QStringList files;
    26.  
    27. files = directory.entryList(QStringList(filename),
    28.  
    29. QDir::Files | QDir::NoSymLinks);
    30.  
    31.  
    32.  
    33. loadImage();
    34.  
    35. }
    36.  
    37.  
    38.  
    39. void ImagePreview::loadImage()
    40.  
    41. {
    42.  
    43. QString filename = lineEdit->text();
    44.  
    45. int f = filenum;
    46.  
    47.  
    48.  
    49. filename += files->at(f);
    50.  
    51.  
    52.  
    53. QImage image(filename);
    54.  
    55. if (!filename.isNull()){
    56.  
    57. QMessageBox::information(this, tr("Photoshed Image Load"),
    58.  
    59. tr("Cannot load %1.").arg(filename));
    60.  
    61. return;
    62.  
    63. }
    64.  
    65. QPixmap picture = QPixmap::fromImage(image);
    66.  
    67. imageLabel->setPixmap(picture.scaledToWidth(200));
    68.  
    69. fileLabel->setText(files->at(f));
    70.  
    71. a = picture.hasAlpha();
    72.  
    73. w = picture.width();
    74.  
    75. h = picture.height();
    76.  
    77. QString wint(QString::number(w, 10));
    78.  
    79. QString hint(QString::number(h, 10));
    80.  
    81. QString wlabel("<b>Width: </b>");
    82.  
    83. wlabel += wint;
    84.  
    85. widthLabel->setText(wlabel);
    86.  
    87.  
    88.  
    89. QString hlabel("<b>Height: </b>");
    90.  
    91. hlabel += hint;
    92.  
    93. heightLabel->setText(hlabel);
    94.  
    95.  
    96.  
    97. if (a){
    98.  
    99. alphaLabel->setText("<b>Alpha Channel: <font color=green>ON</font></b>");
    100.  
    101. } else {
    102.  
    103. alphaLabel->setText("<b>Alpha Channel: <font color=red>OFF</font></b>");
    104.  
    105. }
    106.  
    107. }
    108.  
    109.  
    110.  
    111. void ImagePreview::next()
    112.  
    113. {
    114.  
    115.  
    116. if (filenum > files->size()){
    117.  
    118.  
    119. return;
    120.  
    121. }
    122.  
    123. filenum++;
    124.  
    125. loadImage();
    126.  
    127.  
    128. }
    To copy to clipboard, switch view to plain text mode 

    I of course get an error that "files" hasn't been initialized in loadImage().

    Wysota suggested making getfiles a function with a QStringList return, but where am I returning that and how am I calling that from browse().

    Sorry for being such a newbie!

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Get a QStringList out of a function.

    You could make it a member variable:
    Qt Code:
    1. class ImagePreview
    2. {
    3. ...
    4. private:
    5. QStringList files; // a member variable
    6. };
    7.  
    8. void ImagePreview::getFiles(const QString &dir)
    9. {
    10. ...
    11. // QStringList files; // a local variable (commented out on purpose, to make sure not to assign to a local variable but to the member variable)
    12. files = directory.entryList(QStringList(filename),
    13. QDir::Files | QDir::NoSymLinks);
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Jul 2007
    Location
    Scottsdale, AZ USA
    Posts
    17
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Get a QStringList out of a function.

    jpn You could make it a member variable:
    jpn, I was doing this all day and kept getting segmentation errors until I noticed I called it out using:

    Qt Code:
    1. QStringList *files;
    To copy to clipboard, switch view to plain text mode 

    I was using a pointer and this was giving me fits. Thank you for helping me on this. I hate spending a day looking for a misplaced *.

    Could you answer a question for me? Why don't I use an * in this situation? Is this because a QStringList is implicitly shared?

    Thanks.

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Get a QStringList out of a function.

    Quote Originally Posted by devilj View Post
    Why don't I use an * in this situation? Is this because a QStringList is implicitly shared?
    There is no need to complicate things. You should take care of freeing the memory if you allocate it on the heap whereas like this it gets automatically destructed together with the ImagePreview object.

    Implicit sharing doesn't actually help in this case because the list doesn't get copied anyway. You just access exactly the same list in different member functions. Implicit sharing came forward if you for example returned the list as a return value of a function. This would cause the list object to be copied but the actual data still wouldn't get copied until needed, thanks to implicit sharing.
    J-P Nurmi

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  2. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. Qt 4.1.4 plugin QPSQL
    By jcr in forum Installation and Deployment
    Replies: 4
    Last Post: 22nd June 2006, 22:55
  5. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52

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.