QObject是QT所有类的基类

QObject是QT所有类的基类,QObject是Qt Object Model的核心

CODE:

/*
什么是Qt Object Model,主要包括如下的东西
1.信号和槽
2.良好的对象属性,如可查询和很好看,,,~~
  3.有力的事件和事件过滤器
4.国际化字符设计
5.定时器为GUI的事件提供毫秒级的支持
6.很优秀的对象树结构
7.当对象销毁时指针自动设置为0
  8.a dynamic cast that works across library boundaries,不知如何翻译

[Copy to clipboard]

你可以通过connect()连接一个信号到槽,并通过disconnect()来解除这个连接,临时中断用blockSignals(),还可以用 connectNotify()和disconnectNotify()来监听一个连接状态
每个对象有个objectName(),他的类名可以在metaObject() (查阅QMetaObject::className())找到,还可以用inherits()函数来检测他的继承关系
当一个对象销毁时发生信号 destroyed()
更多请参阅 QMetaObject, QPointer, QObjectCleanupHandler, 和Object Trees and Object Ownership.


属性:

1.对象名
objectName : QString
设置函数
QString objectName () const
void setObjectName ( const QString & name )
请参阅 metaObject() 和QMetaObject::className().


成员方法:

1.构造函数
QObject::QObject ( QObject * parent = 0 )
2.虚函数,析构函数
QObject::~QObject () [virtual]
要删除一个类请用deleteLater()代替
请参阅deleteLater()

2.阻塞信号
bool QObject::blockSignals ( bool block )
成功返回true,失败为false
请参阅signalsBlocked()

3.虚保护函数,得到子类的事件
void QObject::childEvent ( QChildEvent * event ) [virtual protected]
请参阅QChildEvent和QEvent类

4.得到子类列表
const QObjectList & QObject::children () const
他在#include<QObject>里面的定义如下:

CODE:
typedef QList<QObject*> QObjectList;
[Copy to clipboard]

first()和last()分别得到子类的第1个和最后一个,同时可以用 QWidget类的 raise()或者lower()来改变,这2个都是槽,注意
请参阅findChild(), findChildren(), parent(), 和setParent()

5.静态连接信号和槽的函数
bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection ) [static]
使用时必须用SIGNAL() 和SLOT()宏指出信号和槽
如:

CODE:
QLabel *label = new QLabel;
QScrollBar *scrollBar = new QScrollBar;
QObject::connect(scroll, SIGNAL(valueChanged(int)),
              label, SLOT(setNum(int)));
[Copy to clipboard]

注意:
a.这里要千万注意,里面的参数不能有变量名,只能有类型,不然不能工作
错误例子:

CODE:
  // WRONG
  QObject::connect(scroll, SIGNAL(valueChanged(int value)),
              label, SLOT(setNum(int value)));
[Copy to clipboard]

b.同样信号可以连接到信号,如:

CODE:
  class MyWidget : public QWidget
  {
    Q_OBJECT

  public:
    MyWidget();

  signals:
    void buttonClicked();

  private:
    QPushButton *myButton;
  };

  MyWidget::MyWidget()
  {
    myButton = new QPushButton(this);
    connect(myButton, SIGNAL(clicked()),
          this, SIGNAL(buttonClicked()));
  }

[Copy to clipboard]

这个例子的意思是,就是MyWidget的构造函数将自己私有成员的信号以另外一种方式传出去,使其有效
c.一个信号可以连到很多信号和槽上,但是一个槽只能连到一个信号上
d.成功返回true,失败false
请参阅disconnect()

6.connect函数2
bool QObject::connect ( const QObject * sender, const char * signal, const char * method, Qt::ConnectionType type = Qt::AutoCompatConnection ) const

7.这个函数检测当有solt连接到这个信号时被调用
void QObject::connectNotify ( const char * signal ) [virtual protected]
请参阅connect() 和disconnectNotify()

8.定义自己的类的行为
void QObject::customEvent ( QEvent * event ) [virtual protected]
请参阅event() 和QEvent类

9.槽,删除此对象
void QObject::deleteLater () [slot]
请参阅destroyed() and QPointer

10.信号,当对象销毁时发生
void QObject::destroyed ( QObject * obj = 0 ) [signal]
请参阅deleteLater() 和QPointer类

11.解除信号和槽的连接
bool QObject::disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method ) [static]
他一般有3种用法
a.解除一个类的所有信号的连接

CODE:
disconnect(myObject, 0, 0, 0);
或者
disconnect(myObject, 0, 0, 0);
[Copy to clipboard]

b.解除一个特定的信号

CODE:
disconnect(myObject, SIGNAL(mySignal()), 0, 0);
或者
myObject->disconnect(SIGNAL(mySignal()));
[Copy to clipboard]

c.解除一个特定的接受对象

CODE:
disconnect(myObject, 0, myReceiver, 0);
或者
myObject->disconnect(myReceiver);
[Copy to clipboard]

0就代表"any signal","any receiving object","any slot in the receiving object",就是所有信号,所有接收对象,一个接收对象的所有槽
注意:第一个参数就是sender不能为0,因为你不能解除所有信号和槽的关系
请参阅connect()

12.disconnect的函数2
bool QObject::disconnect ( const char * signal = 0, const QObject * receiver = 0, const char * method = 0 )

13.disconnect的函数3
bool QObject::disconnect ( const QObject * receiver, const char * method = 0 )

14.解除检测
void QObject::disconnectNotify ( const char * signal ) [virtual protected]
请参阅disconnect() 和connectNotify()

15.显示对象信息,一般用来调试用
void QObject::dumpObjectInfo ()
请参阅dumpObjectTree()

16.显示对象树
void QObject::dumpObjectTree ()
请参阅dumpObjectInfo()

17.检测事件,如果事件是经过验证或者执行过就返回true,否则false
bool QObject::event ( QEvent * e ) [virtual]
请参阅installEventFilter(), timerEvent(), QApplication::sendEvent(), QApplication::postEvent(), 和QWidget::event()

18.事件过滤
bool QObject::eventFilter ( QObject * watched, QEvent * event ) [virtual]
例子:

CODE:
  class MainWindow : public QMainWindow
  {
  public:
    MainWindow();

  protected:
    bool eventFilter(QObject *obj, QEvent *ev);

  private:
    QTextEdit *textEdit;
  };

  MainWindow::MainWindow()
  {
    textEdit = new QTextEdit;
    setCentralWidget(textEdit);

    textEdit->installEventFilter(this);
  }

  bool MainWindow::eventFilter(QObject *obj, QEvent *event)
  {
    if (obj == textEdit) {
        if (event->type() == QEvent::KeyPress) {
          qDebug("Ate key press");
          return true;
        } else {
          return false;
        }
    } else {
        // pass the event on to the parent class
        return QMainWindow::eventFilter(obj, event);
    }
  }
[Copy to clipboard]

在这个例子中只响应textEdit的事件
请参阅installEventFilter()

19.找出子类
T QObject::findChild ( const QString & name = QString() ) const
没有找到返回0,常用的调用
a.找特定的子类

CODE:
QPushButton *button = parentWidget->findChild<QPushButton *>("button1");
[Copy to clipboard]

b.找所有的同一类别子类

CODE:
QListWidget *list = parentWidget->findChild<QListWidget *>();
[Copy to clipboard]

在VC6上,这个函数请用qFindChild()代替~~请参阅findChildren() 和qFindChild()

20.找出子类2,请参阅上例
QList<T> QObject::findChildren ( const QString & name = QString() ) const

21.找出子类3,请参阅上例
QList<T> QObject::findChildren ( const QRegExp & regExp ) const

22.判断继承关系,是true,否false
bool QObject::inherits ( const char * className ) const
例子:

CODE:
  QTimer *timer = new QTimer;       // QTimer inherits QObject
  timer->inherits("QTimer");       // returns true
  timer->inherits("QObject");       // returns true
  timer->inherits("QAbstractButton"); // returns false

  // QLayout inherits QObject and QLayoutItem
  QLayout *layout = new QLayout;
  layout->inherits("QObject");     // returns true
  layout->inherits("QLayoutItem");   // returns false
[Copy to clipboard]

请参阅metaObject() 和qobject_cast()

23.安装事件过滤
void QObject::installEventFilter ( QObject * filterObj )
请参阅 removeEventFilter(), eventFilter(), 和event().

24.判断一个类是不是Widget组件
bool QObject::isWidgetType () const

25.删除定时器
void QObject::killTimer ( int id )

26.得到对象元信息
const QMetaObject * QObject::metaObject () const [virtual]

27.移动到某一线程
void QObject::moveToThread ( QThread * targetThread )
请参阅thread()

28.得到父类
QObject * QObject::parent () const
请参阅setParent() 和children()

29.得到属性值
QVariant QObject::property ( const char * name ) const
请参阅setProperty(), QVariant::isValid(), 和metaObject()

30.得到一个信号的连接数
int QObject::receivers ( const char * signal ) const [protected]

31.删除事件监控
void QObject::removeEventFilter ( QObject * obj )
请参阅installEventFilter(), eventFilter(), 和event()

32.得到信号的发送者
QObject * QObject::sender () const [protected]

33.设置父类
void QObject::setParent ( QObject * parent )
请参阅 parent() 和QWidget::setParent().

34.设置属性值
bool QObject::setProperty ( const char * name, const QVariant & value )
请参阅property() and metaObject()

35.开始一个定时器
int QObject::startTimer ( int interval )
失败返回0,注意这个参数依赖操作系统的时间
请参阅timerEvent(), killTimer(), and QTimer::singleShot().

36.得到些类的线程
QThread * QObject::thread () const
请参阅moveToThread().

37.计时器事件
void QObject::timerEvent ( QTimerEvent * event ) [virtual protected]
请参阅startTimer(), killTimer(), event()

38.翻译函数,关于这个函数的更多描述请参阅本网站国际化变成这一栏目
QString QObject::tr ( const char * sourceText, const char * comment ) [static]
请参阅trUtf8(), QApplication::translate(), and Internationalization with Qt.

39.翻译函数2
QString QObject::trUtf8 ( const char * sourceText, const char * comment ) [static]


QObject宏

1.Q_CLASSINFO ( Name, Value ),他设置的东西可以在 QObject::metaObject()中找到
例子

CODE:
  class MyClass : public QObject
  {
    Q_OBJECT
    Q_CLASSINFO("Author", "Pierre Gendron")
    Q_CLASSINFO("URL", "[url]http://www.my-organization.qc.ca[/url]")

  public:
    ...
  };
[Copy to clipboard]


2.Q_ENUMS ( ... )

CODE:
Q_ENUMS(Option AlignmentFlag EditMode TransformationMode)
[Copy to clipboard]

更多请参阅Qt's Property System

3.Q_FLAGS ( ... )

CODE:
Q_FLAGS(Options Alignment)
[Copy to clipboard]

更多请参阅Qt's Property System

4.Q_INTERFACES ( ... )接口生命,一般用于写插件

CODE:
  class BasicToolsPlugin : public QObject,
                  public BrushInterface,
                  public ShapeInterface,
                  public FilterInterface
  {
    Q_OBJECT
    Q_INTERFACES(BrushInterface ShapeInterface FilterInterface)

  public:
    ...
  };
[Copy to clipboard]


5.Q_OBJECT,略

6.Q_PROPERTY ( ... )
他用来声明一个类的属性,语法如下

CODE:
  Q_PROPERTY(type name
          READ getFunction
          [WRITE setFunction]
          [RESET resetFunction]
          [DESIGNABLE bool]
          [SCRIPTABLE bool]
          [STORED bool])
[Copy to clipboard]

例子:

CODE:
Q_PROPERTY(QString title READ title WRITE setTitle)


FROM: http://blog.csdn.net/robertkun/article/details/5718344

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值