• 7760阅读
  • 1回复

关于自定义事件的问题[已经解决] [复制链接]

上一主题 下一主题
离线vaqeteart
 

只看楼主 正序阅读 楼主  发表于: 2009-11-24
— 本帖被 XChinux 执行加亮操作(2009-11-24) —
我的目的是:
点击一个按钮将会建立一个自己定义的事件然后将这个事件发送出去并处理。
但是问题是:
可以点击按钮,第一次可以处理,但是第二次点击按钮之后,就会程序崩溃了,这是怎么回事呢?
代码如下:

  1. //main.cpp
  2. /*程序功能:自定义一个事件并且发送自定义的事件
  3. * 点击一个按钮将会建立一个自己定义的事件然后将这个事件发送出去并处理
  4. * */
  5. #include <QApplication>
  6. #include "myCallback.h"
  7. int main(int argc, char *argv[])
  8. {
  9.     QApplication a(argc,argv);
  10.     MyCallback my;
  11.     my.show();
  12.     a.exec();
  13.     return 0;
  14. }


  1. //myCallback.h
  2. #ifndef __MYCALLBACK_H
  3. #define __MYCALLBACK_H
  4. #include <QWidget>
  5. #include <QEvent>
  6. class QString;
  7. class QPushButton;
  8. class UserEvent: public QEvent
  9. {//用户自定义事件类
  10.     public:
  11.     //这里的事件id要大于QEvent::User,小于QEvent::MaxUser
  12.     //registerEventType安全注册一个事件,防止重复,其参数是一个提示仅在可用时用,可为-1.
  13.     //它返回事件id,必须强制转换为QEvent::Type
  14.     UserEvent(QString s):QEvent((QEvent::Type)registerEventType(QEvent::User+1)), sz(s){;}
  15.     QString str() const {return sz;}
  16.     private:
  17.     QString sz;
  18. };
  19. class MyCallback:public QWidget
  20. {
  21.     Q_OBJECT
  22.     public:
  23.     //点击按钮的槽函数用来建立和提交自定义事件
  24.         MyCallback(QWidget *parent = 0);
  25.     protected:
  26.     //这个函数会处理收到的自定义事件
  27.     //当发生自定义的事件的时候自动会调用这个函数
  28.         void customEvent(QEvent *e);
  29.     protected slots:
  30.         void callback(void);
  31.     private:
  32.     QPushButton *button;
  33.     UserEvent *se;
  34. };
  35. #endif


  1. //myCallback.cpp
  2. #include "myCallback.h"
  3. #include <QPushButton>
  4. #include <QApplication>
  5. #include <QThread>
  6. #include <iostream>
  7. using std::cout;
  8. using std::endl;
  9. MyCallback::MyCallback(QWidget *parent):QWidget(parent)
  10. {
  11.     button = new QPushButton(this);
  12.     se = NULL;
  13.     QObject::connect(button, SIGNAL(clicked()), this, SLOT(callback()));
  14. }
  15. void MyCallback::callback(void)
  16. {//点击按钮
  17.     //建立自定义事件
  18.     if(se == NULL)
  19.     {
  20.         se = new UserEvent("my custom event");
  21.     }
  22.     //提交自定义事件
  23.     QApplication::postEvent(this,se);
  24.     //QThread::postEvent(this,se);
  25. }
  26. void MyCallback::customEvent(QEvent *e)
  27. {
  28.     if(e->type() == QEvent::User+1)
  29.     {
  30.         UserEvent *re = (UserEvent*)e;
  31.         cout<<re->str().toLocal8Bit().data()<<endl;
  32.     }
  33. }
[ 此帖被vaqeteart在2009-12-07 10:58重新编辑 ]
离线vaqeteart

只看该作者 1楼 发表于: 2009-11-24
找到原因了,用sendEvent.
http://qt.nokia.com/doc/4.5/qcoreapplication.html#sendEvent
Sends event event directly to receiver receiver, using the notify() function. Returns the value that was returned from the event handler.
The event is not deleted when the event has been sent. The normal approach is to create the event on the stack.

http://qt.nokia.com/doc/4.5/qcoreapplication.html#postEvent
The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted. It is not safe to modify or delete the event after it has been posted.
快速回复
限100 字节
 
上一个 下一个