62035

QString to short using toShort

Question:

I want to convert QString to short.when I try this code

ui->lineEdit->text().toShort();

It works well for text = 20 but it returns "0" for value = 20.5.

but I need value = 20. how can I solve it?

Answer1:

The reason that 0 is returned is because a decimal point is an invalid character for the short data type.

If you want to be able to convert floating-point numbers from QString to integers, you need to convert your text to a float or double first, then use normal rounding/truncation to convert to short.

Answer2:

use that convertion string:

ui->lineEdit->text()->split(".")[0].toShort(0,10);

Recommend

  • Convert .5 into 1/2
  • Drawing rectangles on chart C#
  • Passing data to controller while Using URL.Action in jQuery template in Asp.Net MVC dynamic views
  • How to round using ROUND HALF UP. Rounding mode that most of us were taught in grade school
  • Detox: iOS Simulator how to confirm alert message
  • How can I output data with special characters visible?
  • Finding a value of range less than or greater than from container class
  • Drop variable in panel data in R conditional based on a defined number of consecutive observations
  • Android wearable emulator can't connect with usb connected phone
  • oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT
  • Creating My Symmetric Key in C#
  • What is the undocumented SessionIdInterface in PHP 5.5?
  • Create function that can pass a parameter without making a new component
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • How can we prepend rows to a react native list-view?
  • Trying to get the char code of ENTER key
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • Memory error in python- how to use more memory
  • Using Sax parsing to edit and write XML in VB6
  • How do I signal completion of my dataflow?
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Debug.DrawLine not showing in the GameView
  • Caching attributes in superclass
  • Parsing a CSV string while ignoring commas inside the individual columns
  • Yii2: Config params vs. const/define
  • Is it possible to access block's scope in method?
  • Avoid links criss cross / overlap in d3.js using force layout
  • SignalR .NET Client Invoke throws an exception
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Fill an image in a square container while keeping aspect ratio
  • ILMerge & Keep Assembly Name
  • Deserializing XML into class C#
  • Large data - storage and query
  • ORA-29908: missing primary invocation for ancillary operator
  • Rearranging Cells in UITableView Bug & Saving Changes
  • WOWZA + RTMP + HTML5 Playback?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • WPF Applying a trigger on binding failure
  • Reading document lines to the user (python)