Hi guys!

I'm a newbie in the use of witty! I've found some problems in the use of the WButtonGroup class. I'm using the version wt-2.99.3 of the libraries. My problem is in the use of the function setCheckedButton to choose the button I want to be checked.

I attach an example I made inspired by the example of the documentation of WButtonGroup. In the WlineEdit it is asked to insert the number of the button to check. When it is pressed enter the relative button should be checked. The problem is that it only work with button number 3 (NoVote).

I cannot understand what is wrong.

Can you help me!

Thank you

Stefano







/*
 * main.cpp
 *
 */


#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>
#include <Wt/WRadioButton>
#include <Wt/WButtonGroup>
#include <Wt/WGroupBox>
#include <Wt/WIntValidator>

#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>

using namespace std ;
using namespace Wt;


enum Vote { Republican = 1, Democrate = 2, NoVote = 10 };

class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env);

private:
  WLineEdit *nameEdit_;
  WText *greeting_;
  WButtonGroup *group_ ;

  void selectButton();
  void update() ;
};


HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)
{

  // use a button group to logically group the 3 options
  group_ = new WButtonGroup(this);

  WRadioButton *button;
  button = new WRadioButton("I voted Republican", root());
  new WBreak(root());
  group_->addButton(button, Republican);

  button = new WRadioButton("I voted Democrat", root());
  new WBreak(root());
  group_->addButton(button, Democrate);

  button = new WRadioButton("I didn't vote", root());
  new WBreak(root());
  group_->addButton(button, NoVote);


  new WBreak(root());
  root()->addWidget(new WText (WString::fromUTF8("Which button should I check? "))) ;
  nameEdit_ = new WLineEdit(root());

  new WBreak(root());
  greeting_ = new WText(root());                         // empty text
  WIntValidator *dvCheck = new WIntValidator(1,3);
  dvCheck->setMandatory(true);
  nameEdit_->setValidator(dvCheck);
  nameEdit_->enterPressed().connect(SLOT(this, HelloApplication::selectButton));

  update() ;


}


void HelloApplication::selectButton() {

	ostringstream buffer(ostringstream::out);

	int bs=atoi(nameEdit_->text().narrow().c_str());

	switch (bs) {
	case 1:
		group_->setCheckedButton(group_->button(Republican)) ;
		cerr << "Republican" << endl ;
		break ;
	case 2:
		group_->setCheckedButton(group_->button(Democrate)) ;
		cerr << "Democrate" << endl ;
		break ;
	case 3:
		group_->setCheckedButton(group_->button(NoVote)) ;
		cerr << "NoVote" << endl ;
		break ;
	default:
		buffer.str("");
		buffer << "Checkable button from 1 to 3" ;
		greeting_->setText(WString::fromUTF8(buffer.str()));
		return ;
	}

	buffer.str("");
	buffer << "Checked button: " << group_->checkedId() ;

	greeting_->setText(WString::fromUTF8(buffer.str()));
}

void HelloApplication::update() {

	// update by checking the Republican button
	group_->setCheckedButton(group_->button(Republican)) ;

}

WApplication *createApplication(const WEnvironment& env)
{
  return new HelloApplication(env);
}

int main(int argc, char **argv)
{
  return WRun(argc, argv, &createApplication);
}

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to