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. [SOLVED] QTextStream and std::endl
QtWS25 Last Chance

[SOLVED] QTextStream and std::endl

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 24.7k Views
  • 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
    Alterah
    wrote on last edited by
    #1

    Back again with another problem. Seems like this should be easy, but apparently I am doing something wrong. I am trying to write to a file. Here's the code:

    @
    void UsernamePopup::handleOkClicked()
    {
    QString username;
    QString password;

    QFile configFile("config.txt");
    
    username = "Username | " + mUsernameEntry->text();
    
    if(mPasswordEntry->text() == mPasswordCheckEntry->text())
    {
        password = "Password | " + mPasswordEntry->text();
    
        if (configFile.open(QFile::WriteOnly))
        {
            QTextStream out(&configFile);
    
            out << username << std::endl << password << std::endl;
        }
        else
        {
            //Notify user
        }
    }
    else
    {
       //Code to popup window notifying user
    }
    

    }
    @

    The error message I am getting is:
    @
    D:/Coding/QtProjects/SimChat-build-desktop/../SimChat/usernamepopup.cpp:126: error: no match for 'operator<<' in 'out.QTextStream::operator<<(((const QString&)((const QString*)(& username)))) << std::endl'@

    I have looked at some examples online and even in the documentation. As far as I can tell I am doing what the examples show. Thanks for any assistance.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      There's no operator<< overload for QTextStream that takes a @ostream &(*fun)(ostream &)@ (yes, that's a pointer to a function that takes a reference to ostream and returns a reference to a ostream). Which, by the way, is the type of std::endl.

      But there's a nice operator<< overload for QTextStream that takes a @ QTextStream &(*fun)(QTextStream &) @ which is the type of QTextStream::endl.

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

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

        Ok, I think I understand what you are saying. I changed the lined to:

        @out << username << std::endl << password << std::endl;@

        This compiles, but when I open the .txt file, It is all on one line in notepad. It does show a little "square" (looks like [])for where the newline should begin. However, Gedit for Windows shows them on separate lines. Part of me is wondering if I should just use ofstream instead of QFile, etc. Thanks.

        Edit: Nevermind. This appears to be a quirk with Windows and "\r\n".

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          Are you sure you pasted the right line of code? It's the same as before...

          About the "\r\n" terminator: simply open your QFile with the QIODevice::Text option.

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Alterah
            wrote on last edited by
            #5

            Whoops, did not paste the correct line. Ought to be:

            @out << "Username | " << username << endl << "Password | " << password << endl;@

            And thanks for the advice. Worked like a charm.

            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