Extension Example

Screenshot of the Extension example

The Extension application lets the user add search parameters in a dialog and launch a simple or advanced search.

The simple search has two options: Match case and Search from start. The advanced search offers search for Whole words, Search backward, and Search selection. The application starts with simple search as the default. Click the More button to show the advanced search options:

Screenshot of the Extension example

FindDialog Class Definition

The FindDialog class inherits QDialog. QDialog is the base class for dialog windows. A dialog window is a top-level window mostly used for short-term tasks and brief communications with the user.

 
Sélectionnez
class FindDialog : public QDialog
{
    Q_OBJECT

public:
    FindDialog(QWidget *parent = 0);

private:
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *fromStartCheckBox;
    QCheckBox *wholeWordsCheckBox;
    QCheckBox *searchSelectionCheckBox;
    QCheckBox *backwardCheckBox;
    QDialogButtonBox *buttonBox;
    QPushButton *findButton;
    QPushButton *moreButton;
    QWidget *extension;
};

The FindDialog widget is the main application widget, and displays the application's search options and controlling buttons.

In addition to the constructor, there are several child widgets:

  • A QLineEdit with an associated QLabel to let the user type a word to search for.

  • Several QCheckBoxes to facilitate the search options.

  • Three QPushButtons:

    • the Find button to start a search

    • the More button to enable an advanced search

    • a QWidget representing the application's extension part

FindDialog Class Implementation

Create the standard child widgets for the simple search in the constructor: the QLineEdit with the associated QLabel, two {QCheckBox}es and all the QPushButtons.

 
Sélectionnez
FindDialog::FindDialog(QWidget *parent)
    : QDialog(parent)
{
    label = new QLabel(tr("Find &what:"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));
    fromStartCheckBox = new QCheckBox(tr("Search from &start"));
    fromStartCheckBox->setChecked(true);

    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);

    moreButton = new QPushButton(tr("&More"));
    moreButton->setCheckable(true);

This snippet illustrates how you can define a shortcut key for a widget. A shortcut should be defined by putting the ampersand character (&) in front of the letter that should become the shortcut. For example, for Find what, pressing Alt and w transfers focus to the QLineEdit widget. Shortcuts can also be used for checking on or off a checkmark. For example, pressing Alt and c puts the check mark on Match Case if it was unchecked and vice versa. It is the QLabel::setBuddy() method that links a widget to the shortcut character if it has been defined.

Set the Find button's default property to true, using the QPushButton::setDefault() function. Then the push button will be pressed if the user presses the Enter (or Return) key. Note that a QDialog can only have one default button.

 
Sélectionnez
    extension = new QWidget;

    wholeWordsCheckBox = new QCheckBox(tr("&Whole words"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));
    searchSelectionCheckBox = new QCheckBox(tr("Search se&lection"));

Create the extension widget, and the QCheckBoxes associated with the advanced search options.

 
Sélectionnez
    buttonBox = new QDialogButtonBox(Qt::Vertical);
    buttonBox->addButton(findButton, QDialogButtonBox::ActionRole);
    buttonBox