PDA

View Full Version : Get a QStringList out of a function.


devilj
6th July 2007, 10:42
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:

void ImagePreview::getFiles(const QString &dir)
{
QString filename = "*.TIF";
QDir directory = QDir(dir);
QStringList files;
files = directory.entryList(QStringList(filename),
QDir::Files | QDir::NoSymLinks);

loadImage();
}

How do I get this out of this function.

Thanks,

guilugi
6th July 2007, 11:24
Have a look a this ;-)

http://doc.trolltech.com/4.3/shared.html

wysota
6th July 2007, 13:19
In short:
QStringList XX::getFiles(const QString &dir){
//...
return directory.entryList(...);
}

devilj
7th July 2007, 02:14
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:
void ImagePreview::browse()

{

QString directory = QFileDialog::getExistingDirectory(this,

tr("Set Directory"),QDir::currentPath());

lineEdit->setText(directory);

getFiles(directory);

}



void ImagePreview::getFiles(const QString &dir)

{

QString filename = "*.TIF";

QDir directory = QDir(dir);

QStringList files;

files = directory.entryList(QStringList(filename),

QDir::Files | QDir::NoSymLinks);



loadImage();

}



void ImagePreview::loadImage()

{

QString filename = lineEdit->text();

int f = filenum;



filename += files->at(f);



QImage image(filename);

if (!filename.isNull()){

QMessageBox::information(this, tr("Photoshed Image Load"),

tr("Cannot load %1.").arg(filename));

return;

}

QPixmap picture = QPixmap::fromImage(image);

imageLabel->setPixmap(picture.scaledToWidth(200));

fileLabel->setText(files->at(f));

a = picture.hasAlpha();

w = picture.width();

h = picture.height();

QString wint(QString::number(w, 10));

QString hint(QString::number(h, 10));

QString wlabel("<b>Width: </b>");

wlabel += wint;

widthLabel->setText(wlabel);



QString hlabel("<b>Height: </b>");

hlabel += hint;

heightLabel->setText(hlabel);



if (a){

alphaLabel->setText("<b>Alpha Channel: <font color=green>ON</font></b>");

} else {

alphaLabel->setText("<b>Alpha Channel: <font color=red>OFF</font></b>");

}

}



void ImagePreview::next()

{


if (filenum > files->size()){


return;

}

filenum++;

loadImage();


}



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!

jpn
7th July 2007, 08:54
You could make it a member variable:

class ImagePreview
{
...
private:
QStringList files; // a member variable
};

void ImagePreview::getFiles(const QString &dir)
{
...
// QStringList files; // a local variable (commented out on purpose, to make sure not to assign to a local variable but to the member variable)
files = directory.entryList(QStringList(filename),
QDir::Files | QDir::NoSymLinks);
...
}

devilj
9th July 2007, 08:02
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:

QStringList *files;

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 *:mad:.

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.

jpn
9th July 2007, 08:16
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.