gpt4 book ai didi

c++ - QT - C++ 错误 : 'QWidget::QWidget(const QWidget&)' is private within this context

转载 作者:行者123 更新时间:2023-11-30 00:37:59 27 4
gpt4 key购买 nike

我有一个关于 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 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

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

最佳答案

这是因为 QObject(以及继承的 QWidget)有一个私有(private)的复制构造函数。

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

来自Qt文档

QObject has neither a copy constructor nor an assignment operator.This is by design. Actually, they are declared, but in a privatesection with the macro Q_DISABLE_COPY(). In fact, all Qt classesderived from QObject (direct or indirect) use this macro to declaretheir copy constructor and assignment operator to be private. Thereasoning is found in the discussion on Identity vs Value on the QtObject Model page.

The main consequence is that you should usepointers to QObject (or to your QObject subclass) where you mightotherwise be tempted to use your QObject subclass as a value. Forexample, without a copy constructor, you can't use a subclass ofQObject as the value to be stored in one of the container classes. Youmust store pointers.

关于c++ - QT - C++ 错误 : 'QWidget::QWidget(const QWidget&)' is private within this context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11995821/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com