Full Working Example of a wxGrid

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.
Clopper Almon
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sat Feb 04, 2012 6:42 am

Full Working Example of a wxGrid

Post by Clopper Almon » Tue Feb 07, 2017 4:05 pm

The example of a wxGrid on page 348 of the Smart and Hock (S&H) book is evidently sketchy and incomplete. If you are trying to get started using wxGrid, you may find useful the fully functioning example given at http://wiki.codeblocks.org/index.php/Using_wxGrid . It shows how to set up the grid and how to display in it both alphabetical (char) and numerical (float) data from your program. The full code and pictures of the resulting grid are given.

Any char, int, and float data in the program must be converted to wxStrings to be entered into the wxGrid. The comment in the S&H example that "some cells will store numeric values rather than strings" is misleading. It would be clearer to state that "some cells will store numeric values which have been converted to wxStrings." It is precisely to such numeric values stored as strings that the SetColFormatFloat() statement in the S&H example applies.

If you find anything in the Code::Blocks example unclear, please leave a note in the "discussion" file for that page.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3465
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Full Working Example of a wxGrid

Post by ONEEYEMAN » Tue Feb 07, 2017 5:40 pm

Hi,
The book itself is outdated.
If you need to know how to work with something - check the sample that comes with wxWidgets distribution.

Thank you.

Clopper Almon
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sat Feb 04, 2012 6:42 am

Re: Full Working Example of a wxGrid

Post by Clopper Almon » Thu Feb 09, 2017 7:38 pm

Thanks. Is the sample you refer to the same as the one at
https://github.com/wxWidgets/wxWidgets/ ... iddemo.cpp ?

If so, I have to say that I had found it more frightening than helpful. It is some 2400 lines of code with almost no comments, no indication of what it does, and no illustration of the results. For example, it starts with the definition of a custom renderer; surely not the first thing a beginner needs to learn. For a very advanced user of wxGrid who can figure out from the code what is going on, it may be very valuable, But for a beginner I found it bewildering.

If you have in mind a different sample, could you tell me how to find it?

By contrast, the example referred to in this post could be called MyFirstGrid.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3465
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Full Working Example of a wxGrid

Post by ONEEYEMAN » Thu Feb 09, 2017 8:19 pm

Hi,
Yes, this is one of the samples I referenced.
While I agree that the sample code is big and hard to follow you need to consider that the grid control itself is very complicated
And if you run the sample and check what the different menus there does, then look at the code how to do that operation from the code POV, this is really helpful.

Did you try to run this and other sample in the wxWidgets/samples directory?

Thank you.

Clopper Almon
Earned a small fee
Earned a small fee
Posts: 14
Joined: Sat Feb 04, 2012 6:42 am

Re: Full Working Example of a wxGrid

Post by Clopper Almon » Fri Feb 10, 2017 7:59 am

Hello and thanks for your interest. No, I must confess that I did not try to compile and run the sample. My route was to make wxSmith, part of the code::blocks environment, teach me how to use wxGrid. I wanted to use the grid to display a matrix of floating point numbers. The size of the matrix, the row and column names, and the numerical values in the cells are known only at runtime. From what wxSmith wrote and a few bits of information from here and there, I put together a fairly simple and clear example of how to do that. I am now in the process of applying what I learned from this toy example in a serious program.

The code that wxSmith writes gets the job done, but it may not be the most elegant and beautiful way to do so. If you have experience with wxGrid and have some suggestions about how the wxGrid parts of the example could be simplified, I would be most interested. In particular, I am writing every floating point number into a wxString and storing the wxString into the grid. If it were easily possible to store the floats as floats, not as wxStrings, in the grid and then set up a renderer to show them properly on the screen, that could be nice.

Thanks again.

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3465
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Full Working Example of a wxGrid

Post by ONEEYEMAN » Fri Feb 10, 2017 3:39 pm

Hi,
As I said before wxGrid is a very complicated control.
WHat you need is to try to compile and run the grid sample, see what it does from the run-time POV, then go to the code and try to see where the different bits of functionality are.

To answer you specific question - I think you will be able to do that. You will need to create your own grid table, fill it out with the data and then display it. But in order to do so you need to work with the sample - this is the best source of information about this and any other control/functionality used in the library.

Thank you.

User avatar
marcelinux
Knows some wx things
Knows some wx things
Posts: 40
Joined: Thu Nov 07, 2013 9:59 pm
Location: Madrid, Spain

Re: Full Working Example of a wxGrid

Post by marcelinux » Sat Feb 11, 2017 7:28 am

Clopper Almon wrote:In particular, I am writing every floating point number into a wxString and storing the wxString into the grid. If it were easily possible to store the floats as floats, not as wxStrings, in the grid and then set up a renderer to show them properly on the screen, that could be nice.
May be, this help: viewtopic.php?f=1&t=43029&p=174903&hili ... at#p174903
I just need learn a little bit more. Thank you for your help.

MagickPanda
Experienced Solver
Experienced Solver
Posts: 80
Joined: Wed Oct 19, 2016 1:41 pm

Re: Full Working Example of a wxGrid

Post by MagickPanda » Wed Feb 15, 2017 6:14 am

This is how I am using the grid, may not be optimal but it works..
Hope it helps.
Though any improvement or suggestion/fix is appreciated. -.-

Code: Select all

	m_Grid_Hold = new wxGrid(this, GRID_HOLD, wxPoint(10, 350), wxSize(500, 200), 0);
	m_Grid_Hold->CreateGrid(100, 38);
	for (int i = 0; i < OS_STRING_SIZE; i++) {
		m_Grid_Hold->SetColMinimalWidth(i, 20);
		m_Grid_Hold->SetColLabelValue(i, os_string_table[i]);
	}

	void MyFrame::updateGrids(bool bMainThread)
	{
		m_Grid_Hold->ClearGrid();

		wxGridCellCoords coords;
		string t;
		int a_int = 1;

		coords.SetCol(0);
		NumberToString(a_int, t);
		m_Grid_Hold->SetCellValue(coords, t);


		int a_int = 2;
		t.clear();
		coords.SetCol(1);
		NumberToString(int, t);
		m_Grid_Hold->SetCellValue(coords, t);

		blahblah update rest of grid..
	}

gtafan
Earned some good credits
Earned some good credits
Posts: 145
Joined: Wed Mar 29, 2017 9:52 am

Re: Full Working Example of a wxGrid

Post by gtafan » Tue Dec 11, 2018 1:31 pm

That wiki stuff seems to be really interesting, specialy since it shows how useles some wxGrid functions are. I mean for example that SetColFormatFloat, it does nothing really usefull and since it is not posible to get and set float values to a cell of the grid it apears complete useless to me. Can somebody posibly explain why such functions exist at all?
So to make clear what I would like wxGrid to use for, is matrix manipulation, but valide values should be only float values between -1 and 1.

Kvaz1r
Earned some good credits
Earned some good credits
Posts: 129
Joined: Tue Jun 07, 2016 1:07 pm

Re: Full Working Example of a wxGrid

Post by Kvaz1r » Tue Dec 11, 2018 4:45 pm

gtafan wrote:I mean for example that SetColFormatFloat, it does nothing really usefull and since it is not posible to get and set float values to a cell of the grid it apears complete useless to me. Can somebody posibly explain why such functions exist at all?
So to make clear what I would like wxGrid to use for, is matrix manipulation, but valide values should be only float values between -1 and 1.
There is section in the documentation -wxGrid Overview

As far I understand the main aim - provide convenient interface for displaying cells and not only as is.
Example:

Code: Select all

// We can specify the some cells will store numeric
// values rather than strings. Here we set grid column 5
// to hold floating point values displayed with width of 6
// and precision of 2
grid->SetColFormatFloat(5, 6, 2);
grid->SetCellValue(0, 6, "3.1415");

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 3465
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Full Working Example of a wxGrid

Post by ONEEYEMAN » Wed Dec 12, 2018 4:54 am

Hi,
Or you could derive your own wxGridTable...

Thank you.

gtafan
Earned some good credits
Earned some good credits
Posts: 145
Joined: Wed Mar 29, 2017 9:52 am

Re: Full Working Example of a wxGrid

Post by gtafan » Wed Dec 12, 2018 12:26 pm

Kvaz1r wrote:
gtafan wrote:I mean for example that SetColFormatFloat, it does nothing really usefull and since it is not posible to get and set float values to a cell of the grid it apears complete useless to me. Can somebody posibly explain why such functions exist at all?
So to make clear what I would like wxGrid to use for, is matrix manipulation, but valide values should be only float values between -1 and 1.
There is section in the documentation -wxGrid Overview

As far I understand the main aim - provide convenient interface for displaying cells and not only as is.
Example:

Code: Select all

// We can specify the some cells will store numeric
// values rather than strings. Here we set grid column 5
// to hold floating point values displayed with width of 6
// and precision of 2
grid->SetColFormatFloat(5, 6, 2);
grid->SetCellValue(0, 6, "3.1415");
I have read the documentation already and seen it all, like I said the problem is that you can´t really worck with float values using wxGrid. That "3.1415" is just a string, so you have to convert it to float, which is really a big pain in the as in wxWidgets, since it has no simple convertion function. Really not understanding why wxGrid has at leas no suport for validators.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: Full Working Example of a wxGrid

Post by PB » Wed Dec 12, 2018 1:12 pm

gtafan wrote:That "3.1415" is just a string, so you have to convert it to float, which is really a big pain in the as in wxWidgets, since it has no simple convertion function.
While you may be right about other things, I am not sure about lacking a simple conversion function. To convert a number to wxString, you can always use wxString::Format/Printf or the << operator. I don't think taht a wxString to a float conversion really get any simpler than wxString::To(C)Double()...

gtafan
Earned some good credits
Earned some good credits
Posts: 145
Joined: Wed Mar 29, 2017 9:52 am

Re: Full Working Example of a wxGrid

Post by gtafan » Wed Dec 12, 2018 2:46 pm

PB wrote:
gtafan wrote:That "3.1415" is just a string, so you have to convert it to float, which is really a big pain in the as in wxWidgets, since it has no simple convertion function.
While you may be right about other things, I am not sure about lacking a simple conversion function. To convert a number to wxString, you can always use wxString::Format/Printf or the << operator. I don't think taht a wxString to a float conversion really get any simpler than wxString::To(C)Double()...
Number to string is not such big problem in wxWidget, I am using wxString::Format whhich seems to worck similar simple like sprintf, but something like sscanf is unfortunately mising, at least I have not found it.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2057
Joined: Sun Jan 03, 2010 5:45 pm

Re: Full Working Example of a wxGrid

Post by PB » Wed Dec 12, 2018 4:23 pm

gtafan wrote:but something like sscanf is unfortunately mising, at least I have not found it.
Well, I have never used but it there is (undocumented, used in wxWidgets codebase itself) wxSscanf(). However, I believe that using that family of C functions (wrapped by wxWidgets in those wxCRT_* functions) in C++ fell out of fashion quite some time ago and one is supposed to parse strings in a different way nowadays...

But, at least on MSW those functions in std using wchar_t* should be easy to use with wxStrings?

Post Reply