Results 1 to 8 of 8

Thread: call variable from other function

  1. #1
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default call variable from other function

    Hi.

    Im declared one variable in ui.h:

    Qt Code:
    1. class Ui_MainWindow
    2. {
    3. public:
    4. QWidget *centralwidget;
    5. QPushButton *pushButtonOpen;
    6. QPushButton *pushButtonClose;
    7. QLabel *label;
    8. ..........
    9. }
    To copy to clipboard, switch view to plain text mode 

    and I need call from function.cpp and this say on compile: "'label' undeclared"
    Im include ui.h in function.cpp but is the same!

    Any help?.

    Thz

    waly
    Last edited by jacek; 31st August 2007 at 00:16. Reason: changed [qtclass] to [code]

  2. #2
    Join Date
    Aug 2006
    Posts
    90
    Thanks
    6
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    It is really hard to say what your actual problem is with your limited example... but I would make sure that your "label" variable is scoped correctly when you are using it and that <QLabel> is included some how in your ui.h.

  3. #3
    Join Date
    Aug 2007
    Location
    Russia
    Posts
    19
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    You need
    Qt Code:
    1. #include <QLabel>
    To copy to clipboard, switch view to plain text mode 
    in top of MainWindow.cpp
    and
    Qt Code:
    1. ui.label->setText("It's OK now!");
    To copy to clipboard, switch view to plain text mode 
    somewhere you need ))

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    Is by any chance that ui.h the result of uic?
    Because you can't modify that directly. It will get overwritten every time you modify it in Designer.

    Regards

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: call variable from other function

    I warmly suggest reading: Using a Component in Your Application.
    J-P Nurmi

  6. #6
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    it's code for example:

    ui.h:
    Qt Code:
    1. #ifndef UI_GUI_H
    2. #define UI_GUI_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QLabel>
    9. #include <QtGui/QMainWindow>
    10. #include <QtGui/QMenuBar>
    11. #include <QtGui/QPushButton>
    12. #include <QtGui/QWidget>
    13.  
    14. class Ui_MainWindow
    15. {
    16. public:
    17. QWidget *centralwidget;
    18. QPushButton *pushButtonOpen;
    19. QPushButton *pushButtonClose;
    20. QLabel *label;
    21. QMenuBar *menubar;
    22.  
    23. void setupUi(QMainWindow *MainWindow)
    24. {
    25. .....
    26. label = new QLabel(centralwidget);
    27. label->setObjectName(QString::fromUtf8("label"));
    28. label->setGeometry(QRect(60, 100, 46, 14));
    29. .......
    30.  
    31. retranslateUi(MainWindow);
    32. //QObject::connect(pushButtonOpen, SIGNAL(clicked()), label, SLOT(clear()));
    33.  
    34. QMetaObject::connectSlotsByName(MainWindow);
    35. } // setupUi
    36.  
    37. void retranslateUi(QMainWindow *MainWindow)
    38. {
    39. ........
    40. label->setText(QApplication::translate("MainWindow", "TextLabel", 0, QApplication::UnicodeUTF8));
    41. Q_UNUSED(MainWindow);
    42. } // retranslateUi
    43.  
    44. };
    45.  
    46. namespace Ui {
    47. class MainWindow: public Ui_MainWindow {};
    48. } // namespace Ui
    49.  
    50. #endif // UI_GUI_H
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. #include "ventana.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. intervalo = 0;
    11.  
    12. Ventana widget;
    13. widget.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    ventana.h:
    Qt Code:
    1. #include "ui.h"
    2.  
    3. class Ventana : public QMainWindow, private Ui::MainWindow
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. Ventana();
    9.  
    10. public slots:
    11. void Bopen();
    12. void Bclose();
    13. };
    To copy to clipboard, switch view to plain text mode 

    ventana.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "ventana.h"
    3. #include "function.h"
    4.  
    5. Ventana::Ventana()
    6. {
    7. setupUi(this);
    8.  
    9. connect(pushButtonOpen, SIGNAL(clicked()), this, SLOT(Bopen()));
    10. connect(pushButtonClose, SIGNAL(clicked()), this, SLOT(Bclose()));
    11. }
    12.  
    13. void Ventana::Bclose(){
    14. label->setText(QString("CloseOk"));
    15. functionDoSomething();
    16. }
    17.  
    18. void Ventana::Bopen(){
    19. label->setText(QString("OpenOK"));
    20. }
    To copy to clipboard, switch view to plain text mode 

    From HERE im call label->setText();
    But say is undeclared.

    function.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "function.h"
    3.  
    4. void functionDoSomething(){
    5. //here call to Qlabel
    6. label->setText(QString("HELLO WORLD"));
    7. }
    To copy to clipboard, switch view to plain text mode 

    function.h:
    Qt Code:
    1. void functionDoSomething();
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    you cannot add any members there.
    Do it in designer, and uic will put them there.

    Regards

  8. #8
    Join Date
    Jul 2007
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: call variable from other function

    Quote Originally Posted by marcel View Post
    you cannot add any members there.
    Do it in designer, and uic will put them there.

    Regards
    Im not understand!!

Similar Threads

  1. Cannot call function without object
    By Salazaar in forum Newbie
    Replies: 5
    Last Post: 11th June 2007, 14:55
  2. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  3. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52
  4. I got two problems when I used static compiled library of QT4
    By qintm in forum Installation and Deployment
    Replies: 8
    Last Post: 20th April 2006, 08:52
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 21:26

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.