Results 1 to 4 of 4

Thread: Variable Path for QPixmap

  1. #1
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Variable Path for QPixmap

    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.

    Qt Code:
    1. picData(int i) {
    2. std::ostringstream s;
    3. s << i;
    4. greyscale = new QLabel();
    5. greyscale->setPixmap(QPixmap("images/0"+s.str+"_greyscale.png"));
    6. };
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Variable Path for QPixmap

    What happens if you use
    Qt Code:
    1. QString(s.str())
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. s.str
    To copy to clipboard, switch view to plain text mode 
    ?

  3. #3
    Join Date
    Sep 2010
    Location
    Germany
    Posts
    7
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Variable Path for QPixmap

    That didn't solve the problem, but i finally got it running with the following code;

    Qt Code:
    1. QString str_gs("images/0_greyscale.png");
    2. str_gs.insert(8, QString("%1").arg(i));
    To copy to clipboard, switch view to plain text mode 

    Thanks tbscope..

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Variable Path for QPixmap

    Did you try:
    Qt Code:
    1. greyscale->setPixmap( QPixmap( QString("images/0%1_greyscale.png").arg(s.str) ) );
    To copy to clipboard, switch view to plain text mode 
    or this, which allows 00 to 99 prefixes and looks after the leading zero:
    Qt Code:
    1. greyscale->setPixmap( QPixmap( QString("images/%1_greyscale.png").arg(s.str, 2, 10, QLatin1Char('0')) ) );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 8
    Last Post: 17th October 2009, 08:10
  2. Replies: 4
    Last Post: 28th August 2008, 13:13
  3. Replies: 1
    Last Post: 21st August 2008, 07:44
  4. Replies: 5
    Last Post: 9th April 2007, 14:26
  5. Replies: 2
    Last Post: 3rd April 2007, 19:47

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.