Qt World Summit: Submit your Presentation

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

    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
        explicit interfazMatrizExperto(const QString rutaImagenFondo, const QString matrizExpertoFile,int nFil,int nCol,QWidget *parent = 0);
        friend class subQGraphicsScene;
        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();
        Ui::interfazMatrizExperto *ui;
        subQLabel *labelImage;
        subQGraphicsScene *scene;
        QString imagen;
        int numCol;
        int numFil;
        QString rutaFicheroExperto;
    class subQLabel : public QLabel
        subQLabel(QWidget* parent);
        void mousePressEvent(QMouseEvent *event) override;
    class subQGraphicsScene : public QGraphicsScene
        subQGraphicsScene(const QString rutaFichero, QWidget* parent);
        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;}
        void mousePressEvent(QGraphicsSceneMouseEvent *event);
        QFile file;
        int count=0;
        int numFil;
        int numCol;
        int width;
        int height;
        QTextStream stream;
    void interfazMatrizExperto::on_terminarBottom_clicked()
        QFile file = scene->getFile();

  • @AdrianCruz
    We could make this work, if we really wanted. But it doesn't seem like the right approach: you shouldn't need to pass QFile open/closes across widget boundaries. Why don't you re-architect to store, open & close the QFile 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?

  • @AdrianCruz
    Given your way of doing it, pass around a QFile & instead of a QFile. 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.

  • @AdrianCruz
    No, your problem is not about private variables. It's about not being able to copy a QFile. 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) QFiles around, but you cannot pass a plain QFile because that would require copy.

Log in to reply