Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems to access private variables of subclass.
Forum Update on Tuesday, May 27th 2025

Problems to access private variables of subclass.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 969 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deleted396
    wrote on 27 Jun 2018, 15:00 last edited by
    #1

    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();
    
    }
    
    J 1 Reply Last reply 27 Jun 2018, 15:06
    0
    • D deleted396
      27 Jun 2018, 15:00

      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();
      
      }
      
      J Offline
      J Offline
      JonB
      wrote on 27 Jun 2018, 15:06 last edited by JonB
      #2

      @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?

      D 1 Reply Last reply 27 Jun 2018, 15:12
      2
      • J JonB
        27 Jun 2018, 15:06

        @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?

        D Offline
        D Offline
        deleted396
        wrote on 27 Jun 2018, 15:12 last edited by deleted396
        #3

        @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?

        J 1 Reply Last reply 27 Jun 2018, 15:25
        0
        • D deleted396
          27 Jun 2018, 15:12

          @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?

          J Offline
          J Offline
          JonB
          wrote on 27 Jun 2018, 15:25 last edited by JonB
          #4

          @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.

          D 1 Reply Last reply 27 Jun 2018, 15:31
          1
          • J JonB
            27 Jun 2018, 15:25

            @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.

            D Offline
            D Offline
            deleted396
            wrote on 27 Jun 2018, 15:31 last edited by
            #5

            @JonB that doesn't solve the problem because the problem is about private variables.

            I will try to do in other way.

            J 1 Reply Last reply 27 Jun 2018, 15:35
            0
            • D deleted396
              27 Jun 2018, 15:31

              @JonB that doesn't solve the problem because the problem is about private variables.

              I will try to do in other way.

              J Offline
              J Offline
              JonB
              wrote on 27 Jun 2018, 15:35 last edited by JonB
              #6

              @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.

              1 Reply Last reply
              2

              1/6

              27 Jun 2018, 15:00

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved