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. How does QByteArray gets assigned to QStriing

How does QByteArray gets assigned to QStriing

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 556 Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    In the following code

    QFile data(ofilename.toLatin1().constData());
    QString line;
    if (data.open(QFile::ReadOnly)) {
    setText(data.readAll());
    setModified(FALSE);
    data.close();
    d_filename = ofilename;
    emit fileNameChanged(ofilename);
    }

    data.readAll has return type QByteArray but setText takes QString as argument then how does setText(data.readAll()); conversion happens from QByteArray to QString

    kshegunovK 1 Reply Last reply
    0
    • Q Qt Enthusiast

      In the following code

      QFile data(ofilename.toLatin1().constData());
      QString line;
      if (data.open(QFile::ReadOnly)) {
      setText(data.readAll());
      setModified(FALSE);
      data.close();
      d_filename = ofilename;
      emit fileNameChanged(ofilename);
      }

      data.readAll has return type QByteArray but setText takes QString as argument then how does setText(data.readAll()); conversion happens from QByteArray to QString

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by kshegunov
      #2

      @Qt-Enthusiast
      The QByteArray object doesn't get assigned to QString. Rather a temporary QString instance is created from the byte array due to the implicit QString constructor that accepts a byte array and thanks of course to the overloading rules in C++.

      PS:

      QFile data(ofilename.toLatin1().constData());
      

      should just be:

      QFile data(ofilename);
      

      As per the respective QFile constructor.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved