PDA

View Full Version : use value from dynamically created widget in a slot



Cyrebo
30th March 2013, 15:38
Hi, I don't how to use the values from spinboxes that are dynamically created at runtime. I want to get the current value in the spinboxes when I click a push button.

Here's what I've done so far:


QFileInfo checkFile(Path_to_DB);

if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}

int value;
QSqlQuery qry;

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();

//create widgets
QLabel *label = new QLabel(QString(cust));
QSpinBox *spinbox = new QSpinBox;
spinbox->setMaximum(3);
label->setGeometry(0,0,80,41);

//add to VBoxLayout
ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinbox);
value = spinbox->value(); // value is always going to be zero here
qDebug() << value;
}
}
}
else
{
qDebug() << qry.lastError();
}
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{
//get spinbox values
if(!db.isOpen()){
qDebug() << "No connection to db";
return;
}
int value2;
value2 = spinbox->value2();
qDebug() << value2;

//save values to db
............


Thanks in advance!

--

Lesiok
30th March 2013, 18:15
Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).

Cyrebo
30th March 2013, 18:58
Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).
Thanks for the quick reply. When I do that I get the following error:
/mainwindow.cpp:68: error: 'spinbox' cannot appear in a constant-expression

This is what I did:


QString cust = qry.record().value(0).toString();
QLabel *label = new QLabel(QString(cust));
QSpinBox *spinbox = new QSpinBox(this);
QList<*spinbox>;

wysota
30th March 2013, 19:27
It should be QList<QSpinBox*>, however defining the list as a local variable will not do you any good. You need to be able to access that variable from scopes other than this function.

Cyrebo
30th March 2013, 19:38
It should be QList<QSpinBox*>, however defining the list as a local variable will not do you any good. You need to be able to access that variable from scopes other than this function.

How should it be declared to be accessed from various scopes?

wysota
30th March 2013, 19:44
How should it be declared to be accessed from various scopes?

This is really no place to teach you C++. The issue you have is completely unrelated to Qt. You can make the variable a class member or a global one but I'm afraid that without proper C++ skills you'll get stuck again. I suggest you read a good book on C++ or do a couple of C++ tutorials.

Cyrebo
30th March 2013, 19:55
I'm really new to c++ actually, so im not use to classes like you have in java also. I'm pretty good with just ordinary c though and in C to declare something to be accessed from anywhere you declare it at the top of your program. Then it may be accessed by "functions" rather than classes. It's good that it's not qt related so I just need to find out how to declare stuff globally in c++.

But can you tell me how to use functions in qt? I wanted to make the whole creating layout part a function.

wysota
30th March 2013, 20:00
Qt is exactly the same as regular C++. Once you learn C++, you'll know how to use functions in Qt as well. Using functions in C++ is exactly the same as in C, by the way.

Cyrebo
30th March 2013, 20:20
Well actually, I can use functions in the main.cpp, I just need to know how to use them in mainwindow class please.

wysota
30th March 2013, 21:03
I already said, functions in C and C++ work the same way, with respect to scopes (such as classes).

Cyrebo
30th March 2013, 22:37
Yes I understand, I can use it in c++ in the main.cpp, what I want to know is how to use in mainwindow.cpp. I put in the exact same code and it says "expected a declaration" when its supposed to be a function...

giblit
30th March 2013, 22:43
Well actually, I can use functions in the main.cpp, I just need to know how to use them in mainwindow class please.
if you create a mew qt gui file it should do that for you automatically other wise you can do it manually->

use a header like this:


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>


class MainWindow: public QMainWindow
{
Q_OBJECT

public:
MainWindow();
~MainWindow();


private slots:
void functionSlot();

private:
void function();

//QWidgetType *QWidgetName.....
//ex:
QPushButton *pushButton;

};

#endif

then on the mainWindow cpp file


#include <QtGui>
#include "mainwindow.h"


MainWindow::MainWindow(){
pushButton = newQPushButton;
function();
}
void MainWindow::function(){
pusButton->setText("Button");
//do something
}
and on the main.cpp file


