久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 main.cpp 中定義信號和槽

Define signals and slots inside main.cpp(在 main.cpp 中定義信號和槽)
本文介紹了在 main.cpp 中定義信號和槽的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 main.cpp 中用我自己的類編寫了一個小程序.代碼如下:

I wrote a little program with a my own class within the main.cpp. Here the code:

#include <QApplication>
#include <QPushButton>
#include <QLabel>

class MyWidget : public QWidget {
    //Q_OBJECT
public:
    MyWidget(QWidget* parent = 0);
    QLabel* label;
    QString string;

signals:
public slots:
    void setTextLabel();

};

void MyWidget::setTextLabel() {
    label->setText("Test");
}


MyWidget::MyWidget(QWidget* parent) 
     : QWidget(parent) {

}

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    MyWidget widget;
    widget.show();

    return app.exec();
}

它似乎有效,但不是完全".我的插槽不起作用.我想我必須放 Q_OBJECT.但是,這樣做,我得到了一個錯誤列表,如下所示:

it seems work but not "completely". My slot doens't work. I suppose i have to put Q_OBJECT. BUT, doing so, I got a list of errors, like this:

undefined reference to `vtable for MyWidget'
........................................
collect2: error: ld returned 1 exit status
make: *** [mywidget] Error 1

我能做到嗎?問題出在哪里?

I can I manage that? Where the problem?

推薦答案

Qt 中的信號和槽通過 moc: 元對象編譯器進行管理.基本上,moc 為每個包含 Q_OBJECT 宏的類生成額外的 C++ 代碼,以便有效地實現信號和插槽機制.然后附加代碼鏈接到原始類聲明.

Signals and slots in Qt are managed through the moc: meta object compiler. Basically, the moc generates additional C++ code for each class containing the Q_OBJECT macro in order to implement effectively the signals and slots mechanisms. The additional code is then linked to the original class declaration.

這里的問題是你的類是在 main.cpp 中聲明的:這與 moc 如何處理你的代碼相沖突.您應該在單獨的標題中聲明您的類.

The problem here is that your class is declared in main.cpp: this conflicts with how the moc is working with your code. You should declare your class in a separate header.

更多關于 moc

正如海德所指出的,另一種方法是將 moc 生成的文件包含在您的 cpp 中:為什么在 Qt 源代碼文件的末尾包含.moc"文件很重要?

as hyde pointed, an alternative is to include in your cpp the file generated by the moc: Why is important to include ".moc" file at end of a Qt Source code file?

這篇關于在 main.cpp 中定義信號和槽的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 精品一区二区在线视频 | 成人aaaa | 一区二区视频在线 | 精品影院 | 欧美色偷偷 | 人人草在线视频 | 国产三级黄色 | 午夜在线免费观看 | 日韩一区三区 | 日本欧美久久久久免费播放网 | 美女在线播放 | 欧美视频一区 | 一级片大全| 国产欧美精品一区二区色综合 | 自拍偷拍中文字幕 | 国产精品久久久久久久久久久久午夜片 | 免费看黄色aaaaaa 片 | 日韩伦理在线观看 | 国产伦精品一区二区三区视频网站 | 中文字幕在线观看一区二区三区 | 国产精品网站在线观看 | 在线性视频 | 91蜜桃视频| 激情91 | 欧美在线免费观看视频 | 91午夜精品亚洲一区二区三区 | 日本免费不卡视频 | 久久久久久久久久久久久久久久久久久 | 天天摸天天操 | 国产中文字幕视频 | 韩国精品一区 | 久久久精彩视频 | 五月天婷婷在线观看 | 黄色一级视频 | 国产福利网 | 成人免费毛片网站 | 精品一区在线播放 | 色交视频 | av在线免费网站 | 国产一级二级三级 | 亚洲精品一区二区三区精华液 |