如何解决编译错误QObject :: QObject(const QObject&)'是私有的Q_DISABLE_COPY(QObject) [英] How to solve compile error QObject::QObject(const QObject&)' is private Q_DISABLE_COPY(QObject)
问题描述
我正在尝试在QT中创建一个类我这个编译错误
QObject :: QObject(const QObject&)'是私有的Q_DISABLE_COPY(QObject)
感谢任何可以提供帮助的人
这是我班级的标题connect2sql.h
I'm trying to create a class in QT I gt this compile error
QObject::QObject(const QObject&)' is private Q_DISABLE_COPY(QObject)
Thanks to anyone who can help
Here is the header for my class connect2sql.h
#ifndef CONNECT2SQL_H
#define CONNECT2SQL_H
#include <QApplication>
#include <QDebug>
#include <QString>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QtSql/QSqlError>
#include <QMessageBox>
#include "Globals.h"
//class connect2SQL : public QWidget
namespace grillot {
class connect2SQL;
}
class connect2SQL : public QObject
{
Q_OBJECT
public:
connect2SQL(QObject *parent = 0);
void TryConnect();
~connect2SQL();
QString Server;
QString User;
QString Password;
QString Catalog;
bool isConnected;
signals:
public slots:
//void TryConnect();
protected:
private:
connect2SQL *mconnect2SQL;
QString m_Server;
QString m_User;
QString m_Password;
QString m_Catalog;
QSqlDatabase db;
};
#endif // CONNECT2SQL_H
这是实现connect2sql.cpp
Here is the implementation connect2sql.cpp
#include "connect2sql.h"
connect2SQL::connect2SQL(QObject *parent=0) :
QWidget(parent)
{
void connect2SQL::TryConnect()
{
//QSqlDatabase db;
qDebug() << "inside connect2SQL::TryConnect";
db = QSqlDatabase::addDatabase("QMYSQL");
QString m_server;
QString m_user;
QString m_pwd;
db.setHostName(m_Server);
db.setDatabaseName(m_Catalog);
db.setUserName(m_User);
db.setPassword(m_Password);
if (!db.open()) {
isConnected = false;
}
else
isConnected = true
}
connect2SQL::~TryConnect()
{
db.close;
}
这是我尝试使用这个类的代码
Here is code where I try to use this class
connect2SQL mconnect2SQL = new connect2SQL(this);
mconnect2SQL.server="localhost";
mconnect2SQL.user ="root";
mconnect2SQL.pwd ="moonpie".
mconnect2SQL.catalog="mydb";
mconnect2SQL.TryConnect();
if (!mconnect2SQL.isConnected())
{ ....}
当我双击时
关于错误,它转到object.h中的这段代码。
when I double click on the error it goes to this code in object.h
Q_DISABLE_COPY(QObject)
我尝试过从QWidget中获取并获得相同的错误
I've tried inheirting from QWidget and get the same error
推荐答案
这是设计。据我了解,你必须用指针语法实现一个自己的拷贝构造函数。
BTW:你可以通过Google找到这些信息; - )
This is behaviour by design. As I understand it, you must implement a own copy constructor with pointer syntax.
BTW: you could found this information by Google ;-)
你的代码片段中有两个错误(至少):
你的connect2SQL
类声明基于QObject:
There are two errors (at least) in your code snippets:
Yourconnect2SQL
class is declared based onQObject:
class connect2SQL : public QObject
但是在构造函数中你正在调用 QWidget
构造函数:
But in the constructor you are calling the QWidget
constructor:
connect2SQL::connect2SQL(QObject *parent=0) :
QWidget(parent)
{
// ...
};
new
运算符返回分配的地址宾语。因此,这一行应该导致编译器错误,例如从'connect2SQL *'转换为非标量类型'connect2SQL'请求:
The new
operator returns an address to an allocated object. So this line should result in a compiler error like "conversion from 'connect2SQL*' to non-scalar type 'connect2SQL' requested":
connect2SQL mconnect2SQL = new connect2SQL(this);
相反它必须是一个指针:
Instead it must be a pointer:
connect2SQL *mconnect2SQL = new connect2SQL(this);
mconnect2SQL->server="localhost";
// ...
这篇关于如何解决编译错误QObject :: QObject(const QObject&)'是私有的Q_DISABLE_COPY(QObject)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!