question

JanaCole-7204 avatar image
1 Vote"
JanaCole-7204 asked SimpleSamples edited

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

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++
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

It sounds to me like the tutorial you are using needs for you to create a console application.

0 Votes 0 ·

Would that also display the GUI?

0 Votes 0 ·

I can't answer your question without more information about the tutorial you are working with. The minimal code that you posted would produce console output if it was used in a console application. A typical Windows desktop application (i.e., a GUI application) ordinarily has an entry point of WinMain or wWinMain and does not create a console when it starts up. So it's not exactly clear how you created your project and we don't know what code the tutorial is using.

0 Votes 0 ·
Show more comments

Instead of having me ask you a million questions I suggest you share your project and a link to the wxWidgets tutorial using Onedrive, dropbox or equivalent.

0 Votes 0 ·

I posted a link to the tutorial at the top of my post. I also posted the entire tutorial. The files are very short. If you need more details, let me know.

I just need to know how to see the output of the cout statements in propagate.cpp.

0 Votes 0 ·

1 Answer

SimpleSamples avatar image
0 Votes"
SimpleSamples answered SimpleSamples edited

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.



· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the links. I checked them both, and I see solutions for me there.

0 Votes 0 ·