Results 1 to 6 of 6

Thread: How to use string from a class in other classes

  1. #1
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to use string from a class in other classes

    hi, im new, i have this code


    Qt Code:
    1. QString cuenta;
    2. cuenta=ui->lineEdit->text();
    To copy to clipboard, switch view to plain text mode 



    this string its declared in public members in a Class names log, i need use this string in other classes, please help me, im novice in C++ and new in QT, thanks in advance, greetings

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use string from a class in other classes

    Then you can post questions in General Programming Forum...

    Qt Code:
    1. class LogClass {
    2. public:
    3. QString textString;
    4.  
    5. QString getTextString(void) const;
    6. };
    7.  
    8. QString LogClass::getTextString(void) const
    9. {
    10. return textString;
    11. }
    12.  
    13. function()
    14. {
    15. LogClass log;
    16.  
    17. // Both does the same
    18. QString text_1 = log.getTextString();
    19. QString text_2 = log.textString
    20. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use string from a class in other classes

    thnks, i was try this but i have a error in the compilation, i did this:

    Qt Code:
    1. MainWindow.cpp
    2.  
    3. #include "LogClass.h"
    4.  
    5.  
    6. log login;
    7. QString showme;
    8. showme=login.cuenta;
    9. ui->mostrarcuenta->setText(showtime);
    To copy to clipboard, switch view to plain text mode 

    the error its this:

    login was not declarate in this scope

    thnks

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use string from a class in other classes

    show the log class definition, also I guess there should be more errors, post them too

  5. #5
    Join Date
    Jul 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use string from a class in other classes

    ok, this its:

    Qt Code:
    1. #ifndef LOG_H
    2. #define LOG_H
    3.  
    4. #include <QMainWindow>
    5. #include <QWidget>
    6. #include "mainwindow.h"
    7. #include <QSqlQuery>
    8. #include <QKeyEvent>
    9.  
    10. namespace Ui {
    11. class log;
    12. }
    13.  
    14. class log : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit log(QWidget *parent = 0);
    20.  
    21.  
    22. QString cuenta;
    23. ~log();
    24.  
    25. private:
    26. Ui::log *ui;
    27. MainWindow m;
    28.  
    29. private slots:
    30. // void funcion::keyPressEvent( QKeyEvent* );
    31. void on_pushButton_clicked();
    32. };
    33.  
    34. #endif // LOG_H
    To copy to clipboard, switch view to plain text mode 

    then, i call the Qstring cuenta so:

    this code is located in mainwindow.cpp
    Qt Code:
    1. log login;
    2. QString showme;
    3. showme= login.cuenta;
    To copy to clipboard, switch view to plain text mode 

    the compiler does not show other error

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to use string from a class in other classes

    first point, don't use a standard name like log (it can conflict with log() function in math.h), use a different class name.

    next the error might related to Ui::log class, not ::log class (note the scope), try including "ui_log.h" (or whatever is the ui file name is) in mainwindow.cpp

Similar Threads

  1. Replies: 4
    Last Post: 7th June 2011, 00:46
  2. Replies: 6
    Last Post: 15th April 2010, 20:02
  3. Replies: 11
    Last Post: 25th November 2008, 07:54
  4. wanted to re-instantiate two classes from a class
    By salmanmanekia in forum General Programming
    Replies: 2
    Last Post: 22nd August 2008, 08:59
  5. How to search a string in xml file with Qt Dom classes
    By doganay44 in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2006, 20:16

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.