2017-07-24 55 views
0

我无法使用已连接到正确插槽的按钮。moc文件中缺少信号插槽

这里infoPage.cpp文件:

#include "infoPage.h" 

InfoPage::InfoPage(QWidget *parent) 
    : QDialog(parent) 
{ 
    ui.setupUi(this); 
    bool working = false; 
    working = QObject::connect(ui.b_showReleaseNotes, SIGNAL(clicked()), this, SLOT(openReleaseNotes())); 
    working = QObject::connect(ui.ok_button, SIGNAL(clicked()), this, SLOT(closeInfoPage())); 
} 

void InfoPage::openReleaseNotes() 
{ 

    QString prog = "C:\\Windows\\System32\\"; 

    const char *args[3]; 

    //get full path of file to use it in cmd 

    QString currentAppPath = QCoreApplication::applicationDirPath(); 

    QString releaseNotePath = currentAppPath + "release_notes.txt"; 

    args[0] = "notepad.exe"; 
    args[1] = _strdup(releaseNotePath.toAscii()); 
    args[2] = NULL; 

    prog += args[0]; 
    //LOG_DEBUG("starting external program '%s' with file '%s'", prog.toAscii(), docPath.toAscii()); 

    int errorCode = _spawnv(_P_NOWAIT , _strdup(prog.toAscii()),args); 

    if (errorCode == -1) 
    { 
     QMessageBox::information(NULL, "Error:","An error occured while starting process. The call was\n\""+prog+" "+releaseNotePath+"\""); 
    } 


} 

void InfoPage::closeInfoPage() 
{ 
    QString test = "ABC"; 
    this->close(); 
    return; 
} 


InfoPage::~InfoPage() 
{ 

} 

这里的依赖.h文件中:

#ifndef INFOPAGE_H 
#define INFOPAGE_H 

#include <QDialog> 
#include "ui_infoPage.h" 
#include <QProcess> 
#include <process.h> 
#include <QtDebug> 
#include <QtGui/QMessageBox> 

class InfoPage : public QDialog 
{ 

Q_OBJECT 


public: 
    InfoPage(QWidget *parent = 0); 
    ~InfoPage(); 
    void openReleaseNotes(); 
    void closeInfoPage(); 


//private: 
    Ui::InfoPage ui; 

private: 
    //void openReleaseNotes(); 
    QString programPath; 

}; 

#endif // INFOPAGE_H 

至少,商务部文件:

/**************************************************************************** 
** Meta object code from reading C++ file 'infoPage.h' 
** 
** Created: Mon 24. Jul 10:41:32 2017 
**  by: The Qt Meta Object Compiler version 62 (Qt 4.7.0) 
** 
** WARNING! All changes made in this file will be lost! 
*****************************************************************************/ 

#include "../../infoPage.h" 
#if !defined(Q_MOC_OUTPUT_REVISION) 
#error "The header file 'infoPage.h' doesn't include <QObject>." 
#elif Q_MOC_OUTPUT_REVISION != 62 
#error "This file was generated using the moc from 4.7.0. It" 
#error "cannot be used with the include files from this version of Qt." 
#error "(The moc has changed too much.)" 
#endif 

QT_BEGIN_MOC_NAMESPACE 
static const uint qt_meta_data_InfoPage[] = { 

// content: 
     5,  // revision 
     0,  // classname 
     0, 0, // classinfo 
     0, 0, // methods 
     0, 0, // properties 
     0, 0, // enums/sets 
     0, 0, // constructors 
     0,  // flags 
     0,  // signalCount 

     0  // eod 
}; 

static const char qt_meta_stringdata_InfoPage[] = { 
    "InfoPage\0" 
}; 

const QMetaObject InfoPage::staticMetaObject = { 
    { &QDialog::staticMetaObject, qt_meta_stringdata_InfoPage, 
     qt_meta_data_InfoPage, 0 } 
}; 

#ifdef Q_NO_DATA_RELOCATION 
const QMetaObject &InfoPage::getStaticMetaObject() { return staticMetaObject; } 
#endif //Q_NO_DATA_RELOCATION 

const QMetaObject *InfoPage::metaObject() const 
{ 
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject; 
} 

void *InfoPage::qt_metacast(const char *_clname) 
{ 
    if (!_clname) return 0; 
    if (!strcmp(_clname, qt_meta_stringdata_InfoPage)) 
     return static_cast<void*>(const_cast< InfoPage*>(this)); 
    return QDialog::qt_metacast(_clname); 
} 

int InfoPage::qt_metacall(QMetaObject::Call _c, int _id, void **_a) 
{ 
    _id = QDialog::qt_metacall(_c, _id, _a); 
    if (_id < 0) 
     return _id; 
    return _id; 
} 
QT_END_MOC_NAMESPACE 

期间的运行应用程序中,我收到以下消息:

Object::connect: No such slot InfoPage::openReleaseNotes() in infoPage.cpp:8 
Object::connect: (sender name: 'b_showReleaseNotes') 
Object::connect: (receiver name: 'InfoPage') 
Object::connect: No such slot InfoPage::closeInfoPage() in infoPage.cpp:9 
Object::connect: (sender name: 'ok_button') 
Object::connect: (receiver name: 'InfoPage') 

我已经读过其他线程,moc文件负责创建信号和插槽之间的“关系”。

但是moc文件不包含任何信号槽列表。我知道,它必须包含在qt_meta_data_InfoPage []类似的东西:

// slots: signature, parameters, type, tag, flags 
     10, 9, 9, 9, 0x08, 
     54, 50, 9, 9, 0x08, 

    0 //end 

如果我删除建设部文件的内容,新生成的moc文件中包含了相同的代码。

是否有可能手动添加信号/插槽到moc文件?

问候,

萨米尔

+1

您必须声明'openReleaseNo tes()'和'closeInfoPage()'槽,即在类声明中使用'public slots:'说明符而不是'public'。 – vahancho

回答

2

究其原因,缺乏建设部文件槽的是,这两种方法都没有被声明为槽的事实,它们被声明为简单的方法:

​​

相反,它们应该被声明为槽:

public: 
     InfoPage(QWidget *parent = 0); 
     ~InfoPage(); 

public slots: 
     void openReleaseNotes(); 
     void closeInfoPage();