PDA

View Full Version : Variable Path for QPixmap



moatilliatta
8th September 2010, 15:04
Hello everyone,

i'm trying to add a variable to my QPixmap-Path. I thought it just works as a String but apparently it doesn't. Here is what i've written so far.


picData(int i) {
std::ostringstream s;
s << i;
greyscale = new QLabel();
greyscale->setPixmap(QPixmap("images/0"+s.str+"_greyscale.png"));
};

The "s.str" is an integer that stands for different groups of pictures. In the folder are different .png files from e.g. 1 to 9. I don't want to write everything except for the one number over and over again. Is there another way?

Thank you in advance.
Moat

tbscope
8th September 2010, 16:09
What happens if you use

QString(s.str())
instead of

s.str
?

moatilliatta
8th September 2010, 16:40
That didn't solve the problem, but i finally got it running with the following code;


QString str_gs("images/0_greyscale.png");
str_gs.insert(8, QString("%1").arg(i));

Thanks tbscope..

ChrisW67
9th September 2010, 05:18
Did you try:


greyscale->setPixmap( QPixmap( QString("images/0%1_greyscale.png").arg(s.str) ) );

or this, which allows 00 to 99 prefixes and looks after the leading zero:


greyscale->setPixmap( QPixmap( QString("images/%1_greyscale.png").arg(s.str, 2, 10, QLatin1Char('0')) ) );