我正在尝试在我的新创建的类中使用继承自QObject的QTimer。无论我如何尝试,都会出现错误'QObject' is an ambiguous base of 'Recorder'。我尽力避免在我的简单程序中出现歧义,但仍然陷入了困境。以下是我的类结构。
#include "dialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Dialog w;
w.show();
return a.exec();
}
dialog.h:主窗口界面
#ifndef DIALOG_H
#define DIALOG_H
#include "detector.h"
#include <QDialog>
#include <QtCore>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
explicit Dialog(QWidget *parent = 0);
~Dialog();
private:
Ui::Dialog *ui;
Detector myDetector;
detector.h: 检测器窗口用户界面
#ifndef DETECTOR_H
#define DETECTOR_H
#include <QDialog>
#include <QtCore>
#include <QObject>
#include "actualrec.h"
namespace Ui {
class Detector;
}
class Detector : public QDialog
{
Q_OBJECT
public:
explicit Detector(QWidget *parent = 0);
~Detector();
void run();
private:
ActualRec theDetector;
Ui::Detector *ui;
actualrec.h:探测器代码
#ifndef ACTUALREC_H
#define ACTUALREC_H
#include <QtCore>
#include <QObject>
#include <QImage>
#include "recorder.h"
class ActualRec : public QThread
{
public:
ActualRec();
void run();
private:
Recorder theRecorder;
recorder.h: 这是录音器的代码,我想在其中使用我的QTimer。
#ifndef RECORDER_H
#define RECORDER_H
#include <QtCore>
class Recorder : public QThread, public QObject
{
public:
Recorder();
void run();
private:
QTimer* theTimer;
recorder.cpp的构造函数有
*theTimer = new QTimer(this);
输出如下: http://i.imgur.com/Awb6qhd.png 非常感谢您的帮助。
QThread
已经是QObject
,无需重复继承。 - vahanchotheTimer = new QTimer(this);
。去掉星号即可。 - vahancho