【问题标题】:Access violation error when using QTextStream to read from console使用 QTextStream 从控制台读取时出现访问冲突错误
【发布时间】:2017-02-26 11:31:50
【问题描述】:

我遇到问题,尝试使用 QTextStream 读取或写入控制台数据时遇到访问冲突

ApplicationStub.exe 中 0x77BD1D76 (ntdll.dll) 的第一次机会异常:0xC0000005:
访问冲突写入位置 0x00000014。

ApplicationStub.exe 中 0x77BD1D76 (ntdll.dll) 处的未处理异常: 0xC0000005:
访问冲突写入位置0x00000014。

我的程序很简单:

#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();

}

可能是什么问题? 谢谢

编辑 1 我也试过 QCoreApplication 我正在使用 Visual Studio 2013、Windows 7

我的 .pro 文件中还有:

QT += console
QT += core gui

我有 gui 的 gui 选项,我认为这应该没问题。

【问题讨论】:

  • 它适用于我(在 linux 下),但我将 QApplication 更改为 QCoreApplication。您是否将项目定义为控制台应用程序?您应该添加 .pro 文件。

标签: c++ qt console-application qtextstream


【解决方案1】:

代码完全没有问题,尽管可以清理很多。很可能您没有将它构建为控制台应用程序,因此它在没有控制台的情况下启动,并且任何访问不存在的控制台的尝试都会失败。

评论:

  1. 要包含 Qt 类 Class,请使用 #include &lt;QClass&gt;,而不是 #include &lt;QtModule/QClass&gt;
  2. 您可以包含整个 Qt 模块以减少显式包含的数量,例如#include &lt;QtCore&gt; 对于控制台应用程序来说已经足够了。
  3. 您不需要QCoreApplication 实例来使用QTextStream(注意QApplication 是-a QCoreApplication!)。
  4. stdoutstdin 来自 &lt;cstdio&gt; 标头。你不需要&lt;iostream&gt;
  5. 在项目文件以及您的代码中,您不需要添加模块依赖项,只需添加顶级模块即可。例如。如果您使用core 以外的任何模块,则无需显式添加core 模块,因为所有其他模块都依赖于它。如果在 Qt 5 中添加 widgets 模块,则不需要添加 gui 模块。等等。

有两种方法可以为您的进程分配控制台:

  1. CONFIG += console 添加到 qmake 项目文件中。这样您的进程在启动时将始终有一个控制台窗口:

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

    您的代码现在可以正常工作了:在启动时,您会看到一个控制台窗口打开。

  2. 在 GUI 应用程序中显式分配控制台。控制台窗口仅在您需要时才会出现,默认情况下不会出现:

    # 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
    

    ma​​in.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);
    }
    

重要提示

对项目文件进行任何更改后,您需要重新运行 qmake 重建项目。

为简化此操作,只需删除构建文件夹即可。

【讨论】:

  • 谢谢,仍然没有工作,我说,我使用 Visual Studio 2013,没有 Qt 创建者。
  • 使用 Visual Studio 和使用 Qt Creator 是正交问题:例如,我一直使用 Visual Studio 和 Qt Creator。你从来没有说过你不使用 Qt Creator。您还可以使用 qmake 使用 .pro 文件生成 Visual Studio 项目:) 如果您想要控制台应用程序,请将您的 VS 项目设置为控制台应用程序。如果您想控制控制台何时出现,请将其保留为 Windows 应用程序,并使用 addConsole 函数。就是这样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 2018-08-31
  • 2019-07-16
  • 1970-01-01
  • 2013-04-28
  • 2021-05-08
相关资源
最近更新 更多