Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 56

Thread: use value from dynamically created widget in a slot

  1. #21
    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 giblit View Post
    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
    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).
    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.


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

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

    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?

  4. #24
    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
    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

    --

    Quote Originally Posted by giblit View Post
    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?
    Qt Code:
    1. int numberofpeople = ui->verticalLayout->count();
    2. div(numberofpeople,2);
    3.  
    4. for(int i = 0; i<numberofpeople; i++){
    5. qDebug() << spinboxValues[i];
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    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?
    Qt Code:
    1. value = ui->spinboxName->value()
    To copy to clipboard, switch view to plain text mode 

  6. #26
    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
    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?
    Qt Code:
    1. value = ui->spinboxName->value()
    To copy to clipboard, switch view to plain text mode 
    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.

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


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

    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.
    Last edited by Cyrebo; 31st March 2013 at 10:46.

  9. #29
    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
    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) :

    C Code:
    1. #include <gtk/gtk.h>
    2.  
    3. static void hello( GtkWidget *widget,
    4. gpointer data )
    5. {
    6. g_print ("Hello World\n");
    7. }
    8.  
    9. static gboolean delete_event( GtkWidget *widget,
    10. GdkEvent *event,
    11. gpointer data )
    12. {
    13. g_print ("delete event occurred\n");
    14. return TRUE;
    15. }
    16.  
    17. /* Another callback */
    18. static void destroy( GtkWidget *widget,
    19. gpointer data )
    20. {
    21. gtk_main_quit ();
    22. }
    23.  
    24. int main( int argc, char *argv[] )
    25. {
    26. GtkWidget *window;
    27. GtkWidget *button;
    28.  
    29. gtk_init (&argc, &argv);
    30.  
    31. window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    32.  
    33. g_signal_connect (window, "delete-event",
    34. G_CALLBACK (delete_event), NULL);
    35.  
    36. g_signal_connect (window, "destroy",
    37. G_CALLBACK (destroy), NULL);
    38.  
    39. gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    40.  
    41. button = gtk_button_new_with_label ("Hello World");
    42.  
    43. g_signal_connect (button, "clicked",
    44. G_CALLBACK (hello), NULL);
    45.  
    46. g_signal_connect_swapped (button, "clicked",
    47. G_CALLBACK (gtk_widget_destroy),
    48. window);
    49.  
    50. gtk_container_add (GTK_CONTAINER (window), button);
    51. gtk_widget_show (button);
    52. gtk_widget_show (window);
    53. gtk_main ();
    54. return 0;
    55. }
    To copy to clipboard, switch view to plain text mode 

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


  10. #30
    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 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.
    Last edited by Cyrebo; 31st March 2013 at 19:30.

  11. #31
    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
    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?
    Qt Code:
    1. value = ui->spinboxName->value()
    To copy to clipboard, switch view to plain text mode 
    No but I copy value's values to another variable inside my okButton slot...So it's like:

    The following is in dbInfo()
    Qt Code:
    1. ui->verticalLayout->addWidget(label);
    2. ui->verticalLayout->addWidget(spinbox);
    3.  
    4. values = spinbox->value();
    To copy to clipboard, switch view to plain text mode 

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

    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.
    Last edited by Cyrebo; 31st March 2013 at 21:25.

  12. #32
    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

    if you know how many spinboxes you are going to be using you could do this on the headers private:
    Qt Code:
    1. QLabel *labelName[XAmount];
    2. QSpinBox *spinBoxName[XAMOUNT];
    To copy to clipboard, switch view to plain text mode 

    then on your mainfunction put something like this to declare the items
    Qt Code:
    1. for(int i = 0; i<XAMOUNT; i++){
    2. spinBoxName[i] = new QSpinBox;
    3. labelName[i] = new QLabel;
    4. }
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. layout->addWidget(spinBoxName[spinboxCount+1]);
    2. spinBoxCount += 1;
    To copy to clipboard, switch view to plain text mode 

    then after that you can get the value of each spinbox easy by doing like
    Qt Code:
    1. for(int i = 0; i<spinBoxCount; i++){
    2. values[i] = spinboxname[i]->value();
    3. }
    To copy to clipboard, switch view to plain text mode 

  13. #33
    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

    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?

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

    yes I actually just figured it out via my program lol
    on the header put this:
    Qt Code:
    1. QList<QSpinBox*> spinBoxes;
    2. QSpinBox *spinBox;
    To copy to clipboard, switch view to plain text mode 
    and put this on your mainfunction dbinfo
    Qt Code:
    1. QList<int> spinBoxesValue;
    To copy to clipboard, switch view to plain text mode 
    and on your push button you are creating the spinboxes do this
    Qt Code:
    1. spinBox = new QSpinBox;
    2. spinBoxes << spinBox;
    3. for(int i = 0; i<spinBoxes.size();i++){
    4. //add your widget how ever I am doing it by something like this
    5. layout->addWidget(spinBoxes[i],i+1;0);
    6. //get the value if you want when pressed otherwise put where you are getting
    7. spinBoxesValue << spinBoxes[i]->value();
    8. }
    To copy to clipboard, switch view to plain text mode 
    then just use a for loop to get the numbers

  15. #35
    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

    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

  16. #36
    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

    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

  17. #37
    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

    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?

  18. #38
    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

    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

  19. #39
    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

    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?

  20. #40
    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

    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

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.