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 create and use a QVector in other functions or is there an easier way to do this?

How to create and use a QVector in other functions or is there an easier way to do this?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qvectorqt creatorc++c++ qtoptimization
6 Posts 5 Posters 854 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
    Turbotroop
    wrote on 18 Oct 2022, 13:26 last edited by Turbotroop
    #1

    I am using multiple QSpinBoxes that all need to do the exact same thing when interacted with. I am able to get them to do what I want with no issue but I thought I could optimize them by just having them call on 1 function. The only issue was that I would have to have an if statement in order to set a new minimum every time they are interacted with. If I could put them into some sort of array or list then I would be able to easily just make the change based off of one number. Using various previous threads and websites I tried to make a QVector of QSpinboxes in order to use as I previously described. After a lot of trial and errors I've reached a point where I'm not sure why I am getting the error that I am.

    Here is my code:

    MainWindow::~MainWindow()
    {
        QVector <QSpinBox*> boxes(0);
            
            QSpinBox* sqlBox=new QSpinBox(this);
            QSpinBox* perlBox=new QSpinBox(this);
            QSpinBox* rustBox=new QSpinBox(this);
            QSpinBox* cSharpBox=new QSpinBox(this);
            QSpinBox* htmlBox=new QSpinBox(this);
            QSpinBox* cssBox=new QSpinBox(this);
            QSpinBox* javaBox=new QSpinBox(this);
            QSpinBox* pythonBox=new QSpinBox(this);
            QSpinBox* cPlusBox=new QSpinBox(this);
        
            boxes.append(sqlBox);
            boxes.append(perlBox);
            boxes.append(rustBox);
            boxes.append(cSharpBox);
            boxes.append(htmlBox);
            boxes.append(cssBox);
            boxes.append(javaBox);
            boxes.append(pythonBox);
            boxes.append(cPlusBox);
        
        
        
            for (int i = 0; i <= 8; i++)
            {
                boxes[i]->setRange(0, 1000);
                boxes[i]->accessibleName();
                
                if (i < 3)
                {
        
                    boxes[i]->setGeometry(100, 105+(90*i), 71, 21);
                }
                else if (i >= 3 && i < 6)
                {
                    boxes[i]->setGeometry(290, 105+(90*(i%3)), 71, 21);
                }
                else
                {
                    boxes[i]->setGeometry(460, 105+(90*(i%3)), 71, 21);
                }
            }
        
        
        
        delete ui;
    }
    void MainWindow::buyPrgm(short unsigned int input,short unsigned int prgm)
    {
    
    
    
    
    
        int first = 0, second = 0;
        first = revButton();
        if (code >= prgmCostOG[prgm] + pow(prgmCost[prgm], 0.1*prgmAmount[prgm]))
        {
            prgmAmount[prgm] = input;
            prgmCost[prgm] = prgmCostOG[prgm] + pow(prgmCost[prgm], 0.1*prgmAmount[prgm]);
            ui->boxes[prgm]->setMinimum(prgmAmount);
            
        }
        else
        {
    
            ui->noBuy->setText("You don't have enough code!");
    
    
            for(int i = 0; i > 1000; i++)
            {
                second = revButton();
                if ((second - first) > 5)
                {
                    ui->noBuy->setText("");
                    break;
                }
            }
        }
    }
    

    The two errors I'm getting are:
    "error: Member reference type 'QVector<QSpinBox>' (aka 'QList<QSpinBox>') is not a pointer; did you mean to use '.'? (fix available)"

    // I can fix this error by changing
    boxes[prgm]->setMinimum(prgmAmount[prgm]);
    // into
    boxes[prgm].setMinimum(prgmAmount[prgm]);
    

    Even after making this fix I still have this error:
    "error: No member named 'setMinimum' in 'QList<QSpinBox>'"
    I assume this means that it only wants me to use the built in functions that work with QVectors for example "append' or "capacity" despite being able to use the built in functions for QSpinBoxes in the function that they were declared in.

    Once again everything I've learned about QVectors has been through posts that people have already made so I may be making awful mistakes. Any help is welcome.

    J 1 Reply Last reply 18 Oct 2022, 13:40
    0
    • T Turbotroop
      18 Oct 2022, 13:26

      I am using multiple QSpinBoxes that all need to do the exact same thing when interacted with. I am able to get them to do what I want with no issue but I thought I could optimize them by just having them call on 1 function. The only issue was that I would have to have an if statement in order to set a new minimum every time they are interacted with. If I could put them into some sort of array or list then I would be able to easily just make the change based off of one number. Using various previous threads and websites I tried to make a QVector of QSpinboxes in order to use as I previously described. After a lot of trial and errors I've reached a point where I'm not sure why I am getting the error that I am.

      Here is my code:

      MainWindow::~MainWindow()
      {
          QVector <QSpinBox*> boxes(0);
              
              QSpinBox* sqlBox=new QSpinBox(this);
              QSpinBox* perlBox=new QSpinBox(this);
              QSpinBox* rustBox=new QSpinBox(this);
              QSpinBox* cSharpBox=new QSpinBox(this);
              QSpinBox* htmlBox=new QSpinBox(this);
              QSpinBox* cssBox=new QSpinBox(this);
              QSpinBox* javaBox=new QSpinBox(this);
              QSpinBox* pythonBox=new QSpinBox(this);
              QSpinBox* cPlusBox=new QSpinBox(this);
          
              boxes.append(sqlBox);
              boxes.append(perlBox);
              boxes.append(rustBox);
              boxes.append(cSharpBox);
              boxes.append(htmlBox);
              boxes.append(cssBox);
              boxes.append(javaBox);
              boxes.append(pythonBox);
              boxes.append(cPlusBox);
          
          
          
              for (int i = 0; i <= 8; i++)
              {
                  boxes[i]->setRange(0, 1000);
                  boxes[i]->accessibleName();
                  
                  if (i < 3)
                  {
          
                      boxes[i]->setGeometry(100, 105+(90*i), 71, 21);
                  }
                  else if (i >= 3 && i < 6)
                  {
                      boxes[i]->setGeometry(290, 105+(90*(i%3)), 71, 21);
                  }
                  else
                  {
                      boxes[i]->setGeometry(460, 105+(90*(i%3)), 71, 21);
                  }
              }
          
          
          
          delete ui;
      }
      void MainWindow::buyPrgm(short unsigned int input,short unsigned int prgm)
      {
      
      
      
      
      
          int first = 0, second = 0;
          first = revButton();
          if (code >= prgmCostOG[prgm] + pow(prgmCost[prgm], 0.1*prgmAmount[prgm]))
          {
              prgmAmount[prgm] = input;
              prgmCost[prgm] = prgmCostOG[prgm] + pow(prgmCost[prgm], 0.1*prgmAmount[prgm]);
              ui->boxes[prgm]->setMinimum(prgmAmount);
              
          }
          else
          {
      
              ui->noBuy->setText("You don't have enough code!");
      
      
              for(int i = 0; i > 1000; i++)
              {
                  second = revButton();
                  if ((second - first) > 5)
                  {
                      ui->noBuy->setText("");
                      break;
                  }
              }
          }
      }
      

      The two errors I'm getting are:
      "error: Member reference type 'QVector<QSpinBox>' (aka 'QList<QSpinBox>') is not a pointer; did you mean to use '.'? (fix available)"

      // I can fix this error by changing
      boxes[prgm]->setMinimum(prgmAmount[prgm]);
      // into
      boxes[prgm].setMinimum(prgmAmount[prgm]);
      

      Even after making this fix I still have this error:
      "error: No member named 'setMinimum' in 'QList<QSpinBox>'"
      I assume this means that it only wants me to use the built in functions that work with QVectors for example "append' or "capacity" despite being able to use the built in functions for QSpinBoxes in the function that they were declared in.

      Once again everything I've learned about QVectors has been through posts that people have already made so I may be making awful mistakes. Any help is welcome.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 18 Oct 2022, 13:40 last edited by
      #2

      @Turbotroop said in How to create and use a QVector in other functions or is there an easier way to do this?:

      "error: No member named 'setMinimum' in 'QList<QSpinBox>'"

      This error does not match the code you posted: the error meantions QList<QSpinBox>, but in your code boxes is defined as QVector <QSpinBox*>. So what is your real code? I'm anyway confused by your code: why do you initialise boxes in the destructor?!

      Probably the error is coming from "ui->boxes[prgm]->setMinimum(prgmAmount);", but ui->boxes is not the same as boxes in the destructor. So, clean up your code and post it if it still does not work.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 18 Oct 2022, 13:56 last edited by Chris Kawa
        #3

        @Turbotroop On top of what @jsulm pointed you have a local variable boxes in that destructor. It will be destroyed as soon as the function ends. In the buyPrgm function you're referring to something called ui->boxes[prgm]. ui member is a container for stuff created in the designer, but designer can't create arrays or vectors, so I don't know what that is supposed to be. It's not the same boxes variable for sure. Then you have another piece of code without the ui->, so what you posted is gibberish to us.

        When asking question please post the actual code, the actual error and in which line it occurs. When it's about a variable post the declaration and definition of that variable and where it occurs. We don't know your code, we know only what you tell us and we can't make good guesses if you're posting made up stuff that doesn't make sense.

        T 1 Reply Last reply 19 Oct 2022, 13:06
        2
        • C Chris Kawa
          18 Oct 2022, 13:56

          @Turbotroop On top of what @jsulm pointed you have a local variable boxes in that destructor. It will be destroyed as soon as the function ends. In the buyPrgm function you're referring to something called ui->boxes[prgm]. ui member is a container for stuff created in the designer, but designer can't create arrays or vectors, so I don't know what that is supposed to be. It's not the same boxes variable for sure. Then you have another piece of code without the ui->, so what you posted is gibberish to us.

          When asking question please post the actual code, the actual error and in which line it occurs. When it's about a variable post the declaration and definition of that variable and where it occurs. We don't know your code, we know only what you tell us and we can't make good guesses if you're posting made up stuff that doesn't make sense.

          T Offline
          T Offline
          Turbotroop
          wrote on 19 Oct 2022, 13:06 last edited by
          #4

          @Chris-Kawa This is my actual code and errors, previous errors have referred to QVector<QSpinBox> as "QVector<QSpinbox> (aka QList<QSpinBox>)" so I am assuming that the error I have now is somehow related to that. I had been messing with that one line attempting to set the minimum for awhile and had accidentally posted it with ui-> ahead of it, that was the only difference in my code from what I posted.

          Before this I had only created widgets through QT creators interactive drag and drop system, and so I had (and still do not) have any idea of where I am supposed to create the QVector

          M J 2 Replies Last reply 19 Oct 2022, 13:16
          0
          • T Turbotroop
            19 Oct 2022, 13:06

            @Chris-Kawa This is my actual code and errors, previous errors have referred to QVector<QSpinBox> as "QVector<QSpinbox> (aka QList<QSpinBox>)" so I am assuming that the error I have now is somehow related to that. I had been messing with that one line attempting to set the minimum for awhile and had accidentally posted it with ui-> ahead of it, that was the only difference in my code from what I posted.

            Before this I had only created widgets through QT creators interactive drag and drop system, and so I had (and still do not) have any idea of where I am supposed to create the QVector

            M Offline
            M Offline
            mchinand
            wrote on 19 Oct 2022, 13:16 last edited by
            #5

            @Turbotroop said in How to create and use a QVector in other functions or is there an easier way to do this?:

            previous errors have referred to QVector<QSpinBox>

            None of the code you've shown has a QVector<QSpinBox>. Also, as others have mentioned, you're creating the spinboxes in the destructor of your MainWindow, so you're never going to actually see them even if the code compiles successfully.

            1 Reply Last reply
            0
            • T Turbotroop
              19 Oct 2022, 13:06

              @Chris-Kawa This is my actual code and errors, previous errors have referred to QVector<QSpinBox> as "QVector<QSpinbox> (aka QList<QSpinBox>)" so I am assuming that the error I have now is somehow related to that. I had been messing with that one line attempting to set the minimum for awhile and had accidentally posted it with ui-> ahead of it, that was the only difference in my code from what I posted.

              Before this I had only created widgets through QT creators interactive drag and drop system, and so I had (and still do not) have any idea of where I am supposed to create the QVector

              J Offline
              J Offline
              JonB
              wrote on 19 Oct 2022, 13:16 last edited by
              #6

              @Turbotroop
              OK, so:

              • Is the code pasted now currently what you have, exactly?
              • What error do you currently have?
              • Have you acted on the previous comments?

              For example: although it won't generate a compilation error, it does not make any sense to set up all these boxes or arrays in the main window destructor, does it? Why not in, say, the constructor instead?

              1 Reply Last reply
              0

              3/6

              18 Oct 2022, 13:56

              • Login

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