WxFAQ

From WxWiki
Jump to: navigation, search

This is a user maintained FAQ, the purpose of which is to allow easy augmentation of questions and answers to build up a large knowledge base on wxWidgets. If you don't find your question or the answer to your question doesn't give enough details, check the Official wxWidgets FAQ as well as the FAQs maintained on the wxWidgets Forums (don't miss the exhaustive C++ Users Forum FAQ or Paul's wxWidgets Newbie FAQ).

Can I sell my application?

There are certain exceptions, please read the License article for more information.

Why are backgrounds of windows ugly, dark grey on Windows? I want them like my dialog boxes.

This is the default background colour for frames on Microsoft Windows. You can get the familiar light-grey background by putting a wxPanel on the frame first, and putting all your other controls on this wxPanel. If the wxPanel is the only child window of the frame then it will automatically take up the entire client area of the frame.

wxBoxSizer* topSizer = new wxBoxSizer( wxHORIZONTAL );
wxPanel *panel = new wxPanel(this, wxID_ANY, 
                             wxDefaultPosition, 
                             wxDefaultSize,
                             wxTAB_TRAVERSAL,
                             _("mypanel"));
panel->SetSizer(topSizer);
wxButton *ok_button = new wxButton( panel, wxID_OK );
wxButton *cancel_button = new wxButton( panel, wxID_CANCEL );
topSizer->Add(ok_button, 0, wxALL, 10);
topSizer->Add(cancel_button, 0, wxALL, 10);

See wxPanel in the documentation for more information on panels.

Objects I put in sizers are not expanding like I want them to

wxWidgets sizers are quite powerful but understanding how they work can be a little confusing at first. In addition to sizer tutorials and documentation around, here is the golden tip :

There are two different ways to make a widget expand when calling wxSizer::Add

  • The first is the proportion integer parameter
    • this flag acts in the "main" direction of the sizer. For instance, in a vertical sizer, the proportion dictates vertical grow, whereas in a horizontal sizer the proportion flag dictates the horizontal growth.
    • A proportion of "0" means "stick to the minimum size in this direction", while a proportion > 0 makes the widget grow (and a widget with proportion "2" will be twice as big as a widget with proportion "1")
  • The second is the wxEXPAND style flag.
    • This flag basically causes the widget to expand in the direction(s) not covered by the proportion flag mentioned just above. For instance, in a horizontal sizer, the wxEXPAND flag makes the widget expand vertically (if there is room), whereas in a vertical sizer it makes the widget expand horizontally.
    • It's important to note that this flag will only make the widget expand within its parent; so also make sure the parent container is large enough and/or expands too, as setting the wxEXPAND flag on a widget that is placed e.g. inside a small panel may not cause much visible effect since the widget won't expand beyond its parent panel.

Objects I put in box sizers are not centering like I want them to

There are two different ways to make a widget center when calling wxSizer::Add

  • The first is the flag parameter, where you can pass values like wxALIGN_CENTER_VERTICAL or wxALIGN_CENTER_HORIZONTAL . In a vertical box sizer, you can pass the wxALIGN_CENTER_HORIZONTAL flag to center horizontally; in a horizontal box sizer you can pass the wxALIGN_CENTER_VERTICAL to vertically center the object. You cannot pass flags to center horizontally in a horizontal sizer, or flags to center vertically in a vertical sizer, as it wouldn't make sense (what would a horizontal sizer do if some objects are horizontally centered but others not?)
  • The second is to add stretch spacers (see wxSizer::AddStretchSpacer) before and after the object. In a horizontal box sizer, this allows to center an object (or a group of objects) horizontally; In a vertical box sizer, this allows to center an object (or a group of objects) vertically.

Why does my app take a Windows-95-like look on Windows ?

This question is asked very frequently, do a quick search for 'manifest'. This is a Windows issue, not a wxWidgets issue.

A quick search on the forum gave these :

Also see this official readme from the wxWidgets source tree : http://svn.wxwidgets.org/viewvc/wx/wxWidgets/trunk/docs/msw/winxp.txt?view=markup

How can I convert a wxString to <insert type here>?

See Converting everything to and from wxString

How can I convert a <insert type here> to wxString?

See Converting everything to and from wxString

