Visual Studio 2019 desktop GUI application how to get console output at the same time

Jana Cole 26 Reputation points
2021-08-23T16:15:20.46+00:00

Here's a link to the tutorial this post is about. It's four very short files under the heading Event Propagation. https://zetcode.com/gui/wxwidgets/events/

I've included all of the code at the bottom of this post. There are only 2 very short header files and 2 very short source files.

I've been running wxWidgets tutorials successfully for several days. My latest tutorial indicates there should be std::cout ouput (see propagate.cpp below) along with the appearance of the GUI, but I'm not getting any text output.

I need to know how to direct the output in Visual Studio.

The GUI is a little window with a button in it. When you click the button, the couts show which classes the event is passing through as it propagates from control to parent to grandparent.

When I run the program, the GUI with the button pops up, and the button responds to hovering and to my mouse click, but there's no output in the output window at the bottom of the screen.

The ouput window provides three choices: output from Build, Build order or Debug. I set it to debug and ran the program both with and without debugging. In both cases I can click the button, but the cout text doesn't appear in the output window.

I created a new Windows desktop project, test_console_output. At the top of test_console_output.cpp the includes are "framework.h" "test_console_output.h" and <iostream>

After all the standard code, I have this:

int main()
{
    std::cout << "hi" << std::endl;
    return 0;
}

The console window pops up, but there's no output.

Where should I be seeing the text output, and how do I direct it there?

For the tutorials I'm using pre-built binaries and .props file provided by wxWidgets for Visual Studio 2019.
Let me know if you need more details.

Here's the code defining the control (MyButton class), parent (MyPanel class) and grandparent (Propagate class).
I've also posted the entry point. The wxWidgets documentation tells me the entry point is OnInit(). I have to declare a class inheriting from wxApp, with a member function OnInit(). I've called the class MyApp.

There are only two source files in the tutorial (with their header files): propagate.cpp and main.cpp

in main.cpp

#include "main.h"  
#include "propagate.h"

IMPLEMENT_APP(MyApp) // copy-paste code

bool MyApp::OnInit()
{
    Propagate* propagate = new Propagate(wxT("This demonstrates event propagation"));
    propagate->Show(true); 
    return true:
}

in propagate.cpp

#include "propagate.h"
#include <iostream>

const int ID_BUTTON = 1;

Propagate::Propagate(const wxString& title)
 : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(250, 130))
{
 MyPanel* panel = new MyPanel(this, -1);
 new MyButton(panel, ID_BUTTON, wxT("Ok"));
 Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(Propagate::OnClick));

 Centre();
}

void Propagate::OnClick(wxCommandEvent& event)
{
 std::cout << "Event reached frame class." << std::endl;
 event.Skip();
}

MyPanel::MyPanel(wxFrame* frame, int id)
 : wxPanel(frame, id)
{
 Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyPanel::OnClick));
}

void MyPanel::OnClick(wxCommandEvent& event)
{
 std::cout << "Event reached panel class." << std::endl;
 event.Skip();
}

MyButton::MyButton(MyPanel* mypanel, int id, const wxString& label)
 : wxButton(mypanel, id, label, wxPoint(15, 15))
{
 Connect(ID_BUTTON, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyButton::OnClick));
}

void MyButton::OnClick(wxCommandEvent& event)
{
 std::cout << "Event reached button class." << std::endl;
 event.Skip();
}

In propagate.h

#pragma once
#include <wx/wx.h>

class Propagate : public wxFrame
{
public:
 Propagate(const wxString& title);

 void OnClick(wxCommandEvent& event);
};

class MyPanel : public wxPanel
{
public:
 MyPanel(wxFrame* frame, int id);

 void OnClick(wxCommandEvent& event);
};

class MyButton : wxButton
{
public:
 MyButton(MyPanel* panel, int id, const wxString& label);

 void OnClick(wxCommandEvent& event);
};

in main.h

#pragma once
#include <wx/wx.h>

class MyApp : public wxApp
{
public:
 virtual bool OnInit();
};
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,553 questions
{count} vote

Accepted answer
  1. Sam of Simple Samples 5,516 Reputation points
    2021-08-23T18:17:49.56+00:00

    See std::cout in a wxWidgets frame - wxWidgets Discussion Forum. It says what has already been said, Console output doesn't work with non-console applications.

    Note that it is not normal for std::cout to go to the VS output window.

    There are many possible solutions. You can create a console window from a GUI application. Alternatively you can use the Win32 API to write debug output to the VS output window. wxWidgets: wxTextOutputStream Class Reference might be a possibility. You could create a separate GUI window that you put diagnostic information into. You could write to a text file. I do not know which is best for you and/or what you prefer.


0 additional answers

Sort by: Most helpful