Page 1 of 3 123 LastLast
Results 1 to 20 of 56

Thread: use value from dynamically created widget in a slot

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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:
    Qt Code:
    1. QFileInfo checkFile(Path_to_DB);
    2.  
    3. if(checkFile.isFile())
    4. {
    5. if(db.open())
    6. {
    7. qDebug() << "Connected to database file";
    8. }
    9. }else{
    10. qDebug() << "Database file not found";
    11. }
    12.  
    13. int value;
    14. QSqlQuery qry;
    15.  
    16. if (qry.exec("SELECT name FROM customer"))
    17. {
    18. while(qry.next())
    19. {
    20. qDebug() << qry.value(0).toString();
    21. if(qry.isValid())
    22. {
    23. QString cust = qry.record().value(0).toString();
    24.  
    25. //create widgets
    26. QLabel *label = new QLabel(QString(cust));
    27. QSpinBox *spinbox = new QSpinBox;
    28. spinbox->setMaximum(3);
    29. label->setGeometry(0,0,80,41);
    30.  
    31. //add to VBoxLayout
    32. ui->verticalLayout->addWidget(label);
    33. ui->verticalLayout->addWidget(spinbox);
    34. value = spinbox->value(); // value is always going to be zero here
    35. qDebug() << value;
    36. }
    37. }
    38. }
    39. else
    40. {
    41. qDebug() << qry.lastError();
    42. }
    43. }
    44.  
    45. MainWindow::~MainWindow()
    46. {
    47. delete ui;
    48. }
    49.  
    50. void MainWindow::on_pushButton_clicked()
    51. {
    52. //get spinbox values
    53. if(!db.isOpen()){
    54. qDebug() << "No connection to db";
    55. return;
    56. }
    57. int value2;
    58. value2 = spinbox->value2();
    59. qDebug() << value2;
    60.  
    61. //save values to db
    62. ............
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

    --

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: use value from dynamically created widget in a slot

    Save spinbox pointers to the list defined in MainWindow class (QList<QSpinBox*>).

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: use value from dynamically created widget in a slot

    Quote Originally Posted by Lesiok View Post
    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:
    Qt Code:
    1. QString cust = qry.record().value(0).toString();
    2. QLabel *label = new QLabel(QString(cust));
    3. QSpinBox *spinbox = new QSpinBox(this);
    4. QList<*spinbox>;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: use value from dynamically created widget in a slot

    Quote Originally Posted by wysota View Post
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: use value from dynamically created widget in a slot

    Quote Originally Posted by Cyrebo View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

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

  12. #12
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: use value from dynamically created widget in a slot

    Quote Originally Posted by Cyrebo View Post
    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:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6.  
    7. class MainWindow: public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. MainWindow();
    13. ~MainWindow();
    14.  
    15.  
    16. private slots:
    17. void functionSlot();
    18.  
    19. private:
    20. void function();
    21.  
    22. //QWidgetType *QWidgetName.....
    23. //ex:
    24. QPushButton *pushButton;
    25.  
    26. };
    27.  
    28. #endif
    To copy to clipboard, switch view to plain text mode 

    then on the mainWindow cpp file
    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(){
    6. pushButton = newQPushButton;
    7. function();
    8. }
    9. void MainWindow::function(){
    10. pusButton->setText("Button");
    11. //do something
    12. }
    To copy to clipboard, switch view to plain text mode 
    and on the main.cpp file
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #inlucde "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[]){
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

  13. The following user says thank you to giblit for this useful post:

    Cyrebo (30th March 2013)

  14. #13
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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..
    -build-desktop-Qt_4_8_3_in_PATH__System__Release/moc_mainwindow.cpp:-1: error: undefined reference to `MainWindow::dbinfoSlot()'

  15. #14
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: use value from dynamically created widget in a slot

    because you need to put void before MainWindow more than likely.
    like this:
    Qt Code:
    1. void MainWindow::somethingSlotIForgotName(){
    2. //do something
    3. }
    To copy to clipboard, switch view to plain text mode 

    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.

  16. #15
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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

    Qt Code:
    1. void MainWindow::dbinfo()
    2. {
    3. int value;
    4.  
    5. if (qry.exec("SELECT name FROM customers"))
    6. {
    7. while(qry.next())
    8. {
    9. qDebug() << qry.value(0).toString();
    10. if(qry.isValid())
    11. {
    12. QString cust = qry.record().value(0).toString();
    13. QLabel *label = new QLabel(QString(cust));
    14. QSpinBox *spinbox = new QSpinBox(this);
    15.  
    16. label->setGeometry(0,0,80,41);
    17.  
    18. ui->verticalLayout->addWidget(label);
    19. ui->verticalLayout->addWidget(spinbox);
    20. value = spinbox->value();
    21. qDebug() << value;
    22. }
    23. }
    24. }
    25. else
    26. {
    27. qDebug() << qry.lastError();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

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

  17. #16
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default 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:
    Qt Code:
    1. MainWindow::MainWindow(){
    2. connect(pushButton,SIGNAL(clicked()),
    3. this,SLOT(functionSlot()));
    4. }
    5. MainWindow::functionSlot(){
    6. //do something
    7. }
    To copy to clipboard, switch view to plain text mode 

  18. #17
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: use value from dynamically created widget in a slot

    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?

  19. #18
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default 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
    Qt Code:
    1. QList<int> spinboxValues;
    2. spinboxValues << value;
    3. for(int i = 0; i<numberofpeople; i++){
    4. cout << spinBoxValues[i] << endl;
    5. }
    To copy to clipboard, switch view to plain text mode 

  20. #19
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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++...

  21. #20
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: use value from dynamically created widget in a slot

    Quote Originally Posted by Cyrebo View Post
    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:
    Qt Code:
    1. QList<double> spinboxValues;
    2. double values;
    3. mainWindow::dbinfo(){
    4. //get values from spin box
    5. }
    6. void MainWindow::okbuttonslot(){
    7. spinBoxValue << value;
    8. }
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Accessing Dynamically created Checkboxe
    By premroxx in forum Newbie
    Replies: 1
    Last Post: 6th November 2012, 08:14
  2. Replies: 9
    Last Post: 2nd November 2011, 10:12
  3. Replies: 12
    Last Post: 24th October 2011, 08:56
  4. Replies: 3
    Last Post: 11th August 2011, 18:16
  5. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 10:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.