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
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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.


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

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

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

    Cyrebo (30th March 2013)

  9. #8
    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()'

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

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

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

    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 

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

    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?

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

    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 

  15. #14
    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++...

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.