Why do I get weird issues when modifying GUI controls from a thread?

wxWidgets (like most GUI toolkits underneath it) is not thread-safe, and handling of GUI components should always be done exclusively in the main thread. So your threads must not edit the GUI controls directly, but post an event to the main thread and let it do it : Inter-Thread_and_Inter-Process_communication#Sending_custom_events_to_the_main_thread

Note that, although ::wxMutexGuiEnter and ::wxMutexGuiLeave exist, they are more a hack than a desirable solution, and will not work in every situation; so event passing between thread remains the correct way to go.

XPM images don't display the 'silver' colour correctly

In wxWidgets XPM, "silver" is not defined as a color. This "silver" will be generated by a lot of external tools that output XPM. You will need to replace it manually in the XPM file with the hex number C6C6C6.

Why is the IMPLEMENT_APP macro used to generate the entry point? Can't I just declare a "main" function?

Actually, nothing prevents you from declaring a main function yourself if you do not wish to use the macros. The reason the IMPLEMENT_APP macro exists is simply because not all platforms use "main"; as a notable example, Windows uses "WinMain" instead. So the macro is there to save you from the need to write different mains for different platforms.

How can I access argc/argv (command-line arguments passed to my application) ?

See these manual pages :

Why does my control take up the whole frame even though I give it an explicit size?

If you only have one window in a frame, that window will automatically take the whole space. If you really want only one window that doesn't take the whole space, try adding a second, blank wxWindow to the frame.

Under normal circumstances, however, the only child of your frame should be a wxPanel, and any other controls should be descendents of that panel. Having a panel present in this manner can solve a number of unexpected behaviors including incorrect colors, missing events, and improper sizer behavior.

Why isn't wxString( wxT("(non iso8859-1 chars)") ) displayed correctly and why can't it be converted back to the initial encoding?

Because the input charset of a cpp compiler is 7bit-ascii. Chars >0x7f are considered iso8859-1. It is these iso8859-1 chars that are encoded to unicode and not the ones of your native language. Do not use wchar_t string literals. Use wxString( "(non iso8859-1 chars)", wxConvLibc ) instead.

Another option is to configure your compiler to accept Unicode encoded files.

What's the difference between a wxFrame and a wxWindow?

A wxFrame is a top-level window, normally referred to as windows in interfaces. A wxWindow is a generic class for any drawable object on the screen. Under normal circumstances, you create a frame, place a panel in the frame, then place other wxWindow subclasses (wxButton, wxTextCtrl, etc.) in the panel.

What's the difference between a wxFrame and a wxDialog?

A frame is what you normally think of as a window; it's used for the primary application display or documents or other long-running views. A dialog is for short term interactions to query the user for specific information.

From the end user perspective, some platforms might render dialogs differently, e.g. with different decorations. From the programmer perspective, dialogs can (more easily) be modal, and don't delete themselves on close so that you can retrieve the information from them after close.

If you were writing a web browser in wxWidgets, the browser window would be a frame and the preferences dialog would be a dialog.

If you were writing a word processor in wxWidgets, the document would be a frame and the font dialog would be a dialog.

If you're tempted to put an "OK" button on a top level window, it's probably a dialog. If you're tempted to use a menu with a top level window, it's probably a frame.

How can I make plots or charts with wxWidgets?

One way is to manually draw them on a panel (http://wiki.wxwidgets.org/Drawing_on_a_panel_with_a_DC)

However, several people already went through that and provided various add-ons you can try :

How can I create diagrams with connected nodes?

A not very good idea is to create a wxPanel for each node and then move the panels around with drag and drop (wxWidgets does not support overlapping components, and besides this will get very slow when you have a lot of nodes)

One way is to manually draw them on a panel (http://wiki.wxwidgets.org/Drawing_on_a_panel_with_a_DC)

However, several people already went through that and provided various add-ons you can try :

wxODBC was removed in wxWidgets 2.9. What are the alternatives?

First and foremost, you can use ODBC directly; your database library doesn't need to have "wx" in its name. Especially since wxWidgets 2.9 supports transparent conversion between wxString and char*/string there should be no problem using third-party libraries.

Other alternatives that are often mentionned "

And in addition to that there are usually APIs for each specific database type