Page 3 of 3 FirstFirst 123
Results 41 to 56 of 56

Thread: use value from dynamically created widget in a slot

  1. #41
    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 giblit View Post
    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():
    Qt Code:
    1. spinBox = new QSpinBox;
    2. spinBoxes << spinBox;
    3. QList<int> spinBoxesValue;
    4. for(int i = 0; i<spinBoxes.size();i++){
    5. ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
    6. spinBoxesValue << spinBoxes[i]->value();
    7. }
    To copy to clipboard, switch view to plain text mode 

    And this is to print values aka okButton function
    Qt Code:
    1. spinboxValues << spinBoxesValue;
    2. for(int i = 0; i<numberofpeople; i++){
    3. qDebug() << spinboxValues[i];
    4. }
    To copy to clipboard, switch view to plain text mode 

    I have included everything you said and even passed spinBoxesValue as a variable.

  2. #42
    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

    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 View Post
    The function that outputs the user input values can't use the spinBoxesValue variable..

    This is getdbInfo():
    Qt Code:
    1. spinBox = new QSpinBox;
    2. spinBoxes << spinBox;
    3. QList<int> spinBoxesValue;
    4. for(int i = 0; i<spinBoxes.size();i++){
    5. ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
    6. spinBoxesValue << spinBoxes[i]->value();
    7. }
    To copy to clipboard, switch view to plain text mode 

    And this is to print values aka okButton function
    Qt Code:
    1. spinboxValues << spinBoxesValue;
    2. for(int i = 0; i<numberofpeople; i++){
    3. qDebug() << spinboxValues[i];
    4. }
    To copy to clipboard, switch view to plain text mode 

    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:
    Qt Code:
    1. //header file
    2. #ifndef MAINWINDOW_H
    3. #define MAINWINDOW_H
    4.  
    5. #include <QMainWindow>
    6. #include <QSpinBox>
    7. #include <QPushButton>
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MainWindow();
    15.  
    16. private slots:
    17. void okButton();
    18.  
    19. private:
    20. QWidget* widget;
    21. QList<QSpinBox*> spinBoxes;
    22. QSpinBox *spinbox;
    23. QPushButton *button;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    27.  
    28. //main c++
    29. #include <QtGui/QApplication>
    30. #include "mainwindow.h"
    31.  
    32. int main(int argc, char *argv[])
    33. {
    34. QApplication a(argc, argv);
    35. MainWindow w;
    36. w.show();
    37.  
    38. return a.exec();
    39. }
    40.  
    41.  
    42. //mainwindow c++
    43. #include "mainwindow.h"
    44. #include <QGridLayout>
    45. #include <iostream>
    46. using namespace std;
    47.  
    48. QList<int> values;
    49. MainWindow::MainWindow(){
    50. widget = new QWidget;
    51. button = new QPushButton;
    52. button->setText("Ok");
    53. QGridLayout* layout = new QGridLayout;
    54. for(int i = 0; i<5; i++){
    55. spinbox = new QSpinBox;
    56. spinBoxes << spinbox;
    57. layout->addWidget(spinBoxes[i], i+1, 0);
    58. }
    59. layout->addWidget(button, 0, 0);
    60. widget->setLayout(layout);
    61. setCentralWidget(widget);
    62. connect(button,SIGNAL(clicked()),
    63. this,SLOT(okButton()));
    64. }
    65.  
    66. void MainWindow::okButton(){
    67. for(int i = 0; i<spinBoxes.size();i++){
    68. values << spinBoxes[i]->value();
    69. cout << values[i] << endl;
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 

    with the inputed values of 4, 10, 11, 12, 8 you will get a printed value of
    4
    10
    11
    12
    8
    Last edited by giblit; 1st April 2013 at 03:15.

  3. #43
    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

    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.

    Qt Code:
    1. void MainWindow::dbinfo()
    2. {
    3.  
    4. if (qry.exec("SELECT name FROM customers"))
    5. {
    6. while(qry.next())
    7. {
    8. qDebug() << qry.value(0).toString();
    9. if(qry.isValid())
    10. {
    11. QString cust = qry.record().value(0).toString();
    12. QLabel *label = new QLabel(QString(cust));
    13. spinBox = new QSpinBox;
    14. spinBoxes << spinBox;
    15.  
    16. label->setGeometry(0,0,80,41);
    17. ui->verticalLayout->addWidget(label);
    18. for(int i = 0; i<spinBoxes.size();i++){
    19. ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
    20. }
    21. }
    22. }
    23. }
    24. else
    25. {
    26. qDebug() << qry.lastError();
    27. }
    28. }
    29. void MainWindow::on_pushButton_clicked()
    30. {
    31. //int numberofitems = ui->verticalLayout->count();
    32. //div(numberofitems,2);
    33.  
    34. for(int i = 0; i<spinBoxes.size(); i++){
    35. values << spinBoxes[i]->value();
    36. qDebug() << values[i];
    37. }
    38. }
    To copy to clipboard, switch view to plain text mode 

  4. #44
    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 probably have this on your header:
    Qt Code:
    1. QList<QSpinBox*> spinBoxes;
    2. QSpinBox *spinBoxes;
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. QList<QSpinBox*>spinBoxes;
    2. QSpinBox *spinBox;
    To copy to clipboard, switch view to plain text mode 

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

  5. #45
    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
    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.
    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.


  6. #46
    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 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.

    Qt Code:
    1. void MainWindow::dbinfo()
    2. {
    3.  
    4. if (qry.exec("SELECT name FROM customer"))
    5. {
    6. while(qry.next())
    7. {
    8. qDebug() << qry.value(0).toString();
    9. if(qry.isValid())
    10. {
    11. QString cust = qry.record().value(0).toString();
    12. QLabel *label = new QLabel(QString(cust));
    13. spinbox = new QSpinBox;
    14. spinBoxes << spinbox;
    15.  
    16. label->setGeometry(0,0,80,41);
    17. for(int i = 0; i<spinBoxes.size();i++){
    18. ui->verticalLayout->addWidget(label);
    19. ui->verticalLayout->addWidget(spinBoxes[i],i+1,0);
    20. }
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cyrebo; 1st April 2013 at 12:52.

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

    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.

  8. #48
    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
    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..

  9. #49
    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

    To use someone else's code you need to first understand it. "Copy and paste" is not a good tool for programmers.

  10. #50
    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 slotn't

    well I wasn't trying to give him code but an example of how to get values of dynamicly created spinboxes

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

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

    Qt Code:
    1. {
    2. ui->setupUi(this);
    3.  
    4. fb = new FrmBuilder(ui);
    5. QFileInfo checkFile(Path_to_DB);
    6.  
    7. if(checkFile.isWritable())
    8. {
    9. if(db.open())
    10. {
    11. qDebug() << "Connected to database file";
    12. }
    13. }else{
    14. qDebug() << "Database file not found";
    15. }
    16.  
    17. fb->buildFrm();
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete fb;
    23. delete ui;
    24. }
    25.  
    26. void MainWindow::on_pushButton_clicked()
    27. {
    28. fb->display();
    29. }
    To copy to clipboard, switch view to plain text mode 

    class FrmBuilder:
    Qt Code:
    1. QList<int> values;
    2. QLabel *label;
    3.  
    4. FrmBuilder::FrmBuilder(Ui::MainWindow *ui)
    5. {
    6. this->myUi = ui;
    7. }
    8.  
    9. void FrmBuilder::buildFrm()
    10. {
    11. QSqlQuery qry;
    12.  
    13. if (qry.exec("SELECT name FROM persons"))
    14. {
    15. while(qry.next())
    16. {
    17. qDebug() << qry.value(0).toString();
    18. QString person = qry.record().value(0).toString();
    19. label = new QLabel(QString(person));
    20. spinbox = new QSpinBox;
    21. spinBoxes << spinbox;
    22.  
    23. label->setGeometry(0,0,80,41);
    24.  
    25. for(int i = 0; i<spinBoxes.size();i++){
    26. myUi->verticalLayout->addWidget(label);
    27. myUi->verticalLayout_2->addWidget(spinBoxes[i],0,0);
    28. }
    29. }
    30. }
    31. else
    32. {
    33. qDebug() << qry.lastError();
    34. }
    35. qry.clear();
    36. }
    37.  
    38. void FrmBuilder::display()
    39. {
    40. for(int i = 0; i<spinBoxes.size(); i++){
    41. values << spinBoxes[i]->value();
    42. qDebug() << values[i];
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

  12. #52
    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 slotn't

    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.


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

    Default Re: use value from dynamically created widget in a slotn't

    .............
    Last edited by Cyrebo; 23rd April 2013 at 10:45.

  14. #54
    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 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.
    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.


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

    Default Re: use value from dynamically created widget in a slotn't

    ................
    Last edited by Cyrebo; 23rd April 2013 at 12:17.

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

    Default Re: use value from dynamically created widget in a slotn't

    Thread SOLVED. Had the answer all along, thanks.

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.