- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
wxWidgets helper classes
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 will show only a tiny drop of the whole lake.
wxWidgets library can be used to create console and GUI applications. In this chapter, we will 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.
console.cpp
#include <wx/string.h> int main(int argc, char **argv) { wxPuts(wxT("A wxWidgets console application")); }
A wxWidgets console application
This is the output.
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.
addition.cpp
#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; wxPuts(str); }
Linux Operating System
This is the output.
The Printf()
method is used to format strings.
formatted.cpp
#include <wx/string.h> int main(int argc, char **argv) { int flowers = 21; wxString str; str.Printf(wxT("There are %d red roses."), flowers); wxPuts(str); }
There are 21 red roses.
This is the output.
The following example checks, whether a string contains another string. For this we have a Contains()
method.
contains.cpp
#include <wx/string.h> int main(int argc, char **argv) { wxString str = wxT("The history of my life"); if (str.Contains(wxT("history"))) { wxPuts(wxT("Contains!")); } if (!str.Contains(wxT("plain"))) { wxPuts(wxT("Does not contain!")); } }
Contains! Does not contain!
This is the output.
The Len()
method returns the number of characters in the string.
length.cpp
#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 string has 22 characters
This is the output.
The MakeLower()
and MakeUpper()
methods make characters lower case and upper case.
cases.cpp
#include <wx/string.h> int main(int argc, char **argv) { wxString str = wxT("The history of my life"); wxPuts(str.MakeLower()); wxPuts(str.MakeUpper()); }
the history of my life THE HISTORY OF MY LIFE
This is the output.
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).
shell.cpp
#include <wx/string.h> #include <wx/utils.h> int main(int argc, char **argv) { wxShell(wxT("ls -l")); }
total 40 -rwxr-xr-x 1 vronskij vronskij 9028 2007-09-06 22:10 basic -rw-r--r-- 1 vronskij vronskij 95 2007-09-06 22:09 basic.cpp -rw-r--r-- 1 vronskij vronskij 430 2007-09-06 00:07 basic.cpp~ -rwxr-xr-x 1 vronskij vronskij 11080 2007-09-05 23:17 console -rw-r--r-- 1 vronskij vronskij 500 2007-09-05 23:17 console.cpp -rw-r--r-- 1 vronskij vronskij 485 2007-09-05 23:16 console.cpp~
This is the output.
Next we will we will get the home user directory, os name, user name, host name, and total free memory.
system.cpp
#include <wx/string.h> #include <wx/utils.h> int main(int argc, char **argv) { wxPuts(wxGetHomeDir()); wxPuts(wxGetOsDescription()); wxPuts(wxGetUserName()); wxPuts(wxGetFullHostName()); long mem = wxGetFreeMemory().ToLong(); wxPrintf(wxT("Memory: %ld\n"), mem); }
/home/vronskij Linux 2.6.20-16-generic i686 jan bodnar spartan Memory: 741244928
This is the output.
Time & date
In wxWidgets, we have several classes for working with date & time.
The example shows current date or time in various formats.
datetime.cpp
#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")); wxPuts(date1); wxPuts(date2); wxPuts(date3); }
Fri Sep 7 21:28:38 2007 21:28:38 09/07/07
This is the output.
Next we will show current time in different cities.
datetime2.cpp
#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()); }
Tokyo: Sat 05:42:24 Moscow: Sat 00:42:24 Budapest: Fri 22:42:24 London: Fri 22:42:24 New York: Fri 16:42:24
This is the output.
The following example shows, how we can add date spans to our date/time. We add one month to the current time.
datespan.cpp
#include <wx/datetime.h> int main(int argc, char **argv) { wxDateTime now = wxDateTime::Now(); wxString date1 = now.Format(wxT("%B %d %Y")); wxPuts(date1); wxDateSpan span(0, 1); wxDateTime then = now.Add(span); wxString date2 = then.Format(wxT("%B %d %Y")); wxPuts(date2); }
September 07 2007 October 07 2007
This is the output.
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.
createfile.cpp
#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()) wxPuts(wxT("the file is opened")); file.Write(str); file.Close(); if (!file.IsOpened()) wxPuts(wxT("the file is not opened")); }
$ ls qoute ls: qoute: No such file or directory $ ./createfile the file is opened the file is not opened $ cat quote You make me want to be a better man.
This is the output.
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 will print the number of lines in a file, first and last lines and finally we will read and show the contents of the file.
readfile.cpp
#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()); wxPuts(wxT("-------------------------------------")); wxString s; for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() ) { wxPuts(s); } file.Close(); }
Number of lines: 8 First line: #include <glib.h> Last line: } ------------------------------------- #include <glib.h> #include <glib/gstdio.h> int main() { g_mkdir("/home/vronskij/test", S_IRWXU); }
This is the output.
The wxDir
class allows us to enumerate files and directories.
In the following example, we will print all files and directories available in the current working directory.
dir.cpp
#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) { wxPuts(file); cont = dir.GetNext(&file); } }
$ ./dir dir temp console basic.cpp basic quote createfile console.cpp basic.cpp~ test.c console.cpp~
This is the output.
In this chapter, we have covered some wxWidgets helper classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论