#include <QtGui/QApplication>
#inlucde "mainwindow.h"

int main(int argc, char *argv[]){
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Cyrebo
30th March 2013, 23:00
Thanks man, I've been looking for something like that all day but I still get this error on runtime..

-build-desktop-Qt_4_8_3_in_PATH__System__Release/moc_mainwindow.cpp:-1: error: undefined reference to `MainWindow::dbinfoSlot()'

giblit
30th March 2013, 23:08
because you need to put void before MainWindow more than likely.
like this:

void MainWindow::somethingSlotIForgotName(){
//do something
}

Edit:: if it is a private/slot you have to declare it as void/unsigned/int/ect
if it is the public function (the main one for the class) you do not have to declare it.

Cyrebo
30th March 2013, 23:14
It was the name dbInfoSlot(), it was unused..I took that line out of the private slots and it worked but the program runs the function twice or something because it creates two instances of...I'll just show you



void MainWindow::dbinfo()
{
int value;
QSqlQuery qry;

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();
QLabel *label = new QLabel(QString(cust));
QSpinBox *spinbox = new QSpinBox(this);

label->setGeometry(0,0,80,41);

ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinbox);
value = spinbox->value();
qDebug() << value;
}
}
}
else
{
qDebug() << qry.lastError();
}
}


I only want to create a label and spinbox for each customer but when I enter only 1 customer it creates two at run time...

giblit
30th March 2013, 23:19
do you have something else calling the dbinfo function? thats the onlything I can think of im still a beginner at qt/c++ haha
oh and the reason I put slot in the example was because if in your function you want you can connect in t he main fucntion like this:

MainWindow::MainWindow(){
connect(pushButton,SIGNAL(clicked()),
this,SLOT(functionSlot()));
}
MainWindow::functionSlot(){
//do something
}

Cyrebo
30th March 2013, 23:29
:D yes I called it twice, first with dbInfo(); then MainWindow::dbInfo();...

Do you know how to get the values from the spinbox for each person?

giblit
30th March 2013, 23:37
you could possibly send the values from the spinbox into a list of ints like
QList<int> spinboxValues;
spinboxValues << value;
for(int i = 0; i<numberofpeople; i++){
cout << spinBoxValues[i] << endl;
}

Cyrebo
31st March 2013, 00:19
So this action is triggered when i click another button which an ok button slot to get the figures. How may I use all the variables in my function. I know in c you just type in the variable function name but that doesn't work on c++...

giblit
31st March 2013, 00:28
So this action is triggered when i click another button which an ok button slot to get the figures. How may I use all the variables in my function. I know in c you just type in the variable function name but that doesn't work on c++...
do you mean use the variables form the other function? if this is what you mean you could try this:

QList<double> spinboxValues;
double values;
mainWindow::dbinfo(){
//get values from spin box
}
void MainWindow::okbuttonslot(){
spinBoxValue << value;
}

that would mean when you press the button the value from your spinbox would be appended to the QList spinbox but you will probably have to use a boolen or something if you use this method otherwise each time you press the button it will append it to the end

wysota
31st March 2013, 00:39
do you mean use the variables form the other function? if this is what you mean you could try this:

QList<double> spinboxValues;
double values;
mainWindow::dbinfo(){
//get values from spin box
}
void MainWindow::okbuttonslot(){
spinBoxValue << value;
}

that would mean when you press the button the value from your spinbox would be appended to the QList spinbox but you will probably have to use a boolen or something if you use this method otherwise each time you press the button it will append it to the end

The thing is he/she doesn't know how to get the "value". Which he/she supposedly knows how to do in C (which works the same way in C++ for everyone else but not for him/her).

Cyrebo
31st March 2013, 00:51
It sort of worked but I think its printing out the memory address or something because its all hex...

Here is the output and this is for 3 inputs:


7.95446e-322
1.1117e-316
6.91643e-310
2.24083e-311
211
8.91041e-317

giblit
31st March 2013, 00:54
thats not hex...those are just realllllllllllllly small numbers think there is something wrong what is the code you are using to get the value?

Cyrebo
31st March 2013, 01:02
The thing is he/she doesn't know how to get the "value". Which he/she supposedly knows how to do in C (which works the same way in C++ for everyone else but not for him/her).

First of all, C isn't a GUI programming language, it is a I/O lower level language so you don't do things like sql, spinboxes and widgets its more programming like microchips...
Second of all, as you can see I'm still a novice to qt/c++ and nearly every programming language in the "C-Family" works in the same way, don't mean you can use em all the same way.

Personally I think qt/c++ is the best tool for GUI Programming period. So I'm trying to learn by practising and doing something worthwhile at the same time. Excuse me for seeking help in the Qt/Programming forum for help...

Back to the thread we're actually meant to be discussing not my programming knowledge of the C-Family...If you know how to get the values please tell me or let me work.

Thank you

--


thats not hex...those are just realllllllllllllly small numbers think there is something wrong what is the code you are using to get the value?



int numberofpeople = ui->verticalLayout->count();
div(numberofpeople,2);

for(int i = 0; i<numberofpeople; i++){
qDebug() << spinboxValues[i];
}

giblit
31st March 2013, 01:07
well the number of people is how many people input values for example if you have two ppl that input values the number of people is two. im not sure what that verticalLayout is.
and what are you using to actually get the value from the spinbox? are you using this on the okButtonslot?
value = ui->spinboxName->value()

Cyrebo
31st March 2013, 01:13
well the number of people is how many people input values for example if you have two ppl that input values the number of people is two. im not sure what that verticalLayout is.
and what are you using to actually get the value from the spinbox? are you using this on the okButtonslot?
value = ui->spinboxName->value()

Yes I'm getting the values from when the okButton is clicked. The VerticalLayout is used to layout the names+spinboxes in as the name says a vertical layout. I'm dynamically adding the labels and spinboxes to it by the amount of people in the database at the time of creation. I half the value of the numberofpeoples because I have a label+spinbox for each person so thats just math. Whether I'm doing it the right way is what I'm not sure about.

wysota
31st March 2013, 03:27
First of all, C isn't a GUI programming language
C and C++ are both general purpose languages. You can implement a graphical user interface as well using C (e.g. using GTK+ or WinAPI) as you can with C++ (e.g. using Qt or MFC).


it is a I/O lower level language so you don't do things like sql,
That's not true either. Most database client drivers (e.g. libmysql, libpg, etc.) were written in C or at least expose a C compatible interface which allows them to be used from within virtually any programming language out there that allows calling external routines.


spinboxes and widgets its more programming like microchips...
C++ was first developed by Bjarne Stoustrup around 1979 as an extension to C (its original name was "C with classes" later renamed to C++ which stands for "one more than C") and is 99% forward compatible with C. First widely acknowleged graphical user interface was created in 1963 (so even before C language was created) in Xerox labs (application was called Sketchpad) for a platform much weaker than todays microcontrollers so you can see there is no fixed relation between GUI, microchips and programming languages. If you want to go into discussions about programming languages then do your homework first and research the subject.


Second of all, as you can see I'm still a novice to qt/c++
So learn to walk first before you sign in for a marathon.


and nearly every programming language in the "C-Family" works in the same way, don't mean you can use em all the same way.
You can write your program in C and compile it with a C++ compiler and it will work exactly the same way as if you built it with a C compiler. If you're familiar with C then go ahead and build your UI with what you know -- you don't have to implement any classes or use any other C++ features to create a simple user interface using Qt (at least since Qt5 where you can connect signals to arbitrary functions).


So I'm trying to learn by practising and doing something worthwhile at the same time.
So far you are trying to learn by having others do your tasks for you. Save yourself trouble, spend a week with a C++ book and then come back to your original project. I assure you it will pay back. Otherwise you'll be scratching your head forever wondering why you get random values from an uninitialized array which suggests your issues are something more than simply lack of C++ skills. Sorry to be so blunt but I'm way past my days of political correctness (if anyone wants to discuss that, I invite them to Warsaw for a beer or a cup of coffee). If you want to learn programming then do it by learning and then programming and not vice versa.

Cyrebo
31st March 2013, 10:38
Ok I can be blunt too, you are acting ignorant especially since you say people program GUI's in C haha. Everyone knows c++ and java are used more for GUI programming because they are OBJECT ORIENTED. Of course its not impossible but who does GUIs in C???? Once you know C you can program in all of the C family. And are you really aruguing the fact that C is a low-level langauge???Really??? I've written nearly a thousand lines of C++ code for this singular program just because you feel you know everything about C++ doesn't mean everyone you come in contact with has to aswell. I'm sure you're a nice person in the real world but you really come across as rude and abrasive here. So you can save yourself the trouble and pick up a book on communication/interpersonal skills and come back to helping people you believe aren't as knowledgeable as you on forums.

GTK+/Qt are nearly identical in implementation, I don't know why you would even mention that. They are both OBJECT-ORIENTED. And also, GTK isn't available for all versions of Linux and seeing as Qt is native to my version of Linux...

Thanks I might upgrade to Qt 5.

wysota
31st March 2013, 15:10
Ok I can be blunt too, you are acting ignorant especially since you say people program GUI's in C haha.
Well, you can laugh all you want but this won't change the fact that people do program GUI in C.


Everyone knows c++ and java are used more for GUI programming because they are OBJECT ORIENTED.
I don't see how this is relevant to anything. I'm not saying C++ is not used for GUI programming, I'm saying C is a general purpose language that is fit for UI programming.


Of course its not impossible but who does GUIs in C????
You'd be surprised :)


Once you know C you can program in all of the C family.
Your case proves you wrong. You know C (at least you say so and I have no reason to not believe you) but apparently currently you are not able to program in C++.


And are you really aruguing the fact that C is a low-level langauge???
I'm not arguing anything however the level of C and C++ is usually considered the same (I wouldn't call it "low level" though -- assembly is low level, C/C++ is definitely higher level than assembly). Both are strongly typed, compiled, native symbol based languages. The fact that one is more object oriented than the other doesn't influence the level of the language.


I've written nearly a thousand lines of C++ code for this singular program
Well, that's your problem, not mine. I'm not saying I could do the same in 400 lines of code but then I'm not saying I couldn't either.


just because you feel you know everything about C++
I never said I know everything about C++. But I do know similarities and differences between programming languages I use.


doesn't mean everyone you come in contact with has to aswell.
I never said that too. The point is that you will gain much more if you focus on learning the language first before you start using it with a framework as complex as Qt. I noticed that I'm not the only one to tell you that. If one person tells you there is something wrong with you, you can ignore him. If two people tell you that you are sick -- think about seeing a doctor.


GTK+/Qt are nearly identical in implementation
Nnnno.... GTK+ is written in C, Qt is pure C++ -- how come possibly would they be "nearly identical" in implementation?


They are both OBJECT-ORIENTED
No, they are not.

This is the official GTK+ "hello world" tutorial (with comments stripped out) :

#include <gtk/gtk.h>

static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}

int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

g_signal_connect (window, "delete-event",
G_CALLBACK (delete_event), NULL);

g_signal_connect (window, "destroy",
G_CALLBACK (destroy), NULL);

gtk_container_set_border_width (GTK_CONTAINER (window), 10);

button = gtk_button_new_with_label ("Hello World");

g_signal_connect (button, "clicked",
G_CALLBACK (hello), NULL);

g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);

gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}

Where do you see anything object oriented here?

Fundamental concepts of object-oriented programming are abstraction, encapsulation, polymorphism and inheritance. The code I quoted is just a set of plain function calls -- no polymorphism, no encapsulation, no inheritance and practically no abstraction, no objects.


And also, GTK isn't available for all versions of Linux
GTK doesn't care about your Linux version. The fact that your distro may be missing precompiled packages for GTK doesn't mean you can't build GTK for it.

This discussion is going nowhere. The primary fact (ignoring all the other things) is that you are trying to use a language you don't know and which makes you confused and I (we?) am telling you to focus on the language itself (without Qt, just plain C++) before trying to employ it to solve complex problems. Without knowledge about classes, inheritance, using and overriding virtual methods you will not be able to use Qt efficiently. You can learn all that in a couple of days and then you will have a chance to understand how to use Qt. We can see that even scopes of visibility are giving you problems and they are the same in both C and C++. You either accept that fact or not.

Cyrebo
31st March 2013, 16:24
I don't agree with everything you said but one important part of you speil is that I don't know C++ well enough to implement something on this scale on that point I agree. I will learn it more in the future, atleast I hope to, that and Qt which I really like now but the thing is I need to get this done so its not really a matter of learning c++ right now but getting my program finished before a certain deadline. If I get this part of reading the values from the spinbox, I don't think I will have any more major problems in the future in getting it done which is why I'm asking.

Cyrebo
31st March 2013, 21:00
well the number of people is how many people input values for example if you have two ppl that input values the number of people is two. im not sure what that verticalLayout is.
and what are you using to actually get the value from the spinbox? are you using this on the okButtonslot?
value = ui->spinboxName->value()

No but I copy value's values to another variable inside my okButton slot...So it's like:

The following is in dbInfo()

ui->verticalLayout->addWidget(label);
ui->verticalLayout->addWidget(spinbox);

values = spinbox->value();

Then okButton slot

spinboxValues << values;
for(int i = 0; i<numberofpeople; i++){
qDebug() << spinboxValues[i];
}

I'm getting this kind of output now for three spinboxes:

0
48
913
151259145
-1253538145
0

I was expecting 0,0,0 because Im only passing the initialised values which i thought would be default value of 0. How to get the user inputted values is what I need.

giblit
31st March 2013, 23:47
if you know how many spinboxes you are going to be using you could do this on the headers private:
QLabel *labelName[XAmount];
QSpinBox *spinBoxName[XAMOUNT];

then on your mainfunction put something like this to declare the items
for(int i = 0; i<XAMOUNT; i++){
spinBoxName[i] = new QSpinBox;
labelName[i] = new QLabel;
} then add how ever many spinboxes/labels you want at startup by adding them to the layout then send that amount to a variable called something like spinboxCount and on the okButtonSlot put something like this:

layout->addWidget(spinBoxName[spinboxCount+1]);
spinBoxCount += 1;

then after that you can get the value of each spinbox easy by doing like
for(int i = 0; i<spinBoxCount; i++){
values[i] = spinboxname[i]->value();
}

Cyrebo
1st April 2013, 00:56
That's just the problem, I don't, the number is unknown as the spnboxes are created dynamically based on the number in the database. Is there no way to implement this if the number of spinboxes is unknown?

giblit
1st April 2013, 01:09
yes I actually just figured it out via my program lol
on the header put this:

QList<QSpinBox*> spinBoxes;
QSpinBox *spinBox;

and put this on your mainfunction dbinfo
QList<int> spinBoxesValue;
and on your push button you are creating the spinboxes do this

spinBox = new QSpinBox;
spinBoxes << spinBox;
for(int i = 0; i<spinBoxes.size();i++){
//add your widget how ever I am doing it by something like this
layout->addWidget(spinBoxes[i],i+1;0);
//get the value if you want when pressed otherwise put where you are getting
spinBoxesValue << spinBoxes[i]->value();
}
then just use a for loop to get the numbers

Cyrebo
1st April 2013, 01:34
Kinda got excited there for a second until I read the lastpart....
and on your push button you are creating the spinboxes do this Its a GUI and that way the user creates the spinboxes on button clicked. Is there a way this could work with the spinboxes dynamically created at run time and read the values on button clicked rather?

I appreciate all the help from you on this:)

giblit
1st April 2013, 01:54
U could put the buttons in a list then loop the signal from button x to spinhox x value

And have same slot for all buttons

Cyrebo
1st April 2013, 01:56
Would this mean the spinboxes are added automatically when the form opens? The ok button then reads the values entered by the user on the form?

giblit
1st April 2013, 02:02
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

Cyrebo
1st April 2013, 02:17
Got an error on this line.
layout->addWidget(spinBoxes[i],i+1;0);
I substituted with my actual layout and it says expected ; got )
What is it doing so I can make it sit well with the compiler?

giblit
1st April 2013, 02:23
The I+1;0 is supposed to be I+1,0 was a mistype and the I+1 is the row pos and 0 is column pos

And u need to declare layout by QGridLayout then create a QWidget and set the layout of the qwidget to that layout the set the qwidget as the central widget

Cyrebo
1st April 2013, 02:58
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():

spinBox = new QSpinBox;
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


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.

giblit
1st April 2013, 03:15
declare the QList<int> spinBoxesValue outside of the getdbinfo function sorry. declare it above it by the headers so it is global.


The function that outputs the user input values can't use the spinBoxesValue variable..

This is getdbInfo():

spinBox = new QSpinBox;
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


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:


//header file
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QSpinBox>
#include <QPushButton>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow();

private slots:
void okButton();

private:
QWidget* widget;
QList<QSpinBox*> spinBoxes;
QSpinBox *spinbox;
QPushButton *button;
};

#endif // MAINWINDOW_H

//main c++
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, 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(){
widget = new QWidget;
button = new QPushButton;
button->setText("Ok");
QGridLayout* layout = new QGridLayout;
for(int i = 0; i<5; i++){
spinbox = new QSpinBox;
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
4
10
11
12
8

Cyrebo
1st April 2013, 03:39
hmmmmm wonder what I'm doing wrong but I'm getting the errr

(.bss+0x10):-1: error: multiple definition of `spinBoxes'

Followed by:


(.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.



void MainWindow::dbinfo()
{
QSqlQuery qry;

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();
QLabel *label = new QLabel(QString(cust));
spinBox = new QSpinBox;
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];
}
}

