Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
#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


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()) 

{ ....}

when I double click on the error it goes to this code in object.h

Q_DISABLE_COPY(QObject)


I've tried inheirting from QWidget and get the same error
Posted

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 ;-)
 
Share this answer
 
There are two errors (at least) in your code snippets:

Your connect2SQL class is declared based on QObject:
C++
class connect2SQL : public QObject

But in the constructor you are calling the QWidget constructor:
C++
connect2SQL::connect2SQL(QObject *parent=0) :
QWidget(parent)
{
// ...
};


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";
// ...
 
Share this answer
 
Comments
JohnnyG62 15-Dec-15 16:33pm    
Thanks Jochen, I am new to C++ & QT. This is the first time I've written a class. I thought there could be more other errors. This worked perfectly ! I can't believe I missed calling the QWidget constructor instead of QObject. .This defintely help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900