Results 1 to 20 of 56

Thread: use value from dynamically created widget in a slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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()'

Similar Threads

  1. Accessing Dynamically created Checkboxe
    By premroxx in forum Newbie
    Replies: 1
    Last Post: 6th November 2012, 07:14
  2. Replies: 9
    Last Post: 2nd November 2011, 09:12
  3. Replies: 12
    Last Post: 24th October 2011, 07:56
  4. Replies: 3
    Last Post: 11th August 2011, 17:16
  5. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09: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.