giblit
1st April 2013, 03:54
you probably have this on your header:

QList<QSpinBox*> spinBoxes;
QSpinBox *spinBoxes;
instead of

QList<QSpinBox*>spinBoxes;
QSpinBox *spinBox;

or you declared spinBoxes as something else like QString or something somewhere else in any of your c++/header files.

wysota
1st April 2013, 10:56
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.

Cyrebo
1st April 2013, 12:34
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.


void MainWindow::dbinfo()
{
QSqlQuery qry;

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();
QLabel *label = new QLabel(QString(cust));
spinbox = new QSpinBox;
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);
}
}
}
}

Lesiok
1st April 2013, 19:46
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.

Cyrebo
1st April 2013, 20:38
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..

Lesiok
1st April 2013, 21:34
To use someone else's code you need to first understand it. "Copy and paste" is not a good tool for programmers.

giblit
1st April 2013, 23:44
well I wasn't trying to give him code but an example of how to get values of dynamicly created spinboxes

Cyrebo
23rd April 2013, 00:07
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:




{
ui->setupUi(this);

fb = new FrmBuilder(ui);
QFileInfo checkFile(Path_to_DB);

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:



QList<int> values;
QLabel *label;

FrmBuilder::FrmBuilder(Ui::MainWindow *ui)
{
this->myUi = ui;
}

void FrmBuilder::buildFrm()
{
QSqlQuery qry;

if (qry.exec("SELECT name FROM persons"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
QString person = qry.record().value(0).toString();
label = new QLabel(QString(person));
spinbox = new QSpinBox;
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];
}
}

wysota
23rd April 2013, 00:43
You can find a solution here: http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

Cyrebo
23rd April 2013, 10:39
.............

wysota
23rd April 2013, 11:01
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.

Cyrebo
23rd April 2013, 12:02
................

Cyrebo
23rd April 2013, 15:51
Thread SOLVED. Had the answer all along, thanks.