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 to enable debug console?

How to enable debug console?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • C Offline
    C Offline
    ChiaoHuang
    wrote on last edited by
    #1

    In my case, sometime I will need the console to show log for my QT process and C++ DLL library.

    I found the some code and show console from Internet.

        // detach from the current console window
        // if launched from a console window, that will still run waiting for the new console (below) to close
        // it is useful to detach from Qt Creator’s <Application output> panel
        FreeConsole();
    
        // create a separate new console window
        AllocConsole();
    
        // attach the new console to this application’s process
        AttachConsole(GetCurrentProcessId());
    
        SetConsoleOutputCP(65001);
    
        // reopen the std I/O streams to redirect I/O to the new console
        freopen("CON", "w", stdout);
        freopen("CON", "w", stderr);
        freopen("CON", "r", stdin);
    

    This is my example code

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        lib.setFileName("ExampleDLL.dll");
        if(!lib.load())
        {
            qDebug() << "load ExampleDLL.dll library failed. " << lib.errorString();
        }
        else
        {
            qDebug() << "loaded ExampleDLL.dll library";
        }
    
        printHelloWorldFun = (printHelloWorld)lib.resolve("printHelloWorld");
    
    }
    
    void MainWindow::on_DebugButton_clicked()
    {
        // detach from the current console window
        // if launched from a console window, that will still run waiting for the new console (below) to close
        // it is useful to detach from Qt Creator’s <Application output> panel
        FreeConsole();
    
        // create a separate new console window
        AllocConsole();
    
        // attach the new console to this application’s process
        AttachConsole(GetCurrentProcessId());
    
        SetConsoleOutputCP(65001);
    
        // reopen the std I/O streams to redirect I/O to the new console
        freopen("CON", "w", stdout);
        freopen("CON", "w", stderr);
        freopen("CON", "r", stdin);
    }
    
    void MainWindow::on_QTButton_clicked()
    {
        fprintf(stdout, "Hello World from QT\n");
    }
    
    void MainWindow::on_DLLButton_clicked()
    {
        printHelloWorldFun();
    }
    

    My DLL example code:

    void printHelloWorld()
    {
    	fprintf(stdout, "Hello World from DLL\n");
    }
    

    It sometimes works and sometimes doesn't.
    alt text
    alt text

    How to fix this issue?
    Do everyone have any idea?

    Thanks

    JonBJ 1 Reply Last reply
    0
    • C ChiaoHuang

      In my case, sometime I will need the console to show log for my QT process and C++ DLL library.

      I found the some code and show console from Internet.

          // detach from the current console window
          // if launched from a console window, that will still run waiting for the new console (below) to close
          // it is useful to detach from Qt Creator’s <Application output> panel
          FreeConsole();
      
          // create a separate new console window
          AllocConsole();
      
          // attach the new console to this application’s process
          AttachConsole(GetCurrentProcessId());
      
          SetConsoleOutputCP(65001);
      
          // reopen the std I/O streams to redirect I/O to the new console
          freopen("CON", "w", stdout);
          freopen("CON", "w", stderr);
          freopen("CON", "r", stdin);
      

      This is my example code

      mainwindow.cpp

      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          lib.setFileName("ExampleDLL.dll");
          if(!lib.load())
          {
              qDebug() << "load ExampleDLL.dll library failed. " << lib.errorString();
          }
          else
          {
              qDebug() << "loaded ExampleDLL.dll library";
          }
      
          printHelloWorldFun = (printHelloWorld)lib.resolve("printHelloWorld");
      
      }
      
      void MainWindow::on_DebugButton_clicked()
      {
          // detach from the current console window
          // if launched from a console window, that will still run waiting for the new console (below) to close
          // it is useful to detach from Qt Creator’s <Application output> panel
          FreeConsole();
      
          // create a separate new console window
          AllocConsole();
      
          // attach the new console to this application’s process
          AttachConsole(GetCurrentProcessId());
      
          SetConsoleOutputCP(65001);
      
          // reopen the std I/O streams to redirect I/O to the new console
          freopen("CON", "w", stdout);
          freopen("CON", "w", stderr);
          freopen("CON", "r", stdin);
      }
      
      void MainWindow::on_QTButton_clicked()
      {
          fprintf(stdout, "Hello World from QT\n");
      }
      
      void MainWindow::on_DLLButton_clicked()
      {
          printHelloWorldFun();
      }
      

      My DLL example code:

      void printHelloWorld()
      {
      	fprintf(stdout, "Hello World from DLL\n");
      }
      

      It sometimes works and sometimes doesn't.
      alt text
      alt text

      How to fix this issue?
      Do everyone have any idea?

      Thanks

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @ChiaoHuang
      Just in case opening a CON is still block-buffered, put a fflush(stdout) after your fprintf(stdout, "...\n") statements.

      C 1 Reply Last reply
      0
      • JonBJ JonB

        @ChiaoHuang
        Just in case opening a CON is still block-buffered, put a fflush(stdout) after your fprintf(stdout, "...\n") statements.

        C Offline
        C Offline
        ChiaoHuang
        wrote on last edited by
        #3

        @JonB

        Thank you for your suggestion.
        Unfortunately it is not working sometimes.

        Thanks

        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