Results 1 to 6 of 6

Thread: How to convert from QString to quint16 ?

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to convert from QString to quint16 ?

    How do I convert from QString to quint16 ?

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to convert from QString to quint16 ?

    QString::number doesnt help?
    a life without programming is like an empty bottle

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

    Default Re: How to convert from QString to quint16 ?

    Qt Code:
    1. QString str("1234");
    2. QTextStream ts(&str);
    3. quint16 num = 0;
    4. ts >> num;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to convert from QString to quint16 ?

    I have a QLineEdit:
    Qt Code:
    1. QLineEdit *lePort = new QLineEdit();
    2. lePort->setText("3000");
    To copy to clipboard, switch view to plain text mode 

    In my tcpSocket I need to connect:
    Qt Code:
    1. QTcpSocket *tcpSocket = new QTcpSocket();
    2. tcpSocket->connectToHost(QHostAddress("127.0.0.1"), quint16(2000));
    To copy to clipboard, switch view to plain text mode 

    In stead of having the quint16(2000) I would like to have the value of lePort. I tried
    Qt Code:
    1. tcpSocket->connectToHost(QHostAddress("127.0.0.1"), QString::number(lePort->text()));
    To copy to clipboard, switch view to plain text mode 
    but it doesn't work.

  5. #5
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: How to convert from QString to quint16 ?

    Actually i see that you want string - > int not int->string as i understad before.

    Try next
    Qt Code:
    1. tcpSocket->connectToHost(QHostAddress("127.0.0.1"),lePort->text().toShort());
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

  6. #6
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to convert from QString to quint16 ?

    QString::number() makes a QString from a number.. read the docs first.
    Use QString::toInt(), QString::toUInt(), etc.

Similar Threads

  1. convert from QString to char[sizeof(...)]
    By adamatic in forum Qt Programming
    Replies: 4
    Last Post: 3rd September 2011, 09:05
  2. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  3. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  4. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.