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.
QtWS25 Last Chance

Problems to access private variables of subclass.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 957 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 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();
    
    }
    
    JonBJ 1 Reply Last reply
    0
    • D deleted396

      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();
      
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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
      2
      • JonBJ JonB

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

        JonBJ 1 Reply Last reply
        0
        • D deleted396

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          1
          • JonBJ JonB

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

            JonBJ 1 Reply Last reply
            0
            • D deleted396

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

              I will try to do in other way.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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

              • Login

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