Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Wt::WButtonGroup

 

Wt::WButtonGroup is a Wt class to display a button group.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppWButtonGroup.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-04-10T11:16:42
#
#-------------------------------------------------
QT       -= core
QT       -= gui
LIBS += \
  -lwt \
  -lwthttp \
  -lboost_program_options
QMAKE_CXXFLAGS += -DNDEBUG
TARGET = CppWRadioGroup
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += wtmain.cpp \
  wtdialog.cpp
HEADERS += \
  wtdialog.h

 

 

 

 

 

wtdialog.cpp

 

//---------------------------------------------------------------------------
/*
CppWButtonGroup, Wt::WButtonGroup demonstration
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWButtonGroup.htm
//---------------------------------------------------------------------------
#include <boost/lexical_cast.hpp>
#include <boost/numeric/conversion/cast.hpp>
//---------------------------------------------------------------------------
#include <Wt/WBreak>
#include <Wt/WGroupBox>
#include <Wt/WLabel>
#include <Wt/WRadioButton>
#include <Wt/WButtonGroup>
//---------------------------------------------------------------------------
#include "wtdialog.h"
//---------------------------------------------------------------------------
WtDialog::WtDialog()
{
  this->setContentAlignment(Wt::AlignCenter);
  ShowMain();
}
//---------------------------------------------------------------------------
void WtDialog::OnButtonGroupChanged()
{
  m_label->setText(
    "You selected "
    + m_group->selectedButton()->text());
}
//---------------------------------------------------------------------------
void WtDialog::ShowMain()
{
  this->clear();
  //Title
  {
    Wt::WLabel * const title = new Wt::WLabel("WButtonGroup");
    title->setStyleClass("title");
    this->addWidget(title);
  }
  this->addWidget(new Wt::WBreak);
  this->addWidget(new Wt::WBreak);
  //ButtonGroup
  {
    Wt::WGroupBox * const container = new Wt::WGroupBox("Which option?",this);
    m_group = new Wt::WButtonGroup(this);

    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("A", container);
      new Wt::WBreak(container);
      m_group->addButton(button,0);
    }
    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("B", container);
      new Wt::WBreak(container);
      m_group->addButton(button,1);
    }
    {
      Wt::WRadioButton * const button = new Wt::WRadioButton("C", container);
      new Wt::WBreak(container);
      m_group->addButton(button,2);
    }

    m_group->setCheckedButton(m_group->button(0));
    m_group->checkedChanged().connect(
      this,&WtDialog::OnButtonGroupChanged);
  }
  this->addWidget(new Wt::WBreak);
  {
    m_label = new Wt::WLabel;
    this->addWidget(m_label);
  }
  this->addWidget(new Wt::WBreak);
}
//---------------------------------------------------------------------------

 

 

 

 

 

wtdialog.h

 

//---------------------------------------------------------------------------
/*
CppWButtonGroup, Wt::WButtonGroup demonstration
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWButtonGroup.htm
//---------------------------------------------------------------------------
#ifndef WTDIALOG_H
#define WTDIALOG_H
//---------------------------------------------------------------------------
#include <Wt/WContainerWidget>
//---------------------------------------------------------------------------
namespace Wt
{
  struct WButtonGroup;
  struct WLabel;
}
//---------------------------------------------------------------------------
struct WtDialog : public Wt::WContainerWidget
{
  WtDialog();

  private:
  Wt::WLabel * m_label;
  Wt::WButtonGroup * m_group;
  void OnButtonGroupChanged();
  void ShowMain();
};
//---------------------------------------------------------------------------
#endif // WTDIALOG_H

 

 

 

 

 

wtmain.cpp

 

//---------------------------------------------------------------------------
/*
CppWButtonGroup, Wt::WButtonGroup demonstration
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppWButtonGroup.htm
//---------------------------------------------------------------------------
#include <iostream>
//---------------------------------------------------------------------------
#include <boost/program_options.hpp>
//---------------------------------------------------------------------------
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WText>
//---------------------------------------------------------------------------
#include "wtdialog.h"
//---------------------------------------------------------------------------
struct WtApplication : public Wt::WApplication
{
  WtApplication(const Wt::WEnvironment& env);
};
//---------------------------------------------------------------------------
WtApplication::WtApplication(
  const Wt::WEnvironment& env)
  : WApplication(env)
{
  setTitle("WButtonGroup");
  this->useStyleSheet("wt.css");
  root()->addWidget(new WtDialog);
}
//---------------------------------------------------------------------------
Wt::WApplication *createApplication(const Wt::WEnvironment& env)
{
  return new WtApplication(env);
}
//---------------------------------------------------------------------------
int main(int argc, char **argv)
{
  // Declare the supported options.
  boost::program_options::options_description d(
    "Allowed options for WButtonGroup");
  std::string docroot;
  std::string http_address;
  std::string http_port;
  d.add_options()
      ("help",
        "produce this help message")
      ("docroot",
         boost::program_options::value<std::string>(&docroot)->default_value("."),
         "the docroot")
      ("http-address",
         boost::program_options::value<std::string>(&http_address)->default_value("0.0.0.0"),
         "the server's http address")
      ("http-port",
         boost::program_options::value<std::string>(&http_port)->default_value("8080"),
         "the server's http port")
      ;

  boost::program_options::variables_map m;
  boost::program_options::store(
    boost::program_options::parse_command_line(
      argc, argv, d), m);
  boost::program_options::notify(m);

  if (m.count("help"))
  {
    //Display the options_description
    std::cout << d << "\n";
    return 0;
  }

  //Create the arguments in std::string format
  std::vector<std::string> v(7);
  v[0] = argv[0];
  v[1] = "--docroot";
  v[2] = docroot;
  v[3] = "--http-address";
  v[4] = http_address;
  v[5] = "--http-port";
  v[6] = http_port;

  //Convert the arguments to char* format
  std::vector<char*> w(7);
  for (int i=0; i!=7; ++i) w[i] = &v[i][0];

  //Give Wt the modified parameters
  return WRun(w.size(), &w[0], &createApplication);
}
//---------------------------------------------------------------------------

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict