简体   繁体   English

QT-MSVC错误C2248

[英]QT-MSVC error C2248

I'm currently working on a gui using QT and MSVC 13 compiler. 我目前正在使用QT和MSVC 13编译器处理gui。 Whilst setting up a grid I have an error with QWidget 在设置网格时,我在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 这是cpp文件

#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). 基本上是因为无法复制QWidget而不是使用= new初始化它,所以在头文件中创建对象,然后在cpp中使用Constructor objectname(parameters)调用构造函数。

Just incase anyone else is having this problem. 只是因为其他人遇到这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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