讓客戶將CustomRedrawEvents發佈到QCoreApplication。
class CustomRedrawEvent : public QEvent
{
public:
static Type registeredEventType() {
static Type myType
= static_cast<QEvent::Type>(QEvent::registerEventType());
return myType;
}
CustomRedrawEvent() : QEvent(registeredEventType()) {
}
};
void redrawEvent() {
QCoreApplication::postEvent(
QCoreApplication::instance(),
new CustomRedrawEvent());
}
在CoreApplication實例上安裝一個事件,並連接到重繪信號:
class CustomRedrawEventFilter : public QObject
{
Q_OBJECT
public:
CustomRedrawEventFilter(QObject *const parent) : QObject(parent) {
}
signals:
void redraw();
protected:
bool eventFilter(QObject *obj, QEvent *event) {
if(event && (event->type()==CustomRedrawEvent::registeredEventType())) {
emit redraw();
return true;
}
return QObject::eventFilter(obj, event);
}
};
//main()
QMainWindow mainWindow;
QCoreApplication *const coreApp = QCoreApplication::instance();
CustomRedrawEventFilter *const eventFilter(new CustomRedrawEventFilter(coreApp));
coreApp->installEventFilter(eventFilter);
mainWindow.connect(eventFilter, SIGNAL(redraw()), SLOT(update()));
你誤解如何信號/插槽工作。沒有「捕捉」未處理的信號的概念,就好像它們是例外情況一樣。爲什麼你想避免使用靜態方法返回到主窗口?在這種情況下,它更容易,這就是它的存在... – 2010-03-22 20:27:32
謝謝你是我出錯的地方。我雖然信號不僅僅是一個全球性的getApp :: redraw() – user280760 2010-03-22 20:50:06
@Downvoter:不要爲了缺少的知識而冷靜下來。 – 2010-03-22 20:55:09