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. QIODevice: Trouble replicating writeBlock behaviour
Forum Updated to NodeBB v4.3 + New Features

QIODevice: Trouble replicating writeBlock behaviour

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.3k 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    I have some old, Qt3-based code that shows me how to "talk" with a linux device driver. Basically, the device is handled like a file, I write commands and read the replies.

    However, I am trying to re-write it in clean Qt4 code, and have trouble replicating the exact behavior of the QIODevice::writeBlock method.

    The existing code (which is proven to work, although I cannot currently compile it), is simple enough:

    @bool CTouchpadIO::ExchangeMessage( QString csMessage )
    {
    if( m_DevFile.isOpen() )
    {
    m_DevFile.writeBlock( csMessage, csMessage.length() );
    m_aReply = m_DevFile.readAll();

    return true;
    }
    return false;
    }@

    m_DevFile is a QFile.

    In my own code, I have tried to use:
    @ QString command = sm_cCommandProvideEvents;

    m_Device.write(command,
                   command.length());@
    

    However, the compiler complains about this:

    bq. error: no matching function for call to 'QFile::write(QString&, int)'

    I have looked up writeBlock in the Qt sources, and found that it also takes only const char*, not QString.
    I would happily convert the QString to a const char*, if I knew which of the multiple ways to do that leads to the correct results. Basically, there must have been an implicit conversion going on in the writeBlock call, and I would like to know it's exact behavior, so I can mimic it.

    Any suggestions?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      There's an "FAQ":/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa on the conversion topic.

      And there is no "correct" way to do it, it all depends on your data. If your commands consist of 7 bit ASCII characters only, QString::toLatin1() is a good choice.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        It seems toLocal8Bit does what I need.

        Thanks!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          BTW, I had read the FAQ, but in the end, trial and error proved to be the way to go.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

            I would recommend against using toLocal8Bit(...) here: You might end up getting different output results based on the system/user using your application!

            All of toLatin1(...), toAscii(...) and toUtf8(...) have a well defined output.

            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