Ignore:
Timestamp:
Nov 8, 2009, 10:57:37 PM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

tests: widget: Improved focus traversal test.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/widget/widget.cpp

    r300 r304  
    6262}
    6363
     64////////////////////////////////////////////////////////////////////////////////
     65
     66class MyChild : public QWidget
     67{
     68    Q_OBJECT
     69
     70public:
     71
     72    MyChild(QWidget *aParent) : QWidget(aParent), mPressed(0)
     73    {
     74        setFocusPolicy(Qt::StrongFocus);
     75
     76        resize(64, 32);
     77    }
     78
     79    void paintEvent(QPaintEvent *aE)
     80    {
     81        qDebug() << qWidgetName(this) << __FUNCTION__
     82                 << ": " << aE->rect() << "focus" << hasFocus();
     83
     84        QPainter p(this);
     85        p.setRenderHint(QPainter::Antialiasing);
     86
     87        if (hasFocus())
     88            p.fillRect(aE->rect(), mPressed % 2 ? Qt::red : Qt::green);
     89        else
     90            p.fillRect(aE->rect(), Qt::gray);
     91    }
     92
     93    void mousePressEvent(QMouseEvent *aE)
     94    {
     95        ++mPressed;
     96        update();
     97    }
     98
     99    void focusInEvent(QFocusEvent *aE)
     100    {
     101        qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
     102        QWidget::focusInEvent(aE);
     103    }
     104
     105    void focusOutEvent(QFocusEvent *aE)
     106    {
     107        qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
     108        QWidget::focusOutEvent(aE);
     109    }
     110
     111private:
     112
     113    int mPressed;
     114};
     115
     116////////////////////////////////////////////////////////////////////////////////
     117
    64118class MyButton : public QPushButton
    65119{
     
    68122    MyButton(QWidget *aParent) : QPushButton(aParent) {}
    69123
     124#if 0
    70125    void focusInEvent(QFocusEvent *aE)
    71126    {
    72 #if 0
    73         qDebug() << __FUNCTION__ << ":" << text() << "reason" << aE->reason()
     127        qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
     128                << "reason" << aE->reason()
    74129                 << "focus" << (qApp->focusWidget() ?
    75130                    qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
    76 #endif
     131
     132        QPushButton::focusInEvent(aE);
    77133    }
    78134
    79135    void focusOutEvent(QFocusEvent *aE)
    80136    {
    81 #if 0
    82         qDebug() << __FUNCTION__ << ":" << text() << "reason" << aE->reason()
     137        qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << text()
     138                << "reason" << aE->reason()
    83139                 << "focus" << (qApp->focusWidget() ?
    84140                    qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
    85 #endif
    86     }
     141        QPushButton::focusOutEvent(aE);
     142    }
     143#endif
    87144};
    88145
     146
     147////////////////////////////////////////////////////////////////////////////////
     148
    89149class MyCombo : public QComboBox
    90150{
     
    93153    MyCombo(QWidget *aParent) : QComboBox(aParent) {}
    94154
     155#if 0
    95156    void focusInEvent(QFocusEvent *aE)
    96157    {
    97         update();
    98 #if 0
    99         qDebug() << __FUNCTION__ << ":" << currentText() << "reason" << aE->reason()
     158        qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
     159                 << "reason" << aE->reason()
    100160                 << "focus" << (qApp->focusWidget() ?
    101161                    qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
    102 #endif
     162        QComboBox::focusInEvent(aE);
    103163    }
    104164
    105165    void focusOutEvent(QFocusEvent *aE)
    106166    {
    107         update();
    108 #if 0
    109         qDebug() << __FUNCTION__ << ":" << currentText() << "reason" << aE->reason()
     167        qDebug() << qWidgetName(this) << __FUNCTION__ << ":" << currentText()
     168                 << "reason" << aE->reason()
    110169                 << "focus" << (qApp->focusWidget() ?
    111170                    qWidgetName(qApp->focusWidget()) : QDbgStr(QLatin1String("<none>")));
    112 #endif
    113     }
     171        QComboBox::focusOutEvent(aE);
     172    }
     173#endif
    114174
    115175#if 0
     
    169229};
    170230
     231////////////////////////////////////////////////////////////////////////////////
     232
    171233class MyWidget : public QWidget
    172234{
    173235public:
    174236
    175     MyWidget()
    176     {
    177         //setFocusPolicy(Qt::StrongFocus);
    178 
    179 //      MyButton *btn1 = new MyButton(this);
    180 //      btn1->setText(QLatin1String("Hello 1"));
    181 //      btn1->setObjectName("Hello 1");
    182 //      btn1->move(20, 20);
    183 //      MyButton *btn2 = new MyButton(this);
    184 //      btn2->setText(QLatin1String("Hello 2"));
    185 //      btn2->setObjectName("Hello 2");
    186 //      btn2->move(20, 60);
    187 
    188         QComboBox *cb1 = new MyCombo(this);
    189         cb1->addItem(QLatin1String("Test 1"));
    190         cb1->addItem(QLatin1String("Test 2"));
     237    MyWidget() : mPressed(0)
     238    {
     239//      setFocusPolicy(Qt::StrongFocus);
     240
     241        MyButton *btn1 = new MyButton(this);
     242        btn1->setText(QLatin1String("Hello 1"));
     243        btn1->setObjectName("Hello 1");
     244        btn1->move(20, 20);
     245        MyButton *btn2 = new MyButton(this);
     246        btn2->setText(QLatin1String("Hello 2"));
     247        btn2->setObjectName("Hello 2");
     248        btn2->move(20, 60);
     249
     250//      QComboBox *cb1 = new MyCombo(this);
     251//      cb1->addItem(QLatin1String("Test 1"));
     252//      cb1->addItem(QLatin1String("Test 2"));
    191253
    192254//      QComboBox *cb2 = new MyCombo(this);
     
    194256//      cb2->addItem(QLatin1String("Test 4"));
    195257
    196         QVBoxLayout *mainLayout = new QVBoxLayout();
     258//      QVBoxLayout *mainLayout = new QVBoxLayout();
    197259//      mainLayout->addWidget(btn1);
    198260//      mainLayout->addWidget(btn2);
    199         mainLayout->addWidget(cb1);
     261//      mainLayout->addWidget(cb1);
    200262//      mainLayout->addWidget(cb2);
    201263
    202         setLayout(mainLayout);
     264//      setLayout(mainLayout);
     265
     266#if 0
     267        new MyChild(this);
     268#endif
     269
    203270    };
    204271
     
    211278        p.setRenderHint(QPainter::Antialiasing);
    212279
    213         p.fillRect(0, 0, 20, 20, Qt::green);
     280        p.fillRect(0, 0, 20, 20, mPressed % 2 ? Qt::red : Qt::green);
    214281
    215282        p.setPen(Qt::black);
     
    238305    void focusInEvent(QFocusEvent *aE)
    239306    {
    240         qDebug() << __FUNCTION__ << ": reason" << aE->reason();
     307        qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
     308        QWidget::focusInEvent(aE);
    241309    }
    242310
    243311    void focusOutEvent(QFocusEvent *aE)
    244312    {
    245         qDebug() << __FUNCTION__ << ": reason" << aE->reason();
     313        qDebug() << qWidgetName(this) << __FUNCTION__ << ": reason" << aE->reason();
     314        QWidget::focusOutEvent(aE);
    246315    }
    247316#endif
     
    286355
    287356#if 0
    288     void mouseMoveEvent(QMouseEvent *aE)
     357    void mousePressEvent(QMouseEvent *aE)
    289358    {
    290359        qDebug() << __FUNCTION__ << ": btn" << aE->button()
     
    292361                                              (int) aE->buttons(), (int) aE->modifiers()))
    293362                 << "gpos" << aE->globalPos() << "pos" << aE->pos();
    294     }
    295 
    296     void mousePressEvent(QMouseEvent *aE)
     363
     364        ++mPressed;
     365        update();
     366    }
     367
     368    void mouseReleaseEvent(QMouseEvent *aE)
    297369    {
    298370        qDebug() << __FUNCTION__ << ": btn" << aE->button()
     
    302374    }
    303375
    304     void mouseReleaseEvent(QMouseEvent *aE)
     376    void mouseMoveEvent(QMouseEvent *aE)
    305377    {
    306378        qDebug() << __FUNCTION__ << ": btn" << aE->button()
     
    339411    }
    340412#endif
     413
     414private:
     415
     416    int mPressed;
    341417};
    342418
     
    347423    MyWidget widget;
    348424    widget.resize(100, 100);
    349     widget.move(500, 500);
     425    widget.move(1280-168, 1024-200);
    350426
    351427    widget.show();
    352    
    353 #if 1
     428
     429#if 0
    354430    {
    355431        QFontDialog fntDlg;
    356432        fntDlg.exec();
    357        
     433
    358434        {
    359435            QFont font("MyCoolFont");
     
    399475    return app.exec();
    400476}
     477
     478#include "widget.moc"
Note: See TracChangeset for help on using the changeset viewer.