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. scope of variables in slots
Forum Updated to NodeBB v4.3 + New Features

scope of variables in slots

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 723 Views 2 Watching
  • 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.
  • T Offline
    T Offline
    tannhaus
    wrote on last edited by tannhaus
    #1

    I've got the following code. In my slot, it says xml is undefined in dataReadyToRead. I've tried everything I can think of including prefixing it with this-> and it says no member xml in MainWindow even though it clearly is!

    This is just a rough draft to get everything working correctly

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "podcastitem.h"
    #include "ui_podcastitem.h"
    #include <QDebug>
    #include <QXmlStreamReader>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        netManager = new QNetworkAccessManager(this);
        netReply = nullptr;
        mDataBuffer = new QByteArray();
        QXmlStreamReader xml;
    
        //Define Network Request
        QNetworkRequest request;
        request.setUrl(QUrl("https://destinationlinux.org/feed/mp3"));
    
        netReply = netManager->get(request);
        connect(netReply, SIGNAL(readyRead()), this, SLOT(dataReadyToRead()));
        connect(netReply, SIGNAL(finished()), this, SLOT(dataReadFinished()));
    
        QString title = "Some new text";
        QPixmap image = QPixmap("C:/Users/tausciam/Desktop/Programming/RoSeS/resources/dl191-mp3-cover.jpg");
        PodCastItem *podcastItem = new PodCastItem(this,title,image);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::dataReadyToRead()
    {
        mDataBuffer->append(netReply->readAll());
        this->xml->addData(mDataBuffer);
        parseXML();
    }
    
    void MainWindow::dataReadFinished()
    {
        if(netReply->error()){
            qDebug() << "Error: " << netReply->errorString();
        }else{
            qDebug() << QString(*mDataBuffer);
        }
    }
    
    void MainWindow::parseXML()
    {
    
    }
    
    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Do you understand the purpose of a class definition vs implementation? The way you've defined xml it only exists in the constructor. this-> does nothing if the target hasn't been defined in the class deifnition.

      T 1 Reply Last reply
      2
      • Kent-DorfmanK Kent-Dorfman

        Do you understand the purpose of a class definition vs implementation? The way you've defined xml it only exists in the constructor. this-> does nothing if the target hasn't been defined in the class deifnition.

        T Offline
        T Offline
        tannhaus
        wrote on last edited by tannhaus
        #3

        @Kent-Dorfman Probably not. I did try it like the others above. If I call it like

        QXmlStreamReader *xml = new QXmlStreamReader;

        it doesn't throw any errors, but I still can't use it in my slot. It still says undefined. If I don't make it a pointer, I get errors

        JonBJ 1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          Your problem is basic C++. If you define QXmlStreamReader xml; in the constructor then it exists nowhere except in the constructor, unless you've defined it in the class definition.

          1 Reply Last reply
          4
          • T tannhaus

            @Kent-Dorfman Probably not. I did try it like the others above. If I call it like

            QXmlStreamReader *xml = new QXmlStreamReader;

            it doesn't throw any errors, but I still can't use it in my slot. It still says undefined. If I don't make it a pointer, I get errors

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @tannhaus
            What @Kent-Dorfman is telling you is: as long as you have QXmlStreamReader xml; inside MainWindow::MainWindow() that variable disappears at the end of that method, and you cannot access the xml variable in any other method. If you want it to persist and be accessible to other methods after the constructor has concluded, you must move it to being a member variable of MainWindow.

            T 1 Reply Last reply
            1
            • Chris KawaC Online
              Chris KawaC Online
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @tannhaus The basic premise of variable scope in C++ is that the } is the killer of variables.

              void SomeClass::foo()
              {
                  if (something)
                  {
                     int variable = 42;
                     // ok to use variable here
                  }
                  // doesn't exist here
              }
              
              //doesn't exist here either
              
              void SomeClass:: bar()
              {
                  // certainly doesn't exist here
              }
              

              Put your variable in the class declaration:

              class SomeClass
              {
                 int variable;
              }; 
              

              For classes you can think of their } as the same as their destructor's }, so you can use it in any method of that class:

              class SomeClass
              {
                 int variable;
              
                 void foo() { // variable is still here  }
                 void bar() { // here too  }
              }; 
              
              // but not here
              
              void someRandomFunc()
              {
                 // and not here
              }
              
              T 1 Reply Last reply
              2
              • JonBJ JonB

                @tannhaus
                What @Kent-Dorfman is telling you is: as long as you have QXmlStreamReader xml; inside MainWindow::MainWindow() that variable disappears at the end of that method, and you cannot access the xml variable in any other method. If you want it to persist and be accessible to other methods after the constructor has concluded, you must move it to being a member variable of MainWindow.

                T Offline
                T Offline
                tannhaus
                wrote on last edited by
                #7

                @JonB Thanks. I thought it was a member variable because I declared it inside of MainWindow, but apparently declaring it in the header instead then using a pointer to mDataBuffer does make it compile without errors.

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @tannhaus The basic premise of variable scope in C++ is that the } is the killer of variables.

                  void SomeClass::foo()
                  {
                      if (something)
                      {
                         int variable = 42;
                         // ok to use variable here
                      }
                      // doesn't exist here
                  }
                  
                  //doesn't exist here either
                  
                  void SomeClass:: bar()
                  {
                      // certainly doesn't exist here
                  }
                  

                  Put your variable in the class declaration:

                  class SomeClass
                  {
                     int variable;
                  }; 
                  

                  For classes you can think of their } as the same as their destructor's }, so you can use it in any method of that class:

                  class SomeClass
                  {
                     int variable;
                  
                     void foo() { // variable is still here  }
                     void bar() { // here too  }
                  }; 
                  
                  // but not here
                  
                  void someRandomFunc()
                  {
                     // and not here
                  }
                  
                  T Offline
                  T Offline
                  tannhaus
                  wrote on last edited by
                  #8

                  @Chris-Kawa Thank you. It's a stupid beginner mistake, but I thought calling a function from mainwindow that is prefixed with MainWindow:: would give it the scope of mainwindow. But, apparently only if you declare it in the header. I can't believe I spent a couple of hours on that and that was the reason, but there it is.

                  Chris KawaC 1 Reply Last reply
                  2
                  • T tannhaus

                    @Chris-Kawa Thank you. It's a stupid beginner mistake, but I thought calling a function from mainwindow that is prefixed with MainWindow:: would give it the scope of mainwindow. But, apparently only if you declare it in the header. I can't believe I spent a couple of hours on that and that was the reason, but there it is.

                    Chris KawaC Online
                    Chris KawaC Online
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @tannhaus Yeah, all class members need to be in the class declaration. This is because the compiler needs to know the size of the class just by reading its declaration. Basically when you include the header the sizeof(YourClass) needs to be calculable.

                    Imagine if you could declare a class member anywhere in its methods. It would be impossible to determine the size of the class. What if the function was never called. Does the variable inside still count to the size of the class? What if you declared a variable in a if statement? Sometimes counts and sometimes not?

                    C++ is statically typed and all types have a constant size calculated at compile time from their type declaration.

                    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