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. Add to QByteArray from TextBox

Add to QByteArray from TextBox

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 643 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.
  • J Offline
    J Offline
    joe1652
    wrote on last edited by
    #1

    Hello, I have a lineEdit text box on my GUI Form which I plan to enter in a number such as float. I want to add the contents of the lineEdit box to a QByteArray.

    Something like:
    QByteArray data = QByteArray::number(ui->lineEdit_0->text());

    But when I try I get:

    mainwindow.cpp:39: error: no matching function for call to 'QByteArray::number(QString)'
    QByteArray data = QByteArray::number(ui->lineEdit_0->text());
    ^

    I intend to use the QByteArray to transmit UDP data:
    udp->writeDatagram(data.data(),size,QHostAddress(this->ip),srcPort);

    Can anyone help out a newbie please.

    Thank you
    Joe

    JKSHJ 1 Reply Last reply
    0
    • J joe1652

      Hello, I have a lineEdit text box on my GUI Form which I plan to enter in a number such as float. I want to add the contents of the lineEdit box to a QByteArray.

      Something like:
      QByteArray data = QByteArray::number(ui->lineEdit_0->text());

      But when I try I get:

      mainwindow.cpp:39: error: no matching function for call to 'QByteArray::number(QString)'
      QByteArray data = QByteArray::number(ui->lineEdit_0->text());
      ^

      I intend to use the QByteArray to transmit UDP data:
      udp->writeDatagram(data.data(),size,QHostAddress(this->ip),srcPort);

      Can anyone help out a newbie please.

      Thank you
      Joe

      JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi, and welcome!

      @joe1652 said in Add to QByteArray from TextBox:

      mainwindow.cpp:39: error: no matching function for call to 'QByteArray::number(QString)'
           QByteArray data = QByteArray::number(ui->lineEdit_0->text());
                                                                      ^
      
      • QByteArray::number() converts a number (int, float, double, etc.) to an 8-bit string. This is not what you want.
      • QLineEdit::text() returns a QString. When you call this, your number is already stored a (16-bit) string.
      • QString::toUtf8() converts a QString to a QByteArray (using UTF-8 encoding)

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • J Offline
        J Offline
        joe306
        wrote on last edited by
        #3

        Hello, thank you for responding to my message. I'm a little slow here.

        QByteArray data = QByteArray:: what goes here (ui->lineEdit_0->text() );

        Sorry to ask you again for help.

        JonBJ 1 Reply Last reply
        0
        • J joe306

          Hello, thank you for responding to my message. I'm a little slow here.

          QByteArray data = QByteArray:: what goes here (ui->lineEdit_0->text() );

          Sorry to ask you again for help.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @joe306

          QByteArray data = ui->lineEdit_0->text().toUtf8();
          
          1 Reply Last reply
          2
          • JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            @joe306 It might make it clearer to break the code up into smaller segments. @JonB's code is equivalent to:

            QString str = ui->lineEdit_0->text();
            QByteArray data = str.toUtf8();
            

            Does this make sense?

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            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