Qt 计算器 学习

1. 可以在别的源代码文件实现头文件中定义的函数;例如:

    两个头文件:button.h, Calculator.h 两个源代码文件:button.cpp,Calculator.cpp

    可以在Calculator.cpp中实现button.h中定义的函数。

2. 判断是哪个按钮按下了:

    Button*clickedButton=qobject_cast<Button*>(sender());

3. qobject_cast

   T qobject_cast(QObject*object)

    Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise returns 0. If object is 0 then it will also return 0.

     The class T must inherit (directly or indirectly) QObject and be declared with the Q_OBJECT macro. A class is considered to inherit itself.

     Example:

QObject *obj = new QTimer;          // QTimer inherits QObject

QTimer *timer = qobject_cast<QTimer *>(obj);
// timer == (QObject *)obj

QAbstractButton *button = qobject_cast<QAbstractButton *>(obj);
// button == 0

4. QString  将一个字符串放到另一字符串的前面

QString & QString::prepend(const QString & str)

QString x = "ship";
QString y = "air";
x.prepend(y);
// x == "airship"

5. Qt头文件中的QT_BEGIN_NAMESPACE

 

QT_BEGIN_NAMESPACE
class QAction;
class QCheckBox;
class QComboBox;
class QGroupBox;
class QLabel;
class QLineEdit;
class QMenu;
class QPushButton;
class QSpinBox;
class QTextEdit;
QT_END_NAMESPACE
//code end


class QAction;
class QCheckBox;


是因为在头文件里面只有这些类的指针申明,并没有真正实例化,在这个头文件对应的cpp文件里面应该会
#include <QAction>
#include <QCheckBox>
...
在cpp文件里面才会正真实例化这些类。
其 实直接在头文件里面#include <QAction> #include<QCheckBox>也是可以的,像它这样做,好像是可以降低各个文件编译时的关联度,不会在改动了一下部分类的时候,引 发其他大量文件的重新编译,在做小工程的时候没什么区别,但是做大了,编译一次需要好几个小时的时候,这样做的优势就显现出来了

 

 

posted @ 2013-01-17 18:41  wiessharling  阅读(410)  评论(0编辑  收藏  举报