widget.h:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include<QEvent> class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget(); void enterEvent(QEvent *e); void leaveEvent(QEvent *e); void closeEvent(QEvent *e); }; #endif // WIDGET_H
widget.cpp:
#include "widget.h" #include<QPalette> Widget::Widget(QWidget *parent) : QWidget(parent) { } void Widget::enterEvent(QEvent *e) { QPalette s=this->palette(); s.setColor(QPalette::Background,Qt::red); this->setPalette(s); } void Widget::leaveEvent(QEvent *e) { QPalette s=this->palette(); s.setColor(QPalette::Background,Qt::green); this->setPalette(s); } void Widget:: closeEvent(QEvent *e) { e->ignore();// 这样子就关不掉了 } Widget::~Widget() { }
main.cpp:
#include "widget.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Widget w; w.show(); return a.exec(); }