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. How to access variable from one slot to another slot

How to access variable from one slot to another slot

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 569 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.
  • T Offline
    T Offline
    tsdk
    wrote on last edited by
    #1

    Hi,
    I just started working QT and I need a very small help.
    So basically I have 2 pushbuttons.
    In the first slot(when pushbutton 1 is clicked), I'm storing values inside a QVector of the size of which the user enters.
    I need to access this QVector in the second pushbutton slot.

    void QtSortArray::on_generateArray_clicked()
    {int arraySize_value = ui->lineEditArraySize->text().toInt();
    QVector<double> actualVector(arraySize_value);
                for (int i = 0; i < actualVector.size(); i++)
                {
                    actualVector[i] = distribution(*QRandomGenerator::global());
                    ui->inputArray->addItem(QString::number(actualVector[i]));
                }
    
    void QtSortArray::on_pushbutton2_clicked()
    {
    
            qDebug() << actualVector.size() ;
    }
    

    So I want to access the actualVector elements in the on_pushbutton2_clicked slot. Any suggestions on how to do it?

    JonBJ 1 Reply Last reply
    0
    • T tsdk

      Hi,
      I just started working QT and I need a very small help.
      So basically I have 2 pushbuttons.
      In the first slot(when pushbutton 1 is clicked), I'm storing values inside a QVector of the size of which the user enters.
      I need to access this QVector in the second pushbutton slot.

      void QtSortArray::on_generateArray_clicked()
      {int arraySize_value = ui->lineEditArraySize->text().toInt();
      QVector<double> actualVector(arraySize_value);
                  for (int i = 0; i < actualVector.size(); i++)
                  {
                      actualVector[i] = distribution(*QRandomGenerator::global());
                      ui->inputArray->addItem(QString::number(actualVector[i]));
                  }
      
      void QtSortArray::on_pushbutton2_clicked()
      {
      
              qDebug() << actualVector.size() ;
      }
      

      So I want to access the actualVector elements in the on_pushbutton2_clicked slot. Any suggestions on how to do it?

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

      @tsdk
      So make actualVector a member variable of your class QtSortArray, so that it can be accessed by any method of the class, including any slots. That is probably what you want here, though another approach is to access the items you have put into ui->inputArray (you might have to convert them back from string to number), which is available from the slot, instead of in actualVector.

      BTW, don't start out naming your own class as Qt... or even Q... anything. Leave that for Qt's own classes. Pick some other prefix which relates to your own app instead.

      T 1 Reply Last reply
      4
      • JonBJ JonB

        @tsdk
        So make actualVector a member variable of your class QtSortArray, so that it can be accessed by any method of the class, including any slots. That is probably what you want here, though another approach is to access the items you have put into ui->inputArray (you might have to convert them back from string to number), which is available from the slot, instead of in actualVector.

        BTW, don't start out naming your own class as Qt... or even Q... anything. Leave that for Qt's own classes. Pick some other prefix which relates to your own app instead.

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

        @JonB said in How to access variable from one slot to another slot:

        actualVector

        Thanks a lot. I did define actualVector as a member of class QTSortArray

        class QtSortArray : public QMainWindow
        {
            Q_OBJECT
        public:
            QVector<double> *actualVector;
        

        Then I was pointing it to a temporary variable created in one of the slot.

                QVector<double> tempVector(arraySize_value);
        
                ui->inputArray->clear();
                if (arraySize_value > 3)
                {
                    for (int i = 0; i < tempVector.size(); i++)
                    {
                        tempVector[i] = distribution(*QRandomGenerator::global());
                        ui->inputArray->addItem(QString::number(tempVector[i]));
                    }
                    actualVector = &tempVector;
                    qDebug()<< actualVector->size();
        

        But when I use the same in another slot, still it shows size is 0.

        void QtSortArray::on_sortArray_clicked()
        {
        
            {
               qDebug()<< actualVector->size();
            }
        }
        

        Indeed I was thinking of retrieving it from QlistWidget items but I thought if I can just access it directly then it would be more easier. And Thanks, I will name the classes wisely next time.

        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tsdk said in How to access variable from one slot to another slot:

          Then I was pointing it to a temporary variable created in one of the slot.

          You already explain why it does not work. You assign the pointer to a temporary which is not available later on. You have to assign the values (and don't use a pointer - it's not needed here) instead.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          T 1 Reply Last reply
          1
          • T tsdk

            @JonB said in How to access variable from one slot to another slot:

            actualVector

            Thanks a lot. I did define actualVector as a member of class QTSortArray

            class QtSortArray : public QMainWindow
            {
                Q_OBJECT
            public:
                QVector<double> *actualVector;
            

            Then I was pointing it to a temporary variable created in one of the slot.

                    QVector<double> tempVector(arraySize_value);
            
                    ui->inputArray->clear();
                    if (arraySize_value > 3)
                    {
                        for (int i = 0; i < tempVector.size(); i++)
                        {
                            tempVector[i] = distribution(*QRandomGenerator::global());
                            ui->inputArray->addItem(QString::number(tempVector[i]));
                        }
                        actualVector = &tempVector;
                        qDebug()<< actualVector->size();
            

            But when I use the same in another slot, still it shows size is 0.

            void QtSortArray::on_sortArray_clicked()
            {
            
                {
                   qDebug()<< actualVector->size();
                }
            }
            

            Indeed I was thinking of retrieving it from QlistWidget items but I thought if I can just access it directly then it would be more easier. And Thanks, I will name the classes wisely next time.

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

            @tsdk
            Goodness knows what you think you were doing with some "temporary variable" :) Get rid of tempVector, all you need is your actualVector member variable, it can be accessed in both methods in the class.

            1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              @tsdk said in How to access variable from one slot to another slot:

              Then I was pointing it to a temporary variable created in one of the slot.

              You already explain why it does not work. You assign the pointer to a temporary which is not available later on. You have to assign the values (and don't use a pointer - it's not needed here) instead.

              T Offline
              T Offline
              tsdk
              wrote on last edited by
              #6

              @Christian-Ehrlicher said in How to access variable from one slot to another slot:

              You already explain why it does not work. You assign the pointer to a temporary which is not available later on.

              Ahh I got it. Thanks alot.

              1 Reply Last reply
              0

              • Login

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