久久久久久久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++ 代碼,以便有效地實現(xiàn)信號和插槽機制.然后附加代碼鏈接到原始類聲明.

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 如何處理你的代碼相沖突.您應(yīng)該在單獨的標題中聲明您的類.

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.

更多關(guān)于 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?

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

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 成人网址在线观看 | 成人免费黄色片 | 欧美中文在线 | 亚洲成av人影片在线观看 | 免费看91 | 国产精品久久久久久久久久妇女 | 手机看黄av免费网址 | 欧美日韩在线视频一区 | 精品久久久久久红码专区 | 亚洲成人观看 | 久久久久久91香蕉国产 | 久久久久久久久91 | 精品亚洲一区二区 | 成人福利电影 | 日韩不卡一二区 | 日韩一区二区三区av | 久久网站免费视频 | 欧美日韩一区二区在线 | 免费一区二区三区 | 日日精品| 久久综合888 | 91精品久久久久久久久中文字幕 | 成人h电影在线观看 | 欧美精品一区二区三区在线 | 国产日韩久久 | 日韩在线不卡视频 | 一区二区在线不卡 | 久久精品亚洲精品国产欧美kt∨ | 欧美激情精品久久久久久 | 国产清纯白嫩初高生视频在线观看 | 台湾a级理论片在线观看 | 日本精品免费在线观看 | 色爱综合网 | 亚洲福利在线观看 | 欧美一区视频 | 最新中文字幕第一页视频 | 国产精品一区二区不卡 | 中文字幕精 | 中文字幕av亚洲精品一部二部 | 国产精品美女一区二区 | 亚洲视频在线免费观看 |