现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

Q_OBJECT宏的作用

2014-08-15 06:57 工业·编程 ⁄ 共 1694字 ⁄ 字号 暂无评论

The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots

只有加入了Q_OBJECT,你才能使用QT中的signal和slot机制。

比如编写事件接口等程序时,有时会出现如下问题:

class Widget : public QWidget

{

    Q_OBJECT       

public:

    Widget(QWidget *parent = 0);

    ~Widget();

private slots:

    void mountMessage();

    void umountMessage();

    void saveMessage();

    void adjustMessage();

    void touchMessage();

    void about_usMessage();

    void stop_saveMessage();

    void exitMessage();

private:

    bool is_mount();

    bool have_video();

    bool have_udisk();

    bool mount_u();

    Ui_Widget ui;

};

Widget::Widget(QWidget *parent)

    : QWidget(parent)

{

    QMessageBox::StandardButton message;

    QApplication::setStyle(QStyleFactory::create("plastique"));

    QApplication::setPalette(QApplication::style()->standardPalette());

    ui.setupUi(this);

    connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(mountMessage()));    //只有上面定义了Q_OBJECT才有效

    connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(umountMessage()));

    connect(ui.pushButton_3, SIGNAL(clicked()), this, SLOT(saveMessage()));

    connect(ui.pushButton_5, SIGNAL(clicked()), this, SLOT(adjustMessage()));

    connect(ui.pushButton_7, SIGNAL(clicked()), this, SLOT(about_usMessage()));

    connect(ui.pushButton_4, SIGNAL(clicked()), this, SLOT(stop_saveMessage()));

    connect(ui.pushButton_6, SIGNAL(clicked()), this, SLOT(touchMessage()));

    connect(ui.pushButton_8, SIGNAL(clicked()), this, SLOT(exitMessage()));

    mount_u();     //¹ÒÔØUÅ̳ɹ¦£¡

    if( !have_video() ) {   //if have WebCam?

        message = QMessageBox::information(this, tr("ÉãÏñͷδÁ¬½Ó"),

                                         tr("ÉãÏñͷδÁ¬½Ó£¬ÇëÁ¬½ÓÉãÏñÍ·¡£") );

    }

    setWindowFlags(Qt::FramelessWindowHint);

    setWindowTitle(tr("»¶Ó­Ê¹ÓÃÃÔÄã¼à¿ØÆ÷£¡"));

}

给我留言

留言无头像?