Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
giblit
Where I said that ok button add widgets justt put that stuffvwhere u dynamically create then keep the value part then.cout it or w.e u doing to get the printed values
I misstead earlier on my phone
The function that outputs the user input values can't use the spinBoxesValue variable..
This is getdbInfo():
Code:
spinBoxes << spinBox;
QList<int> spinBoxesValue;
for(int i = 0; i<spinBoxes.size();i++){
ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
spinBoxesValue << spinBoxes[i]->value();
}
And this is to print values aka okButton function
Code:
spinboxValues << spinBoxesValue;
for(int i = 0; i<numberofpeople; i++){
qDebug() << spinboxValues[i];
}
I have included everything you said and even passed spinBoxesValue as a variable.
Re: use value from dynamically created widget in a slot
declare the QList<int> spinBoxesValue outside of the getdbinfo function sorry. declare it above it by the headers so it is global.
Quote:
Originally Posted by
Cyrebo
The function that outputs the user input values can't use the spinBoxesValue variable..
This is getdbInfo():
Code:
spinBoxes << spinBox;
QList<int> spinBoxesValue;
for(int i = 0; i<spinBoxes.size();i++){
ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
spinBoxesValue << spinBoxes[i]->value();
}
And this is to print values aka okButton function
Code:
spinboxValues << spinBoxesValue;
for(int i = 0; i<numberofpeople; i++){
qDebug() << spinboxValues[i];
}
I have included everything you said and even passed spinBoxesValue as a variable.
oh and btw you have it sending the values to it twice. you could just remove the spinBoxValues << spinBoxesValue from the okButton function considering first of all it would add the items twice and secondly it isnt even declared (box)
Added after 11 minutes:
Try this:
Code:
//header file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSpinBox>
#include <QPushButton>
{
Q_OBJECT
public:
MainWindow();
private slots:
void okButton();
private:
QList<QSpinBox*> spinBoxes;
};
#endif // MAINWINDOW_H
//main c++
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
//mainwindow c++
#include "mainwindow.h"
#include <QGridLayout>
#include <iostream>
using namespace std;
QList<int> values;
MainWindow::MainWindow(){
button->setText("Ok");
for(int i = 0; i<5; i++){
spinBoxes << spinbox;
layout->addWidget(spinBoxes[i], i+1, 0);
}
layout->addWidget(button, 0, 0);
widget->setLayout(layout);
setCentralWidget(widget);
connect(button,SIGNAL(clicked()),
this,SLOT(okButton()));
}
void MainWindow::okButton(){
for(int i = 0; i<spinBoxes.size();i++){
values << spinBoxes[i]->value();
cout << values[i] << endl;
}
}
with the inputed values of 4, 10, 11, 12, 8 you will get a printed value of
Re: use value from dynamically created widget in a slot
hmmmmm wonder what I'm doing wrong but I'm getting the errr
Quote:
(.bss+0x10):-1: error: multiple definition of `spinBoxes'
Followed by:
Quote:
(.bss+0x8):-1: error: first defined here
My code is sightly different but in essence is the same as yours except the way I get the number of spinboxes to add to the widget might be affecting the definition of it.
Code:
void MainWindow::dbinfo()
{
if (qry.exec("SELECT name FROM customers"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
if(qry.isValid())
{
QString cust
= qry.
record().
value(0).
toString();
spinBoxes << spinBox;
label->setGeometry(0,0,80,41);
ui->verticalLayout->addWidget(label);
for(int i = 0; i<spinBoxes.size();i++){
ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
}
}
}
}
else
{
qDebug() << qry.lastError();
}
}
void MainWindow::on_pushButton_clicked()
{
//int numberofitems = ui->verticalLayout->count();
//div(numberofitems,2);
for(int i = 0; i<spinBoxes.size(); i++){
values << spinBoxes[i]->value();
qDebug() << values[i];
}
}
Re: use value from dynamically created widget in a slot
you probably have this on your header:
Code:
QList<QSpinBox*> spinBoxes;
instead of
Code:
QList<QSpinBox*>spinBoxes;
or you declared spinBoxes as something else like QString or something somewhere else in any of your c++/header files.
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Cyrebo
hmmmmm wonder what I'm doing wrong but I'm getting the errr
Declaring a variable in a header file as advised by your interlocutor is wrong. If you do that and include the header file in multiple files, you'll get the variable declared in every compilation unit including the file thus getting multiple definition errors. Again, nothing different between C and C++.
If you want a variable exposed to different compilation units then the proper way to do it in C++ is to make it a class member variable and either pass a pointer to an instance of that class to all places where you want to access the variable or make that class a singleton (google if you don't know what it means).
If you really can't do it properly, you can go the crude C way and declare a global extern variable in a header file and define that variable in one of the implementation files (I'm sure you know that mechanism from C -- it works the same way in C++).
However guys, having read your conversation, you are heavily overcomplicating things here. Instead of using a bunch of variables stuck here and there, you should encapsulate everything (the data and the code) in a reusable class.
Re: use value from dynamically created widget in a slot
It worked!Tthe layout is not correct but it did get the correct values from the spinboxes.
I'll work on putting this into a class, like wysota said, after I sort out the layout. I think it's the way in which the widget is added to the layout, should be outside the for loop or something.
Thanks, really appreciate the help.
Code:
void MainWindow::dbinfo()
{
if (qry.exec("SELECT name FROM customer"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
if(qry.isValid())
{
QString cust
= qry.
record().
value(0).
toString();
spinBoxes << spinbox;
label->setGeometry(0,0,80,41);
for(int i = 0; i<spinBoxes.size();i++){
ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
}
}
}
}
Re: use value from dynamically created widget in a slot
Why are You adding every labels and spinboxes for each customer name ? You have for loop in while loop. For 3 customers You will put first label on screen 3 times, second label 2 times etc. And next Yours mail will be titled : why my code crashed. As wysota (and me also) said : first go to C++ school then go back to Qt.
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Lesiok
Why are You adding every labels and spinboxes for each customer name ? You have for loop in while loop. For 3 customers You will put first label on screen 3 times, second label 2 times etc. And next Yours mail will be titled : why my code crashed. As wysota (and me also) said : first go to C++ school then go back to Qt.
I've actually fixed the issue now, And that wasn't exactly my code was it? I just had to adjust it slightly..
Re: use value from dynamically created widget in a slot
To use someone else's code you need to first understand it. "Copy and paste" is not a good tool for programmers.
Re: use value from dynamically created widget in a slotn't
well I wasn't trying to give him code but an example of how to get values of dynamicly created spinboxes
Re: use value from dynamically created widget in a slotn't
Hi again I know its been a while because I had to do some other things but I still don't know how to use the values created from the dynamic widgets. This is the updated version of my code:
Code:
{
ui->setupUi(this);
fb = new FrmBuilder(ui);
if(checkFile.isWritable())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
fb->buildFrm();
}
MainWindow::~MainWindow()
{
delete fb;
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
fb->display();
}
class FrmBuilder:
Code:
QList<int> values;
FrmBuilder::FrmBuilder(Ui::MainWindow *ui)
{
this->myUi = ui;
}
void FrmBuilder::buildFrm()
{
if (qry.exec("SELECT name FROM persons"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
QString person
= qry.
record().
value(0).
toString();
spinBoxes << spinbox;
label->setGeometry(0,0,80,41);
for(int i = 0; i<spinBoxes.size();i++){
myUi->verticalLayout->addWidget(label);
myUi->verticalLayout_2->addWidget(spinBoxes[i],0,0);
}
}
}
else
{
qDebug() << qry.lastError();
}
qry.clear();
}
void FrmBuilder::display()
{
for(int i = 0; i<spinBoxes.size(); i++){
values << spinBoxes[i]->value();
qDebug() << values[i];
}
}
Re: use value from dynamically created widget in a slotn't
Re: use value from dynamically created widget in a slotn't
Re: use value from dynamically created widget in a slotn't
Cyrebo, this forum is really not a C++ kindergarden. The issue you have is completely unrelated to Qt, it's strictly your inability to manage your own objects -- be it widgets or fruits. Unless you spend some time on learning concepts of the programming language (C++) you are using, you'll be struggling with even the simplest things. Storing objects in some container like a list and accessing items in that container is really no rocket science.
Re: use value from dynamically created widget in a slotn't
Re: use value from dynamically created widget in a slotn't
Thread SOLVED. Had the answer all along, thanks.