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文件?
問候,
薩米爾
您必須聲明'openReleaseNo tes()'和'closeInfoPage()'槽,即在類聲明中使用'public slots:'說明符而不是'public'。 – vahancho