9.9.  The Event Loop: Revisited

[ fromfile: eventloop-revisited.xml id: eventloop-revisited ]

Figure 9.22.  KeySequenceLabel Widget

KeySequenceLabel Widget

Example 9.13. src/eventloop/keysequencelabel.h

[ . . . . ]
#include "ui_keysequencelabel.h"
#include <QList>
#include <QPair>
class QObjectBrowserAction;

class KeySequenceLabel : public QMainWindow, private Ui::KeySequenceLabel {
    Q_OBJECT
 public:
    explicit KeySequenceLabel(QWidget* parent = 0);
 protected:                         1
    void changeEvent(QEvent* e);
    void keyPressEvent(QKeyEvent*);
    void leaveEvent(QEvent*);
    void enterEvent(QEvent*);
    void paintEvent(QPaintEvent*);
    void timerEvent(QTimerEvent*);  2
    void updateUi();
 private slots:
    void on_actionShow_ObjectBrowser_triggered(bool checked);
    void on_m_clearButton_clicked();
    void on_actionQuit_triggered();
 private:
    QObjectBrowserAction* m_browserAction;
    QList<QPair<int, int> > m_keys;
    int m_paints;
};
[ . . . . ]

1

QWidget event handler overrides

2

QObject event handler override


Example 9.14. src/eventloop/keysequencelabel.cpp

[ . . . . ]

void KeySequenceLabel::keyPressEvent(QKeyEvent* evt) {
    bool doNothing = false;

    if (evt->key() == 0) doNothing = true;
    if (m_keys.size() > 3) doNothing = true;
    if (doNothing) {
        QMainWindow::keyPressEvent(evt); 1
        return;
    }
    QPair<int, int> pair = QPair<int, int>(evt->modifiers(), evt->key());
    m_keys << pair;
    evt->accept();
    updateUi();
}

void KeySequenceLabel::updateUi() {
    if (!m_keys.isEmpty()) {
        int keys[4] = {0,0,0,0};
        for (int i=0; i<m_keys.size(); ++i) {
            QPair<int, int> pair = m_keys[i];
            keys[i] = pair.first | pair.second;
        }
        QKeySequence seq = QKeySequence(keys[0], keys[1], keys[2], keys[3]);
        m_label->setText(seq.toString());
    }
    else m_label->clear();
}

1

QWidget's base class handler responds to ESC for pop-up windows.


Example 9.15. src/eventloop/keysequencelabel.cpp

[ . . . . ]

void KeySequenceLabel::enterEvent(QEvent* evt) {

    statusBar()->showMessage(tr("Mouse is in da house"));
    evt->accept();

}

void KeySequenceLabel::leaveEvent(QEvent* evt) {
    statusBar()->showMessage(tr("Mouse has left the building"));
    evt->accept();
}

Example 9.16. src/eventloop/keysequencelabel.cpp

[ . . . . ]

KeySequenceLabel::KeySequenceLabel(QWidget* parent) :
    QMainWindow(parent), m_browserAction(new QObjectBrowserAction(this)) {

    setupUi(this);
    startTimer(2000); 1
    m_paints = 0;
}

void KeySequenceLabel::timerEvent(QTimerEvent*) {
    m_lcdNumber->display(m_paints);
}

void KeySequenceLabel::paintEvent(QPaintEvent* evt) {
    ++m_paints;
    QMainWindow::paintEvent(evt);
}

1

A timer event occurs every 2 seconds.


Figure 9.23.  QObjectBrowser Widget

QObjectBrowser Widget



[47] Contributed by Jeremy Magland, you can follow the development of this utility from its initial posting and see some community suggestions and the author's responses at http://tinyurl.com/2a6qkpy