久久久久久久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 ()?環形?)
主站蜘蛛池模板: 中文字幕综合 | 中文字幕av中文字幕 | 亚洲欧美日韩精品久久亚洲区 | 亚洲福利av | 99re视频在线免费观看 | 欧美国产视频一区二区 | 91社影院在线观看 | 成人免费视频 | 午夜三级在线观看 | 最大av在线| 日韩中文字幕视频在线 | 国产精品99久久久久久动医院 | 国产成人啪免费观看软件 | 亚洲一区二区久久 | 99re在线视频精品 | 日韩精品无码一区二区三区 | 国产日韩欧美中文 | 一区二区三区不卡视频 | 一区二区精品在线 | 欧美精品首页 | 欧美亚洲视频在线观看 | 在线免费观看黄色av | 亚洲精品一 | 日韩精品在线一区 | www.色五月.com | 成人在线观看免费视频 | 女同久久另类99精品国产 | 欧美在线a | 美女日皮网站 | 久久中文字幕一区 | 久草精品视频 | 国产精品福利视频 | 在线观看中文字幕 | 婷婷色婷婷 | 亚洲毛片在线观看 | 欧美自拍一区 | 中文字幕亚洲精品 | 午夜精品一区二区三区在线播放 | 中文字幕国产视频 | 亚洲国产电影 | 亚洲一卡二卡 |