C++ (Qt)#ifndef TEST_H#define TEST_H #include <QDialog> class Test: public QDialog{ Q_OBJECT public: Test(QWidget *parent = 0); private: void init(); protected: virtual bool eventFilter(QObject *o, QEvent *e); private: enum State { FocusIn, FocusOut }; int m_rows; int m_columns;}; #endif//TEST_H
C++ (Qt)#include <QtGui>#include "test.h" Test::Test(QWidget *parent) : QDialog(parent), m_rows(5), m_columns(5){ init();} void Test::init(){ QGridLayout *gl = new QGridLayout(this); for (int r = 0; r < m_rows; ++r) { for (int c = 0; c < m_columns; ++c) { QPushButton *pb = new QPushButton(); pb->setProperty("state", FocusOut); const QString fIn = QString::number(r) + QString::number(c) + "in.png"; const QString fOut = QString::number(r) + QString::number(c) + "out.png"; QPixmap fi(100, 100); fi.fill(QColor(qrand()%255, qrand()%255, qrand()%255)); fi.save(fIn); QPixmap fo(100, 100); fo.fill(QColor(qrand()%255, qrand()%255, qrand()%255)); fo.save(fOut); pb->setProperty("focusInImageName", fIn); pb->setProperty("focusOutImageName", fOut); pb->setText("FocusOut"); pb->setIcon(QIcon(fo)); pb->installEventFilter(this); gl->addWidget(pb, r, c); } } setFocus();} bool Test::eventFilter(QObject *o, QEvent *e){ QPushButton *pb = qobject_cast<QPushButton *>(o); if (pb && (e->type() == QEvent::FocusIn || e->type() == QEvent::FocusOut)) { const State state = State(pb->property("state").toInt()); pb->setText(state == FocusIn ? "FocusOut" : "FocusIn"); pb->setProperty("state", state == FocusIn ? FocusOut : FocusIn); const QString fileName = (state == FocusIn ? pb->property("focusOutImageName").toString() : pb->property("focusInImageName").toString()); pb->setIcon(QIcon(fileName)); } return QDialog::eventFilter(o, e);}
C++ (Qt)#include <QApplication>#include "test.h" int main(int argc, char **argv){ QApplication app(argc, argv); Test test; test.show(); return app.exec();}
C++ (Qt)class MyPushButton : public QPushButton { Q_OBJECT public: MyPushButton ( QWidget * parent = 0) : QPushButton(parent) {} ~MyPushButton () {}...... protected: virtual void focusInEvent ( QFocusEvent * event ); virtual void focusOutEvent ( QFocusEvent * event );};
QPushButton#button1 { image: url( image1.png );}QPushButton#button1:focus { image: url( image1_focused.png );}QPushButton#button2 { image: url( image2.png );}QPushButton#button2:focus { image: url( image2_focused.png );}...
QObject::connect(ui->dial, SIGNAL(valueChanged(int)), ui->verticalSlider, SLOT(setValue(int))); QObject::connect(ui->dial, SIGNAL(valueChanged(int)), ui->horizontalSlider, SLOT(setValue(int))); QObject::connect(ui->verticalSlider, SIGNAL(valueChanged(int)), ui->horizontalSlider, SLOT(setValue(int))); QObject::connect(ui->verticalSlider, SIGNAL(valueChanged(int)), ui->dial, SLOT(setValue(int))); QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->verticalSlider, SLOT(setValue(int))); QObject::connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), ui->dial, SLOT(setValue(int))); QObject::connect(ui->dial, SIGNAL(valueChanged(int)), ui->lcdNumber, SLOT(display(int))); QObject::connect(ui->dial, SIGNAL(valueChanged(int)), ui->progressBar, SLOT(setValue(int)));