Results 1 to 9 of 9

Thread: Can i set resultLinEdit->text() from outside class ?

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Can i set resultLinEdit->text() from outside class ?

    Hello! Friends,
    I am working in WinXP with Qt4.3.4. I have designed a GUI for getting square of a given number. I have following files: -
    1.)sample.ui
    2.)sample.h
    3.)sample.cpp
    4.)ui_sample.h
    5.)main.cpp
    6.)square.h
    7.)square.cpp

    From these 5th & 6th are simple .cpp files for calculating square of the number which am passing from sample.cpp. Like getting input from GUI & passing it to square.cpp and displazing the result.

    Am able to set the calculated value from sample.cpp. But i want to set the resultLineEdit->text() from square.cpp to the calculated value.

    I have inherited in square.cpp from ui_sample class, and tried to include ui_sample.h in square.cpp to set text() property, it shows me an error that: -
    {
    square.cpp: In static member function `static int Square::Square_of(int)':
    ./ui_sample.h:34: error: invalid use of member `Ui_Form::resultLineEdit' in stat
    ic member function
    square.cpp:8: error: from this location
    }

    I might be wrong in doing few things. Can anyone please help me out in this problem.

    I will be thankful
    Last edited by Krish; 17th March 2008 at 17:54.

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

    Default Re: Can i set resultLinEdit->text() from outside class ?

    Come on Krish, how many times did I advise you to go through a C++ book? Honestly, this is yet another very basic C++/OOP issue which has nothing to do with Qt. Qt is extremely easy to learn and use ONCE you know C++ and OOP. Without C++ and OOP knowledge you'll be just wasting everyone's precious time...
    J-P Nurmi

  3. #3
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Can i set resultLinEdit->text() from outside class ?

    Hello! Jpn Sir,
    Am telling you sir that i know C++ & OOP Concepts, but only its like am getting confused.
    Anyway sorry for wasting your precious time, but please this time let me know where am i making mistake. Following r the codes: -

    Class Ui_Form is generated by GUI itself. sample.cpp is where all the GUI widgets are handled. From sample.cpp i called Square_of(int) function which is in Square.cpp. When i just returned the calculated value from Square_of(int) to sample.cpp & then used resultLinEdit->text() it worked fine, but when i changed them to: -
    Qt Code:
    1. #ifndef SQUARE_H
    2. #define SQUARE_H
    3.  
    4. #include<iostream>
    5.  
    6. #include "ui_sample.h"
    7.  
    8. class Square:public QWidget, public Ui::Form
    9. {
    10. Q_OBJECT
    11. public:
    12. Square();
    13. static void Square_of(int);
    14. };
    15. #endif
    16.  
    17. ########Square.cpp#######
    18.  
    19. #include<square.h>
    20.  
    21. #include "ui_sample.h"
    22.  
    23. void Square::Square_of( int i )
    24. {
    25. int sq = i*i*i;
    26. Ui_Form *frm = new Ui_Form;
    27. frm->resultLineEdit->setText(QString::number(sq,10));
    28. }
    To copy to clipboard, switch view to plain text mode 

    I am getting the executable but its crashing when called the Square function.

    Again sorry & Thanks in advance for helping

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

    Default Re: Can i set resultLinEdit->text() from outside class ?

    Read this article carefully, please: Using a Component in Your Application.

    To be direct, your code makes no sense. You're already inheriting from Ui::Form and your Square_of() allocates a new Ui::Form every single time it gets called (there's a clear memory leak there). Still, no setupUi() is being called anywhere. Oh, and in addition to that, it sounds like a class which shouldn't be aware of any UI components in the first place.
    J-P Nurmi

  5. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Can i set resultLinEdit->text() from outside class ?

    Thanks Sir,

    Quote Originally Posted by jpn View Post
    Read this article carefully, please: Using a Component in Your Application..
    Sir i have already read this document before using Multiple Inheritance method and then setting up Ui.

    Quote Originally Posted by jpn View Post
    Still, no setupUi() is being called anywhere. Oh, and in addition to that, it sounds like a class which shouldn't be aware of any UI components in the first place.
    In the previous code and this code setupUi() is there in sample.cpp sir, as: -
    Qt Code:
    1. #######sample.cpp#########
    2. #include <QtGui>
    3. #include "sample.h"
    4. #include "square.h"
    5.  
    6. mySample::mySample(QWidget *parent)
    7. :QWidget( parent )
    8. {
    9. setupUi(this); // this sets up GUI
    10. mySample::setWindowTitle(QString("Square the given Number"));
    11.  
    12. connect( startPushButton, SIGNAL( clicked() ), this, SLOT( start() ) );
    13. }
    14.  
    15. void mySample::start()
    16. {
    17. startPushButton->setEnabled( FALSE );
    18. clearPushButton->setEnabled( FALSE );
    19. int i = inputLineEdit->text().toInt();
    20. Square::Square_of(i);
    21. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by jpn View Post
    You're already inheriting from Ui::Form and your Square_of() allocates a new Ui::Form every single time it gets called (there's a clear memory leak there).
    I had tried resultLineEdit->text() instead of
    Ui_Form *frm = new Ui_Form;
    frm->resultLineEdit->setText(QString::number(sq,10));

    but sir it crashed as i think we cant use widgets in a class where Ui is not setup,right?
    As here i have only inherited from Ui::Form class to set the widget.

    If i drop the plan to setup widget's property from outside class, can i do like call a function say set_result() from square.cpp
    As: -
    Qt Code:
    1. ######square.cpp######
    2. #include<square.h>
    3.  
    4. #include "sample.h"
    5. #include "ui_sample.h"
    6.  
    7. void Square::Square_of( int i )
    8. {
    9. int sq = i*i;
    10. mySample::set_result(sq);
    11. }
    To copy to clipboard, switch view to plain text mode 
    Where set_result() is a static function implemented in sample.cpp as: -
    void mySample::set_result(int ans)
    {
    resultLineEdit->setText(QString::number(ans,10));
    }
    But while doing so i got error
    {
    sample.cpp: In static member function `static void mySample::set_result(int)':
    ui_sample.h:34: error: invalid use of member `Ui_Form::resultLineEdit' in static
    member function
    sample.cpp:111: error: from this location
    }

    So is it like i can't setup widgets from static functions?

    To avoid this if i try to create an object of sample.cpp class in square.cpp class for calling the function it will again setup the Ui, right? which is undesirable

    Am Very Sorry Sir as disturbing you so much. But i will be grateful to you Sir if helped.

    Thanks in advance again Sir.
    Last edited by Krish; 18th March 2008 at 13:48.

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

    Default Re: Can i set resultLinEdit->text() from outside class ?

    How about you make the function return the result? Making it aware of certain UI component effectively destroys its reusability.
    J-P Nurmi

  7. The following user says thank you to jpn for this useful post:

    Krish (18th March 2008)

  8. #7
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Smile Re: Can i set resultLinEdit->text() from outside class ?

    Thanks Sir,
    I have tried that only in my previous code as just return the result to sample.cpp and from sample.cpp set text() and it works.

    But in my new program i want to show log information in GUI like when entered the function say Square_of(int) where lot of calculation has to be done like say finding squares of numbers in an array, log should show "We have started Calculation", then as values gets calculated log should update its contents, i mean i will append the strings, but i mean as 1st value gets calculated log should show "First value calculated" and so on.

    So for this i was thinking that as in square.cpp i get to know when the values are calculated then just after each calculation i could send strings like->"First value calculated" to sample.cpp where i will receive them and append to the log.

    I thought as in my example i was returning the result to sample.cpp and showing it in QLineEdit, i could even do it for log.

    If you can suggest me any simple way to do that i will be obliged.

    Thanks in advance again.
    Last edited by Krish; 18th March 2008 at 14:31.

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

    Default Re: Can i set resultLinEdit->text() from outside class ?

    See qInstallMsgHandler(). Try also searching the forums for "qInstallMsgHandler".
    J-P Nurmi

  10. The following user says thank you to jpn for this useful post:

    Krish (18th March 2008)

  11. #9
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Can i set resultLinEdit->text() from outside class ?

    Hello! Jpn Sir,
    Thanks Sir for suggesting me that tool but i think it will print the debug information whereas i want to print the cout's in my file to logwindow.
    Also I searched the forum from where i got to see from here that i can use qdebug.h or qdebugstream.h headers to do the needful.

    In this post Wysota Sir has mentioned
    {Alternatively make your own streaming class that inherits std:: ostream and outputs everything using qDebug or some completely different mechanism. Then undefine cout and substitute it with your own.} So how do i do that? Do i have to use qdebugstream.h and implement my code or something else?

    Also if those headers are used am confused about few things like with: -

    QDebugStream cout(std::cout, QTextEdit* text_edit);
    QDebugStream cerr(std::cerr,QTextEdit* text_edit);

    Will it print all the cout's & cerr's to text_edit, i mean as in my case will it print cout<<"We have started Calculation"( written in my Square.cpp which is subclassed to Sample.cpp) to logTextEdit in my GUI?

    Also will it print to textedit widget added to my GUI or to a separate QTextEdit widget like a popup or QMessageBox or something likethat?

    Thanks again.
    Last edited by Krish; 18th March 2008 at 16:31.

Similar Threads

  1. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 08:07
  2. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 23:37
  3. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 16:05
  4. Replies: 2
    Last Post: 16th March 2007, 10:04
  5. Replies: 2
    Last Post: 4th May 2006, 20:17

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.