Create a GUI starting from c++ source code

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Create a GUI starting from c++ source code

Post by wxJack »

Hi guys,
I'm Jack and I am a newbie in wxWidgets world..this is my first post.
I have an "easy" task: to create a GUI with wxWidgets on Visual Studio 2017 starting from a c++ source code that has already been written.
For excercise, I've created a GUI with wxWidgets (with a menu, some buttons..a very simple one) but it's an empty gui ; I mean that my wxButtons , for now, only show a text line if pressed.
So..the source code in c++ has of course its main function " int main(int argc, char** argv )" but, I know that the "main" function in wxWidgets Apps
is bool OnInit() .

1) So the first question is: I can't have both a main and an OnInit, right?

2) The c++ program takes from the command line a file in input. So, if I can have only the OnInit method, I have to change this modality
and take the file as input with some wxWidgets function..is it right?

I want to thank you for the opportunity to discover this amazing world. Please report me errors about my english, if any..sorry but I'm italian, I know my english is not so perfect:)
have you all a lovely day
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

1) So the first question is: I can't have both a main and an OnInit, right?
You can theoretically initialize wxWidgets from main (check wxInitializer), but then you won't have an event loop. So it's better to go the "official" wxWidgets way.
2) The c++ program takes from the command line a file in input. So, if I can have only the OnInit method, I have to change this modality and take the file as input with some wxWidgets function..is it right?
The wxApp instance has argc and argv as members and you can use them like in main.
http://docs.wxwidgets.org/trunk/classwx ... 31a5e8bfc5
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

Thank you very much for your answer @doublemax

I've read the documentation here http://docs.wxwidgets.org/trunk/classwx ... arser.html and here http://docs.wxwidgets.org/trunk/classwx ... 220d00a7d1
but I still don't know how I can tell my program that the option " - i " , followed by a string , means that I allow my program to take a file specified in the
path as an input..
I tried this(found in the documentation) , in my OnInit Method but it doesn't work, could you please help me?

Code: Select all

