QT学习系列-----3

一些基本的东西(做笔记,因为之前没有学习过C++,现在也是现学现用):

1.

    类的声明一般放在.h文件中,而类成员的实现一般放在相应的cpp文件中。main除外

2.

   关于Qt中的namespace(命名空间)-----遗留问题,例如

QT_BEGIN_NAMESPACE

class Ui_GotoCellDlg
{

.............
      void setupUi(QWidget *GotoCellDlg)
     {
.................
}

}

namespace Ui {
    class GotoCellDlg: public Ui_GotoCellDlg {};
} // namespace Ui

QT_END_NAMESPACE

3. Button的一个性质为checkable... 这个性质害了我一个小时去查找。。。
在Qt assistant中可以查找到button的一些性质,其中对checkable的解释为:

   checkable : bool
      This property holds whether the button is checkable.

      By default, the button is not checkable.

    那么一般情况下button为触发方式(trigger),只有设置为checkable的时候,转为切换状态即toggled

    默认状况下checkable是不选中的,Button默认为触发按钮(trigger button),按下去马上弹起来

     选中checkable后,Button变成切换按钮(toggle button),可以有两种状态:按下/弹起;此时该按钮可以发射 toggled(bool) 信号,与槽函数setVisible(bool) 结合即可用于控件交替显示

  

toggled和trigger区别

  toggle在实物上有开关的意思,这跟我们物理实验用的开关是一回事,两头表示两个状态:合上和断开。于是更准确的译法应该是切换,在两个状态间进行转换。在Qt中,checkable按纽或是图标的槽函数应该用toggled()事件来激活,也是这个道理。

     trigger更有触发的意思。这个单词还有另一个意思就是板机,枪械上用来发射子弹的那种。我们很容易想到板机是没有开/关两种状态的,不能说让它一直关上,一直发射子弹,至少在造词时并没有想到激光武器一说,我想如果针对激光武器,那么要fire的时候应该就不是扣trigger了,而是按toggle。在Qt中,一般的按纽(uncheckable)的激活方式即是triggered()。

4. 关于moc (meta-object system : 元对象编辑器):

    一般只有用到信号和槽时才会用到MOC,因为采用信号和槽是Qt的特性,而C++没有,所以采用了MOC(元对象编译器)把信号和槽部分编译成C++语言。 一下是Qt assistant的解释::

    

The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.

The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

The C++ source file generated by moc must be compiled and linked with the implementation of the class.

If you use qmake to create your makefiles, build rules will be included that call the moc when required, so you will not need to use the moc directly. For more background information on moc, see Why Doesn't Qt Use Templates for Signals and Slots?

用信号signals和槽slots需注意的基本问题是:
(1)、在类class声明中必须加入Q_OBJECT;
(2)、在CPP文件中要把信号signals和槽slots联系起来,即使用connect,例connect( iv, SIGNAL(clicked (QIconViewItem *)), this, SLOT( draw()));

再次强调:只要按上述方式就行了,因为MOC文件的生成和继承都是自动的!!!

posted on 2013-10-22 10:42  展翅的小鸟  阅读(502)  评论(0编辑  收藏  举报

导航