How to Convert string to const char * in C++

In C++, to convert a string datatype variable into const char* pointer we have a simple way.

Use the c_str function of the string class to get the const char pointer.

Example:

string my_str = “Hello, World”;
const char* conv_my_str = my_str.c_str();

That’s easy enough, right??Winking smile

Advertisements

XRC – The XML Based Resource System in wxWidgets

XRC allows user interface elements such as dialogs, menu bars and toolbars, to be stored in text files and loaded into the application at run-time.

XRC files can be compiled in to two formats.
    1. XRS : To store all resources in a single file.
    2. C++ : To embed resource into executable.

Why do we use XRC

  • Resource change doesn’t need recompiling and linking.
  • Separation of resources and code.
  • You can choose between different alternative resource files at run time, if necessary.
  • The XRC format uses sizers for flexibility, allowing dialogs to be resizable and highly portable.
  • The XRC format is a wxWidgets standard, and can be generated or postprocessed by any program that understands it. As it is based on the XML standard, existing XML editors can be used for simple editing purposes.

XRC Basics

  • Include Header : wx/xrc/xmlres.h.
  • wxFileSystem archive handler is needed for XRS files. Install wxFileSystem archive handler first with wxFileSystem::AddHandler(new wxArchiveFSHandler);
  • To load resource file
    • From your wxApp::OnInit() function call wxXmlResource::Get()->InitAllHandlers()
    • Then call wxXmlResource::Get()->Load(“myfile.xrc”)
  • to create a dialog from a resource, create it using the default constructor, and then load it. For example wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1");
  • set up event tables as usual but use the XRCID(str) macro to translate from XRC string names to a suitable integer identifier, for exampleEVT_MENU(XRCID("quit"), MyFrame::OnQuit).
XRC File Creation

To create an XRC file, you can use one of the following methods.

  • Create the file by hand;
  • use wxFormBuilder, Open source designer supporting C++, Python and XRC.
  • use DialogBlocks, a commercial dialog editor;
  • use XRCed, a wxPython-based dialog editor that you can find in the wxPython/tools subdirectory of the wxWidgets CVS archive;
  • use wxGlade, a GUI designer written in wxPython. At the moment it can generate Python, C++ and XRC;
  • convert WIN32 RC files to XRC with the tool in contrib/utils/convertrc.

A complete list of third-party tools that write to XRC can be found at www.wxwidgets.org/wiki/index.php/Tools.

You can use wxXmlResource::Load in a number of ways. You can pass an XRC file (XML-based text resource file) or a zip-compressed file (extension ZIP or XRS) containing other XRC.

How to use QTabWidget

Declaration and Adding Tabs

QTabWidget MainIFaceTab = new QTabWidget(this);

MainIFaceTab->addTab(new QWidget(),”Tab1”);

MainIFaceTab->addTab(new QWidget(),”Tab2”);”

Getting widget back from a tab

QWidget *wgt = MainIFaceTab->widget(0); //Returns widget of Tab1(Index starts from 0)

Setting a widget to Tab

We need to

1. Create a layout.

2. Add the widget to be set to the layout

3. Set the layout to the returned widget from Tab.

QWidget *wgt = MainIFaceTab->widget(0); //Returns widget of Tab1(Index starts from 0)

QVBoxLayout *vBoxLay = new QVBoxLayout; //create layout

vBoxLay->addWidget(qTable); //Adds a table widget to the layout

wgt->setLayout(vBoxLay); //Set the layout with the table to the returned widget.