use value from dynamically created widget in a slot
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:
Code:
if(checkFile.isFile())
{
if(db.open())
{
qDebug() << "Connected to database file";
}
}else{
qDebug() << "Database file not found";
}
int value;
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
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!
--
Re: use value from dynamically created widget in a slot
Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Lesiok
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:
Quote:
/mainwindow.cpp:68: error: 'spinbox' cannot appear in a constant-expression
This is what I did:
Code:
QString cust
= qry.
record().
value(0).
toString();
QList<*spinbox>;
Re: use value from dynamically created widget in a slot
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.
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
wysota
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?
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Cyrebo
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.
Re: use value from dynamically created widget in a slot
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.
Re: use value from dynamically created widget in a slot
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.
Re: use value from dynamically created widget in a slot
Well actually, I can use functions in the main.cpp, I just need to know how to use them in mainwindow class please.
Re: use value from dynamically created widget in a slot
I already said, functions in C and C++ work the same way, with respect to scopes (such as classes).
Re: use value from dynamically created widget in a slot
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...
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Cyrebo
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:
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
{
Q_OBJECT
public:
MainWindow();
~MainWindow();
private slots:
void functionSlot();
private:
void function();
//QWidgetType *QWidgetName.....
//ex:
};
#endif
then on the mainWindow cpp file
Code:
#include <QtGui>
#include "mainwindow.h"
MainWindow::MainWindow(){
pushButton = newQPushButton;
function();
}
void MainWindow::function(){
pusButton->setText("Button");
//do something
}
and on the main.cpp file
Code:
#include <QtGui/QApplication>
#inlucde "mainwindow.h"
int main(int argc, char *argv[]){
MainWindow w;
w.show();
return a.exec();
}
Re: use value from dynamically created widget in a slot
Thanks man, I've been looking for something like that all day but I still get this error on runtime..
Quote:
-build-desktop-Qt_4_8_3_in_PATH__System__Release/moc_mainwindow.cpp:-1: error: undefined reference to `MainWindow::dbinfoSlot()'
Re: use value from dynamically created widget in a slot
because you need to put void before MainWindow more than likely.
like this:
Code:
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.
Re: use value from dynamically created widget in a slot
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
Code:
void MainWindow::dbinfo()
{
int value;
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();
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...
Re: use value from dynamically created widget in a slot
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:
Code:
MainWindow::MainWindow(){
connect(pushButton,SIGNAL(clicked()),
this,SLOT(functionSlot()));
}
MainWindow::functionSlot(){
//do something
}
Re: use value from dynamically created widget in a slot
: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?
Re: use value from dynamically created widget in a slot
you could possibly send the values from the spinbox into a list of ints like
Code:
QList<int> spinboxValues;
spinboxValues << value;
for(int i = 0; i<numberofpeople; i++){
cout << spinBoxValues[i] << endl;
}
Re: use value from dynamically created widget in a slot
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++...
Re: use value from dynamically created widget in a slot
Quote:
Originally Posted by
Cyrebo
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:
Code:
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