QT - C ++错误:' QWidget :: QWidget(const QWidget&)'在这种情况下是私人的

时间:2012-08-16 21:39:07

标签: c++ qt

我对c ++中的qt有疑问

头文件:

#ifndef TIMER_H
#define TIMER_H
#include <QWidget>
#include <QTimer>
#include <QtGui>
#include <QObject>

class Timer : public QWidget
{
    Q_OBJECT
public:
    Timer(QWidget * parent = 0);
    void setTimer(QString title, QString description, QDate date);
public slots:
    void showWarning() {QString show = tit;
                        QPushButton * thanks = new QPushButton(QObject::tr("Thank you for reminding me!"));
                        show.append("\n");
                        show.append(des);
                        QMessageBox popup;
                        popup.setText(show);
                        popup.setWindowTitle("Calendar : Reminder");
                        popup.setDefaultButton(thanks);
                        popup.exec();
                       }
private:
    QString tit;
    QString des;
};

#endif // TIMER_H

cpp文件:

#include "timer.h"

Timer::Timer(QWidget * parent)
    : QWidget(parent)
{
}

void Timer::setTimer(QString title, QString description, QDate date)
{
    QDateTime now = QDateTime::currentDateTime();
    QDateTime timeoftheaction;
    QTimer *timer=new QTimer;
    tit = title;
    des = description;
    timeoftheaction.setDate(date);
    timeoftheaction.setTime(reminderTime);
    QObject::connect(timer, SIGNAL(timeout()),this,SLOT(showWarning()));
    timer->start(now.secsTo(timeoftheaction)*1000);
}

当我编译时,我得到错误:

  

........ \ QtSDK \ Desktop \ Qt \ 4.8.1 \ mingw \ include / QtGui / qwidget.h:In   复制构造函数'Timer :: Timer(const Timer&amp;)':   .. \ projectOOI / timer.h:9:从'void实例化   QList :: node_construct(QList :: Node *,const T&amp;)[with T =   约定]'   ........ \ QTSDK \桌面\ Qt的\ 4.8.1 \ mingw的\包括/ QtCore / qlist.h:512:
  从'void QList :: append(const T&amp;)实例化[与T =   预约]'.. \ projectOOI / appointoverview.h:10:实例化   这里   ........ \ QTSDK \桌面\ Qt的\ 4.8.1 \ mingw的\包括/ QtGui / qwidget.h:806:   错误:'QWidget :: QWidget(const QWidget&amp;)'是私有的   .. \ projectOOI / timer.h:9:错误:在此上下文中

虽然我继承了QWidget publicaly ...所以我不知道我哪里出错了

1 个答案:

答案 0 :(得分:7)

这是因为QObject(以及继承的QWidget)具有私有拷贝构造函数。

一般来说,你应该通过指针/引用而不是值来传递你的对象,以避免使用复制构造函数。或者,您应该能够定义自己的复制构造函数。

来自Qt文档

  

QObject既没有复制构造函数也没有赋值运算符。   这是设计的。实际上,他们被宣布,但在私人   带有宏Q_DISABLE_COPY()的部分。实际上,所有的Qt类   派生自QObject(直接或间接)使用此宏来声明   他们的复制构造函数和赋值运算符是私有的。该   关于Qt上的身份与价值的讨论中可以找到推理   对象模型页面。

     

主要后果是你应该使用   您可能在哪里指向QObject(或您的QObject子类)   否则很想使用您的QObject子类作为值。对于   例如,没有复制构造函数,就不能使用子类   QObject作为要存储在其中一个容器类中的值。您   必须存储指针。

相关问题