How to create and use a QVector in other functions or is there an easier way to do this?
-
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.
-
@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.
-
@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 thebuyPrgm
function you're referring to something calledui->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 theui->
, 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.
-
@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
-
@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. -
@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?