Reputation: 59
i have a question regarding qt in c++
header file:
#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 file:
#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);
}
When i compile i get the error:
........\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtGui/qwidget.h: In copy constructor 'Timer::Timer(const Timer&)': ..\projectOOI/timer.h:9: instantiated from 'void QList::node_construct(QList::Node*, const T&) [with T = appointment]' ........\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtCore/qlist.h:512:
instantiated from 'void QList::append(const T&) [with T = appointment]' ..\projectOOI/appointoverview.h:10: instantiated from here ........\QtSDK\Desktop\Qt\4.8.1\mingw\include/QtGui/qwidget.h:806: error: 'QWidget::QWidget(const QWidget&)' is private ..\projectOOI/timer.h:9: error: within this context
although i have inherited QWidget publicaly... so i do not get where i go wrong
Upvotes: 2
Views: 8285
Reputation: 17535
This is because QObject (and thus QWidget by inheritance) has a private copy constructor.
Generally speaking you should be pass your object around by pointer/reference instead of by value to avoid using the copy constructor. Alternatively you should be able to define your own copy constructor.
From the Qt documentation
QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.
The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.
Upvotes: 8