Results 1 to 2 of 2

Thread: UI text to char

  1. #1
    Join Date
    Feb 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default UI text to char

    Hello newbie here.

    I am having trouble converting a simple text within the UI to Char. My goal is to submit text in the UI which will be sent to a usb to rs 485.

    I have no trouble sending text directly from within the code, but I would like to send my component different commands from within the UI on the fly.

    Here is the snippet of code giving me problems.
    Qt Code:
    1. QString Motor_Command_;
    2.  
    3.  
    4. Motor_Command_ = ui->MotorCommandText->toPlainText().toLocal8Bit().constData();
    5.  
    6.  
    7.  
    8. char TxBuffer[] = {Motor_Command_};
    To copy to clipboard, switch view to plain text mode 

    The error I get:

    error: cannot convert 'QString' to 'char' in initialization
    char TxBuffer[] = {Motor_Command_};
    ^

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UI text to char

    You are doing a double conversion there, potentially lossy.

    First you encode the QString returned by toPlainText() with the 8-bit codec used for local encoding (toLocal8Bit()), then you convert back to QString using UTF-8 (when assigning to the QString variable).

    If your low-level API required a char*, then first get the encoded form as a QByteArray and then send its constData().

    E.g.
    Qt Code:
    1. const QByteArray data = ui->MotorCommandText->toPlainText().toLocal8Bit();
    2. low_level_write(data.constData(), data.size());
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. memset char * char[]
    By youfulan2015 in forum General Programming
    Replies: 2
    Last Post: 4th August 2015, 06:37
  2. Char Followed by a Char is illegal (qglobal.h)
    By saad_saadi in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2014, 14:19
  3. Replies: 1
    Last Post: 16th August 2012, 00:46
  4. How to convert unsigned char[] to char *?
    By Gokulnathvc in forum Newbie
    Replies: 2
    Last Post: 29th April 2011, 09:58
  5. char to const char* with atof
    By mickey in forum General Programming
    Replies: 5
    Last Post: 29th February 2008, 05:10

Tags for this Thread

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.