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 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()'

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

  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

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

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

  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

    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?

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

  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

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

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

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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

  11. #11
    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?

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

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

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


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
  •  
Qt is a trademark of The Qt Company.