c++qtconsole-applicationqtextstream

Access violation error when using QTextStream to read from console


I have problem, got access violation when trying to use QTextStream for data reading or writing to console:

First-chance exception at 0x77BD1D76 (ntdll.dll) in ApplicationStub.exe: 0xC0000005:
Access violation writing location 0x00000014.

Unhandled exception at 0x77BD1D76 (ntdll.dll) in ApplicationStub.exe: 0xC0000005:
Access violation writing location 0x00000014.

My program is simple:

#include <QtWidgets/QApplication>
#include <iostream>
#include <QTextStream>
#include <stdio.h>

using namespace std;

int main(int argc, char *argv[])
{

        QApplication app(argc, argv);

        ///////////////////////////////////////////////CONSOLE

        QTextStream out(stdout);
        out << "Please enter login username and password\n";
        out.flush();


        QTextStream in(stdin);
        QString line;
        in >> line;

        return app.exec();

}

What could be the problem? Thanks

Edit 1 I also tried QCoreApplication I am using Visual studio 2013, Windows 7

Also in my .pro file I have:

QT += console
QT += core gui

I have there gui for gui option, i think this should be ok.


Solution

  • There is no problem at all in the code, though it could be cleaned up quite a bit. Most likely you're not building it as a console application, thus it starts without a console, and any attempts to access the non-existent console fail.

    Comments:

    1. To include a Qt class Class, use #include <QClass>, not #include <QtModule/QClass>.
    2. You can include an entire Qt module to reduce the number of explicit includes, e.g. #include <QtCore> would be sufficient for a console application.
    3. You don't need a QCoreApplication instance to use QTextStream (note that QApplication is-a QCoreApplication!).
    4. stdout and stdin come from the <cstdio> header. You don't need <iostream>.
    5. In the project file as well as in your code, you don't need to add module dependencies, only the top-level modules. E.g. if you're using any module other than core, you don't need to explicitly add the core module since all other modules depend on it. If you add the widgets module in Qt 5, you don't need to add the gui module. And so on.

    There are two ways to assign a console to your process:

    1. Add CONFIG += console to the qmake project file. That way your process will always have a console window upon startup:

      # test1.pro
      QT = core
      CONFIG += console c++11
      CONFIG -= app_bundle
      TARGET = test1
      TEMPLATE = app
      SOURCES += main.cpp
      

      Your code will now work fine: on startup, you'll see a console window open.

    2. Explicitly allocate a console in a GUI application. The console window will appear only when you need it, and not by default:

      # test1.pro
      QT = widgets      # or core if you don't care for a graphical interface
      CONFIG += c++11
      TARGET = test1
      TEMPLATE = app
      SOURCES += main.cpp
      

      main.cpp:

      #include <QtCore>
      #include <cstdio>
      #include <windows.h>
      
      void addConsole() {
        AllocConsole();
        freopen("CON", "wt", stdout);
        freopen("CON", "wt", stderr);
        freopen("CON", "rt", stdin);
      }
      
      int main() {
        addConsole();
        QTextStream out{stdout};
        QTextStream in{stdin};
      
        out << "Enter your name: " << flush;
      
        QString name;
        in >> name;
        out << "Your name is: " << name << "." << endl;
        QThread::sleep(1);
      }
      

    Important Note

    After making any changes to the project file, you need to re-run qmake and rebuild the project.

    context menu of the project

    To simplify this, simply erase the build folder.