Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Function that use setfill and setw to return a QString
Forum Updated to NodeBB v4.3 + New Features

Function that use setfill and setw to return a QString

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 501 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ApprenticeRenoA Offline
    ApprenticeRenoA Offline
    ApprenticeReno
    wrote on last edited by
    #1

    Hi guys, i'm trying to write a function that gets two int and sets a particular String with a certain number of leading 0s.
    Something like this:

    int a=5;
    int b=21;
    
    void MainWindow::Function(int a,int b){
        
       // String s= std::setfill('0') << std::setw(a) << b ;
       // qDebug()<<"value of s -> "<<s;
       // Expected result -> 00021 ; 
    
     //if a=3 i would expect -> 021;
    }
    

    So i can do something like :

    int z=35;
    Function(4,z); // set String s='0035'
    qDebug()<<"s value -> "<<s  // "s value -> 0035"
    

    Any kind of help would be appreciated
    thanks

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2
      QStringLiteral("%1").arg(b, a, 10, QChar('0'))
      

      or

      QString::number(b).rightJustified(a, '0')
      
      1 Reply Last reply
      1
      • ApprenticeRenoA Offline
        ApprenticeRenoA Offline
        ApprenticeReno
        wrote on last edited by
        #3

        @Chris-Kawa Thank you very much.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved