wxWidgets helper classes
last modified July 14, 2026
wxWidgets consists of a large group of helper classes that help programmers to do their job. These include classes for working with strings, files, XML files, streams, database, or network. Here we show only a tiny drop of the whole lake.
wxWidgets library can be used to create console and GUI applications. In this chapter, we illustrate some of the helper classes in console based applications.
Console
This is a simple console application. The application puts some text into the console window.
#include <wx/crt.h>
int main(int argc, char **argv)
{
wxPrintf(wxT("A wxWidgets console application\n"));
}
The program includes the wx/crt.h header, which provides
console I/O functions. In the main function, it calls
wxPrintf to output a string to the console.
The wxPrintf function is used to print text into the console
window. It is located in the wx/crt.h header file, which provides
console I/O functions. The wxT macro is used for backward
compatibility with older ANSI builds of wxWidgets.
$ g++ console.cpp `wx-config --cxxflags --libs` -o console
We compile the program.
$ ./console A wxWidgets console application
wxString
wxString is a class representing a character string.
In the following example, we define three wxStrings. We
create one string of these strings using addition operation.
#include <wx/crt.h>
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str1 = wxT("Linux");
wxString str2 = wxT("Operating");
wxString str3 = wxT("System");
wxString str = str1 + wxT(" ") + str2 + wxT(" ") + str3;
wxPrintf(wxT("%s\n"), str);
}
The program creates three wxString objects initialized with the
values "Linux", "Operating", and
"System". It concatenates them with spaces using the
+ operator and prints the resulting string with
wxPrintf.
$ ./addition Linux Operating System
The Printf method is used to format strings.
#include <wx/crt.h>
#include <wx/string.h>
int main(int argc, char **argv)
{
int flowers = 21;
wxString str;
str.Printf(wxT("There are %d red roses."), flowers);
wxPrintf(wxT("%s\n"), str);
}
The program uses the Printf method of wxString to
create a formatted string by embedding the integer variable
flowers into a template string. The formatted result is then
printed to the console.
$ ./formatted There are 21 red roses.
The following example checks, whether a string contains another string.
For this we have a Contains method.
#include <wx/crt.h>
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
if (str.Contains(wxT("history"))) {
wxPrintf(wxT("Contains!\n"));
}
if (!str.Contains(wxT("plain"))) {
wxPrintf(wxT("Does not contain!\n"));
}
}
The program creates a wxString and uses the
Contains method to check whether the string includes the
substrings "history" and "plain". It prints a
corresponding message for each check.
$ ./contains Contains! Does not contain!
The Len method returns the number of characters in the string.
#include <wx/crt.h>
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPrintf(wxT("The string has %d characters\n"), str.Len());
}
The program creates a wxString with the text
"The history of my life" and calls the Len method to
determine the number of characters in the string. The result is printed with
wxPrintf.
$ ./length The string has 22 characters
The MakeLower and MakeUpper methods make
characters lower case and upper case.
#include <wx/crt.h>
#include <wx/string.h>
int main(int argc, char **argv)
{
wxString str = wxT("The history of my life");
wxPrintf(wxT("%s\n"), str.MakeLower());
wxPrintf(wxT("%s\n"), str.MakeUpper());
}
The program demonstrates the MakeLower and
MakeUpper methods, which convert the string to lowercase and
uppercase respectively. Both methods modify the string in place and return a
reference to it, which is then printed with wxPrintf.
$ ./cases the history of my life THE HISTORY OF MY LIFE
Utility functions
wxWidgets has several handy utility functions for executing a process, getting a home user directory or getting the OS name.
In the following example, we execute the ls command. For this, we
have the wxShell function (Unix only).
#include <wx/app.h>
#include <wx/utils.h>
class ShellApp : public wxAppConsole
{
public:
bool OnInit() override
{
wxShell(wxT("ls -l"));
return false;
}
};
wxIMPLEMENT_APP(ShellApp);
The program defines a ShellApp class derived from
wxAppConsole and overrides the OnInit method.
Inside OnInit, it calls wxShell to execute the
ls -l shell command, which lists the contents of the current
directory in long format.
The wxIMPLEMENT_APP macro creates the application entry point. The
method returns false to prevent entering the GUI event loop since
this is a console application.
$ ./shell total 56 -rwxr-xr-x 1 jano jano 9028 Jul 14 11:10 basic -rw-r--r-- 1 jano jano 95 Jul 14 11:09 basic.cpp -rwxr-xr-x 1 jano jano 11080 Jul 14 11:05 console -rw-r--r-- 1 jano jano 500 Jul 14 11:05 console.cpp
Next we get the home directory, OS name, user name, host name, and total free memory.
#include <wx/app.h>
#include <wx/crt.h>
#include <wx/utils.h>
class SystemApp : public wxAppConsole
{
public:
bool OnInit() override
{
wxPrintf(wxT("%s\n"), wxGetHomeDir());
wxPrintf(wxT("%s\n"), wxGetOsDescription());
wxPrintf(wxT("%s\n"), wxGetUserName());
wxPrintf(wxT("%s\n"), wxGetFullHostName());
long mem = wxGetFreeMemory().ToLong();
wxPrintf(wxT("Memory: %ld\n"), mem);
return false;
}
};
wxIMPLEMENT_APP(SystemApp);
The program defines a SystemApp class derived from
wxAppConsole and overrides the OnInit method.
It calls several wxWidgets utility functions:
wxGetHomeDir retrieves the user's home directory,
wxGetOsDescription returns the OS name and version,
wxGetUserName gets the current user's login name,
wxGetFullHostName returns the machine host name, and
wxGetFreeMemory reports the amount of free system memory in
bytes. The wxIMPLEMENT_APP macro creates the application entry
point.
$ ./system /home/jano Linux 6.8.0-35-generic x86_64 jano zetbook Memory: 16374394880
Time & date
In wxWidgets, we have several classes for working with date & time.
The example shows current date or time in various formats.
#include <wx/crt.h>
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxString date1 = now.Format();
wxString date2 = now.Format(wxT("%X"));
wxString date3 = now.Format(wxT("%x"));
wxPrintf(wxT("%s\n"), date1);
wxPrintf(wxT("%s\n"), date2);
wxPrintf(wxT("%s\n"), date3);
}
The program obtains the current date and time with
wxDateTime::Now(). It then formats it in three ways: the default
full format, the time-only format (%X), and the date-only format
(%x). Each formatted string is printed with
wxPrintf.
$ ./datetime Tue Jul 14 13:29:54 2026 13:29:54 07/14/26
Next we show current time in different cities.
#include <wx/crt.h>
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxPrintf(wxT(" Tokyo: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::GMT9).c_str());
wxPrintf(wxT(" Moscow: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::MSD).c_str());
wxPrintf(wxT("Budapest: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::CEST).c_str());
wxPrintf(wxT(" London: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::WEST).c_str());
wxPrintf(wxT("New York: %s\n"), now.Format(wxT("%a %T"),
wxDateTime::EDT).c_str());
}
The program gets the current time and formats it for five different time zones
using wxDateTime::Format with the appropriate time zone constants:
GMT9 for Tokyo, MSD for Moscow, CEST for
Budapest, WEST for London, and EDT for New York.
./datetime2 Tokyo: Tue 20:35:20 Moscow: Tue 15:35:20 Budapest: Tue 13:35:20 London: Tue 12:35:20 New York: Tue 07:35:20
The following example shows, how we can add date spans to our date/time. We add one month to the current time.
#include <wx/crt.h>
#include <wx/datetime.h>
int main(int argc, char **argv)
{
wxDateTime now = wxDateTime::Now();
wxString date1 = now.Format(wxT("%B %d %Y"));
wxPrintf(wxT("%s\n"), date1);
wxDateSpan span(0, 1);
wxDateTime then = now.Add(span);
wxString date2 = then.Format(wxT("%B %d %Y"));
wxPrintf(wxT("%s\n"), date2);
}
The program creates a wxDateSpan of one month (specified by the
second argument 1) and adds it to the current date using the
Add method. It prints both the original date and the future date
formatted with the full month name, day, and year.
$ ./datespan July 14 2026 August 14 2026
Files
wxWidgets has several classes to facilitate working with files. This is low level access to files, as opposed to working with streams.
In the following example, we use the wxFile class to create a
new file and write data to it. We also test, whether the file is opened.
Note that when we create a file, it automatically stays as opened.
#include <wx/crt.h>
#include <wx/file.h>
int main(int argc, char **argv)
{
wxString str = wxT("You make me want to be a better man.\n");
wxFile file;
file.Create(wxT("quote"), true);
if (file.IsOpened()) {
wxPrintf(wxT("the file is opened\n"));
}
file.Write(str);
file.Close();
if (!file.IsOpened()) {
wxPrintf(wxT("the file is not opened\n"));
}
}
The program creates a new file named quote using
wxFile::Create with the second argument set to true
(overwrite if exists). It checks whether the file is opened with
IsOpened, writes a string to it with Write, closes it
with Close, and finally verifies that the file is no longer
open.
$ ./createfile the file is opened the file is not opened $ cat quote You make me want to be a better man.
The wxTextFile is a simple class which allows to work with text
files on line by line basis.
It is easier to work with this class than with wxFile class.
In the next example, we print the number of lines in a file, first and last lines and finally we read and show the contents of the file.
#include <wx/crt.h>
#include <wx/textfile.h>
int main(int argc, char **argv)
{
wxTextFile file(wxT("test.c"));
file.Open();
wxPrintf(wxT("Number of lines: %d\n"), file.GetLineCount());
wxPrintf(wxT("First line: %s\n"), file.GetFirstLine().c_str());
wxPrintf(wxT("Last line: %s\n"), file.GetLastLine().c_str());
wxPrintf(wxT("-------------------------------------\n"));
wxString s;
for ( s = file.GetFirstLine(); !file.Eof();
s = file.GetNextLine() )
{
wxPrintf(wxT("%s\n"), s);
}
file.Close();
}
The program opens a text file with wxTextFile, retrieves and
prints the total line count, the first line, and the last line. It then
iterates through all lines using GetFirstLine,
GetNextLine, and the Eof method, printing each line
in a loop.
The wxDir class allows us to enumerate files and directories.
In the following example, we print all files and directories available in the current working directory.
#include <wx/crt.h>
#include <wx/dir.h>
#include <wx/filefn.h>
int main(int argc, char **argv)
{
wxDir dir(wxGetCwd());
wxString file;
bool cont = dir.GetFirst(&file, wxEmptyString,
wxDIR_FILES | wxDIR_DIRS);
while (cont) {
wxPrintf(wxT("%s\n"), file);
cont = dir.GetNext(&file);
}
}
The program creates a wxDir object initialized with the current
working directory obtained from wxGetCwd. It then iterates
through all files and subdirectories using GetFirst and
GetNext, printing each entry. The flags
wxDIR_FILES | wxDIR_DIRS specify that both files and directories
should be included in the enumeration.
In this chapter, we have covered some wxWidgets helper classes.