static const wxCmdLineEntryDesc cmdLineDesc[] =
		{
			
		{ wxCMD_LINE_OPTION, "i", "input",   "input dir"},
		{ wxCMD_LINE_PARAM,  NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
		{ wxCMD_LINE_NONE }
		};
		wxCmdLineParser parser;
		parser.SetDesc(cmdLineDesc);
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Create a GUI starting from c++ source code

Post by ONEEYEMAN »

Hi,
IIRC, this shows in the console sample. But the process should be the same for GUI program as well.
Check the sample code and try to do the same in your program.

Thank you.
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

For a quick and dirty solution, you can do it like this:

Code: Select all

bool MyApp::OnInit()
{
    // comment these out when you are passing parameters but want to handle them yourself
    //if ( !wxApp::OnInit() )
        //return false;

    if( argc == 3 && argv[1] == "-i" )
    {
      wxLogMessage("input parameter: %s", argv[2] );
    }
The "official" version would look like this:

Code: Select all

void MyApp::OnInitCmdLine(wxCmdLineParser& parser)
{
    wxApp::OnInitCmdLine(parser);

    parser.AddOption("i", "input", "input file description",  wxCMD_LINE_VAL_STRING );
}

bool MyApp::OnCmdLineParsed(wxCmdLineParser& parser)
{
    if ( !wxApp::OnCmdLineParsed(parser) )
        return false;

    wxString input_file;
    if( parser.Found("i", &input_file ) )
    {
      wxLogMessage("input file: %s", input_file);
    }

    return true;
}

// 'Main program' equivalent: the program execution "starts" here
bool MyApp::OnInit()
{
    // call the base class initialization method, currently it only parses a
    // few common command-line options but it could be do more in the future
    if ( !wxApp::OnInit() )
        return false;
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

thanks you for the answers!
Now I will study the console sample.
Only one thing @doublemax : I tried the "quick and dirty solution" but I still have the same problem
"Unknown option 'i' " .
And why do you say " if (argc == 3) ? I have only the option " -i " followed by the path of the file.
Thank you for you patience

ps: the "official" way works anyway.. ! thanks again
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

I tried the "quick and dirty solution" but I still have the same problem
"Unknown option 'i' " .
Did you comment out the call to the base method wxApp::OnInit() like i wrote in the comment?
And why do you say " if (argc == 3) ? I have only the option " -i " followed by the path of the file.
There is always one value in argv[0] which is the full path to the executable.
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

Did you comment out the call to the base method wxApp::OnInit() like i wrote in the comment?
Sorry, I didin't.
Now yes. It works only if I write

Code: Select all

bool OnInit(int argc, wxString* argv)
and it doesn't work if I write

Code: Select all

bool OnInit (int argc, wxChar** argv) 
Anyway...now I haven't anymore the error with "i unknown" , so many thanks! but my wxFrame doesn't open, don't know why..
I tried to put

Code: Select all

mainFrame->show();
system("pause");
before " return true; " statement.

Sorry if every time I solve a problem, another problems appears.
Thanks a lot


UPDATE: my WxFrame appears only if bool OnInit () has not arguments :/
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

UPDATE: my WxFrame appears only if bool OnInit () has not arguments :/
Yes, because if you change the signature by adding parameters, it's not the same method any more and it doesn't get called.

argc/argv are already members of the wxApp instance, you can use them right away without passing them as parameters.
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

doublemax wrote:
UPDATE: my WxFrame appears only if bool OnInit () has not arguments :/
Yes, because if you change the signature by adding parameters, it's not the same method any more and it doesn't get called.

argc/argv are already members of the wxApp instance, you can use them right away without passing them as parameters.
Awesome, thanks !

Now, the only wxWidgets source code works. It correctly takes an input from the command line and my wxFrame opens.
I think now I have to complete the "puzzle" linking the right pieces of codes to the buttons. Is it a good strategy if
I do this having in the same cpp file my GUI and c++ source code? Or it's better to have different files of the same project?
(I actually have different files..what I mean, is that I have in the same cpp file the OnInit and what remains of the old main of the source code c++
around which I have to build the GUI )
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

Is it a good strategy if I do this having in the same cpp file my GUI and c++ source code?
For a small, experimental program, it's ok to have all in one file. For anything more serious, you should split it into multiple files.
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

Ok thanks a lot !
Now I have new issues, omg!

The c++ source code alone (without GUI) , took a file in input, processed it and gave some values in output.
Now..If I put this code into a button in order to show a value , if pressed.
It's something like this , in my OnInit()

Code: Select all

int x = 0 ; //global 
bool OnInit() 
{
if ( !wxApp::OnInit() )
		return false;
		if (argc == 3 && argv[1] == "-i")
		{
			wxLogMessage("input parameter: %s", argv[2]);
		}
//gui structure (buttons and something else)
wxButton *helloButton = new wxButton(mainFrame, -1, "Extract VALUE", { 10,550 });
helloButton->Bind(wxEVT_BUTTON, &my_first_app::on_button_clicked, this);
----------------
return true;
}

void on_button_clicked(wxCommandEvent&)
	{
	// my old c++ source code 
	//where the file is processed and some values are obtained
	// x = first_value;
	
	wxString msg;
		msg.Printf(wxT("Found value is %d "),
			x);
		wxMessageBox(msg, wxT("About Minimal"),
			wxOK);


	void OnInitCmdLine(wxCmdLineParser& parser)
	{
		wxApp::OnInitCmdLine(parser);

		parser.AddOption("i", "input", "input file description", wxCMD_LINE_VAL_STRING);

	}

	};

	wxIMPLEMENT_APP(my_first_app);

The problem is that x shown after pressing the button is = 0 , the value I initialized x with..
For some reason the old c++ code is not working anymore , but it's not a build problem because it builds succesfully
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

I don't see any code where x is modified at all.

A common mistake would be that you modify another local variable "x" somewhere so that the global variable does not get changed.
Use the source, Luke!
wxJack
Experienced Solver
Experienced Solver
Posts: 83
Joined: Wed Jun 20, 2018 8:06 am

Re: Create a GUI starting from c++ source code

Post by wxJack »

Yes, at the beginning I write int x = 0;
inside the button I write x = firstValue;
where firstValue is one of those values I obtain processing the file in input
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Create a GUI starting from c++ source code

Post by doublemax »

inside the button I write x = firstValue;
where firstValue is one of those values I obtain processing the file in input
There is no hidden magic, there are only two possibilities:
1) The value is the same
2) You're writing into another variable "x" that shadows the global variable.
But without seeing the whole code, i can't tell you which one it is.

At the code line where you set "x = firstValue", add another line that prints the value of "firstValue":

Code: Select all

x = firstValue;
wxLogMessage("firstvalue = %d", firstValue);
This should tell you where the problem is.
Use the source, Luke!
Post Reply