简体   繁体   中英

QT-MSVC error C2248

I'm currently working on a gui using QT and MSVC 13 compiler. Whilst setting up a grid I have an error with QWidget

C:\Users\Gaz\Documents\CS22510\simsquare.h:15: error: C2248: 'QWidget::operator =' : cannot access private member declared in class 'QWidget'

in the header file

#ifndef SIMSQUARE_H
#define SIMSQUARE_H

#include <QWidget>


class SimSquare : public QWidget
{
     Q_OBJECT
public:
    SimSquare(QWidget *parent = 0);
    ~SimSquare();
protected:
    void PaintEvent(QPaintEvent *);
};

#endif // SIMSQUARE_H

this is the cpp file

#include "simsquare.h"
#include <QtGui>


SimSquare::SimSquare(QWidget *parent) : QWidget(parent)
{
    QPalette palette(Square::palette());
    palette.setColor(Qt::white);
    setPalette(palette);

}

void SimSqaure::PaintEvent(QPaintEvent *){
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(Qt::white, Qt::SolidPattern);
    painter.drawRect(10,15,80,90);
}

SimSquare::~SimSquare()
{

}

and this is referenced in

#include "simulation.h"
#include "ui_simulation.h"
#include <QVector>

Simulation::Simulation(QWidget *parent) : QMainWindow(parent),
    ui(new Ui::Simulation)  
{    
    simLayout = new SimBoard(this, 8);
    square = new SimSquare(this);
    for(int x =0; x<8; x++){
        for(int y =0; y<8; y++){
            //square = new SimSquare();
            squaresVec.append(&square);
            simLayout->add_widget(&square);
        }
    }
    this->setLayout(simLayout);
    ui->setupUi(this);

}

Simulation::~Simulation()
{
    delete ui;
}

Thank you for any help that you may be able to give.

Okay solved that problem,

#include "simulation.h"
#include "ui_simulation.h"
#include "simsquare.cpp"
#include "simboard.cpp"
#include <QVector>
#include <QDebug>

Simulation::Simulation(QWidget *parent) : QMainWindow(parent),
    simui(new Ui::Simulation)
{    
    setupUI();
    this->show();
}

void Simulation::setupUI(){

    SimBoard simLayout(this,8);
    for(int x =0; x<8; x++){
        for(int y =0; y<8; y++){
            SimSquare square(this);
            squaresVec.append(&square);
            simLayout.add_widget(&square);
        }
    }
    this->setLayout(&simLayout);
    qDebug() << "Setting layout";
    simui->setupUi(this);
    qDebug() << "setu[ UI";

}

Basically because QWidget can't be copied rather than initialising it using = new, you create the object in the header file and then in the cpp call the constructor using Constructor objectname(parameters).

Just incase anyone else is having this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM