PDA

View Full Version : Can i set resultLinEdit->text() from outside class ?



Krish
17th March 2008, 16:49
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:)

jpn
17th March 2008, 19:11
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...

Krish
18th March 2008, 09:32
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: -


#ifndef SQUARE_H
#define SQUARE_H

#include<iostream>

#include "ui_sample.h"

class Square:public QWidget, public Ui::Form
{
Q_OBJECT
public:
Square();
static void Square_of(int);
};
#endif

########Square.cpp#######

#include<square.h>

#include "ui_sample.h"

void Square::Square_of( int i )
{
int sq = i*i*i;
Ui_Form *frm = new Ui_Form;
frm->resultLineEdit->setText(QString::number(sq,10));
}


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

Again sorry & Thanks in advance for helping:)

jpn
18th March 2008, 11:13
Read this article carefully, please: Using a Component in Your Application (http://doc.trolltech.com/4.3/designer-using-a-component.html).

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.

Krish
18th March 2008, 12:43
Thanks Sir,


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.


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


#######sample.cpp#########
#include <QtGui>
#include "sample.h"
#include "square.h"

mySample::mySample(QWidget *parent)
:QWidget( parent )
{
setupUi(this); // this sets up GUI
mySample::setWindowTitle(QString("Square the given Number"));

connect( startPushButton, SIGNAL( clicked() ), this, SLOT( start() ) );
}

void mySample::start()
{
startPushButton->setEnabled( FALSE );
clearPushButton->setEnabled( FALSE );
int i = inputLineEdit->text().toInt();
Square::Square_of(i);
}



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


######square.cpp######
#include<square.h>

#include "sample.h"
#include "ui_sample.h"

void Square::Square_of( int i )
{
int sq = i*i;
mySample::set_result(sq);
}

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:confused:

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

Thanks in advance again Sir.

jpn
18th March 2008, 12:57
How about you make the function return the result? Making it aware of certain UI component effectively destroys its reusability.

Krish
18th March 2008, 13:22
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.

jpn
18th March 2008, 13:49
See qInstallMsgHandler() (http://doc.trolltech.com/latest/qtglobal.html#qInstallMsgHandler). Try also searching the forums for "qInstallMsgHandler".

Krish
18th March 2008, 15:12
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 (http://www.qtcentre.org/forum/f-qt-programming-2/t-console-replacement-6361.html/?highlight=qInstallMsgHandler) 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.:)