Pages

Button Box

Different platforms have different conventions. This is true also for positioning OK and Cancel buttons. Luckly we can let to Qt to take care about this, that's the rason t'etre of the Button Box.

The disvantage of Button Box is that we can't set the buttons properties in the Designer, we have to do that programmatically.

We copy DlgGoToCell.ui to DlgGoToCell2.ui and modify the last one, removing buttons and spacer, and putting in a Button Box.

We recompile, so qmake could create ui_DlgGoToCell2.h that we will use instead. Then we modify the DlgGoToCell class. In the include file we refer to ui_DlgGoToCell2.h instead of ui_DlgGoToCell.h, and the class DlgGoToCell would extends QDialog, as before, but Ui::DlgGoToCell2 instead of Ui::DlgGoToCell.

The code of the constructor change in this way:

DlgGoToCell::DlgGoToCell(QWidget *parent) : QDialog(parent)
{
setupUi(this);
buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); // 1.

QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
where->setValidator(new QRegExpValidator(regExp, this));

connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); // 2.
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

1. we have to disable the button programmatically, as we said before
2. the button box provides the accepted() / rejected() signals that we use for connection to accept() / reject()

We have to change also on_where_textChanged(), to refer to the button box:

void DlgGoToCell::on_where_textChanged()
{
buttonBox->button(QDialogButtonBox::Ok)->
setEnabled(where->hasAcceptableInput());

}


I wrote this post as homework while reading "C++ GUI Programming with Qt 4, Second Edition" by Jasmin Blanchette and Mark Summerfield.

No comments:

Post a Comment