Problems to access private variables of subclass.
-
Hello friends. I have a QWidget in that i have a subclass of qgraphicsScene. In my QGraphicsScene I open a file to write it and i want to close this file with a Push buttom in my QWidget. I am trying to pass the file since my subclass qgraphicsscene to the qwidget, but i receive the next message :
C:\Qt\5.10.0\mingw53_32\include\QtCore\qfile.h:150: error: 'QFile::QFile(const QFile&)' is private
Q_DISABLE_COPY(QFile)I have tried to do it with friends class but i am not be able to. My code:
#include <QFileInfo> #include <QMessageBox> #include <QTextStream> #include <QList> class subQLabel; class subQGraphicsScene; namespace Ui { class interfazMatrizExperto; } class interfazMatrizExperto : public QWidget { Q_OBJECT public: explicit interfazMatrizExperto(const QString rutaImagenFondo, const QString matrizExpertoFile,int nFil,int nCol,QWidget *parent = 0); ~interfazMatrizExperto(); friend class subQGraphicsScene; protected: void resizeEvent(QResizeEvent *evt); private slots: void on_cargarImagenBottom_clicked(); void on_deshacerbottom_clicked(); void on_comebackbottom_clicked(); void showEvent(QShowEvent *); void on_terminarBottom_clicked(); private: Ui::interfazMatrizExperto *ui; subQLabel *labelImage; subQGraphicsScene *scene; QString imagen; int numCol; int numFil; QString rutaFicheroExperto; }; /////////////////////////////////////////////////////////////////////////////////////////////////////// class subQLabel : public QLabel { Q_OBJECT public: subQLabel(QWidget* parent); ~subQLabel(); void mousePressEvent(QMouseEvent *event) override; }; ////////////////////////////////////////////////////////////////////////////////////////////////////////// class subQGraphicsScene : public QGraphicsScene { Q_OBJECT public: subQGraphicsScene(const QString rutaFichero, QWidget* parent); ~subQGraphicsScene(); inline void setNumFil(const int nFil) { numFil = nFil;} inline void setNumCol(const int nCol) { numCol = nCol;} inline void setWidth(const int sizeWidth) { width = sizeWidth;} inline void setHeight(const int sizeHeight) { height = sizeHeight;} inline QFile getFile()const { return file;} protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); private: QFile file; int count=0; int numFil; int numCol; int width; int height; QTextStream stream; }; #endif // INTERFAZMATRIZEXPERTO_H
void interfazMatrizExperto::on_terminarBottom_clicked() { QFile file = scene->getFile(); file.close(); close(); }
-
Hello friends. I have a QWidget in that i have a subclass of qgraphicsScene. In my QGraphicsScene I open a file to write it and i want to close this file with a Push buttom in my QWidget. I am trying to pass the file since my subclass qgraphicsscene to the qwidget, but i receive the next message :
C:\Qt\5.10.0\mingw53_32\include\QtCore\qfile.h:150: error: 'QFile::QFile(const QFile&)' is private
Q_DISABLE_COPY(QFile)I have tried to do it with friends class but i am not be able to. My code:
#include <QFileInfo> #include <QMessageBox> #include <QTextStream> #include <QList> class subQLabel; class subQGraphicsScene; namespace Ui { class interfazMatrizExperto; } class interfazMatrizExperto : public QWidget { Q_OBJECT public: explicit interfazMatrizExperto(const QString rutaImagenFondo, const QString matrizExpertoFile,int nFil,int nCol,QWidget *parent = 0); ~interfazMatrizExperto(); friend class subQGraphicsScene; protected: void resizeEvent(QResizeEvent *evt); private slots: void on_cargarImagenBottom_clicked(); void on_deshacerbottom_clicked(); void on_comebackbottom_clicked(); void showEvent(QShowEvent *); void on_terminarBottom_clicked(); private: Ui::interfazMatrizExperto *ui; subQLabel *labelImage; subQGraphicsScene *scene; QString imagen; int numCol; int numFil; QString rutaFicheroExperto; }; /////////////////////////////////////////////////////////////////////////////////////////////////////// class subQLabel : public QLabel { Q_OBJECT public: subQLabel(QWidget* parent); ~subQLabel(); void mousePressEvent(QMouseEvent *event) override; }; ////////////////////////////////////////////////////////////////////////////////////////////////////////// class subQGraphicsScene : public QGraphicsScene { Q_OBJECT public: subQGraphicsScene(const QString rutaFichero, QWidget* parent); ~subQGraphicsScene(); inline void setNumFil(const int nFil) { numFil = nFil;} inline void setNumCol(const int nCol) { numCol = nCol;} inline void setWidth(const int sizeWidth) { width = sizeWidth;} inline void setHeight(const int sizeHeight) { height = sizeHeight;} inline QFile getFile()const { return file;} protected: void mousePressEvent(QGraphicsSceneMouseEvent *event); private: QFile file; int count=0; int numFil; int numCol; int width; int height; QTextStream stream; }; #endif // INTERFAZMATRIZEXPERTO_H
void interfazMatrizExperto::on_terminarBottom_clicked() { QFile file = scene->getFile(); file.close(); close(); }
@AdrianCruz
We could make this work, if we really wanted. But it doesn't seem like the right approach: you shouldn't need to passQFile
open/closes across widget boundaries. Why don't you re-architect to store, open & close theQFile
within one class, and as a side-effect your problem will go away? -
@AdrianCruz
We could make this work, if we really wanted. But it doesn't seem like the right approach: you shouldn't need to passQFile
open/closes across widget boundaries. Why don't you re-architect to store, open & close theQFile
within one class, and as a side-effect your problem will go away?@JonB Because i am using my class SubQGraphicsScene to store the mouse position event and when you push the buttom is when i finish to gather the position.
Which will it be the best option to do that?
-
@JonB Because i am using my class SubQGraphicsScene to store the mouse position event and when you push the buttom is when i finish to gather the position.
Which will it be the best option to do that?
@AdrianCruz
Given your way of doing it, pass around aQFile &
instead of aQFile
. Something like:inline QFile& getFile()const { return file;} QFile& file = scene->getFile();
But this is not the best way to arrange things. Provide a close-file method in the scene to call from the interface, or better send the scene a signal to do so.
-
@AdrianCruz
Given your way of doing it, pass around aQFile &
instead of aQFile
. Something like:inline QFile& getFile()const { return file;} QFile& file = scene->getFile();
But this is not the best way to arrange things. Provide a close-file method in the scene to call from the interface, or better send the scene a signal to do so.
@JonB that doesn't solve the problem because the problem is about private variables.
I will try to do in other way.
-
@JonB that doesn't solve the problem because the problem is about private variables.
I will try to do in other way.
@AdrianCruz
No, your problem is not about private variables. It's about not being able to copy aQFile
. Did you try what I suggested?
EDIT I did miss out an&
. I have corrected my suggestion. I am not a native C++-er. The principle is: you can pass references (or pointers to)QFile
s around, but you cannot pass a plainQFile
because that would require copy.