PDA

View Full Version : why we have need of QApplication



Krishnacins
10th April 2006, 12:16
Hi,
Im using QFont & QFontmatrics class only

this my program

QApplication a(argc,argv);

QFont font( strFname, iFsize) ;
font.setBold(true) ;
font.setUnderline(true) ;
font.setItalic(true) ;
font.setStrikeOut(true) ;
int iHeight , iWidth;
QFontMetrics fm(font);
QRect rct = fm.boundingRect(str);

iHeight = rct.height();
iWidth = rct.width();

But I dont want to use QApplication a(argc,argv) class or variable..........

Can i excute this programe without using QApplication a(argc,argv) & How?????

Thanks & Regards
Krishna

nupul
10th April 2006, 12:21
Answer: If you are using a GUI app...u can't do without a Qapplication.

I suggest you first read the docs. I presume you have Qt assistant on your system...run it and read what is said about QApplication...try doing all that without using this class :eek:

If you are using a non gui class...then using Qt can be just considered to be a refinement of C++

Krishnacins
10th April 2006, 12:26
Thanks for reply.
im using attached program, nothing else.
I think there is no GUI application.But its showing error LIKE:::


: undefined reference to `QFontMetrics::QFontMetrics[in-charge](QFont const&)'

: undefined reference to `QFontMetrics::boundingRect(QString const&, int) const'

: undefined reference to `QFontMetrics::~QFontMetrics [in-charge]()'

Thanks & regards
Krishna

nupul
10th April 2006, 12:52
first paste your code between "["code"]"...."[/"code"]" blocks...(without the quotes)

Since you haven't pasted your actual code, i presume you haven't included the files
QApplication, QFont and QFontMetrics...

Krishnacins
10th April 2006, 13:23
This is my actual cod

"
#include <qapplication.h>
#include <iostream.h>
#include <string.h>
#include <qfont.h>
using namespace std;

int GetPixOfChar(QString str , QString strFname, int iFsize)
{
QFont font( strFname, iFsize) ;
font.setBold(true) ;
font.setUnderline(true) ;
font.setItalic(true) ;
font.setStrikeOut(true) ;
int iHeight , iWidth;
QFontMetrics fm(font);
QRect rct = fm.boundingRect(str);

iHeight = rct.height();
iWidth = rct.width();

return iWidth;
}
int main( int argc, char **argv )
{
QApplication a(argc,argv);
QString str= "i";
QString Fname = "Times";
int size = 14;
int iWidth = GetPixOfChar(str , Fname , size);
return 0;
}

"

Please help me

Thanks & Regards
Krishna

wysota
10th April 2006, 14:44
Did you actually link with Qt library? :)

Chicken Blood Machine
10th April 2006, 18:18
Can i excute this programe without using QApplication a(argc,argv) & How?????

Thanks & Regards
Krishna

Nope. You must use QApplication, QApplication does many things including initialising the window system. How are you going to use fonts without initialising the window system?

What are your objections to using QApplication a(argc,argv) ?

nupul
12th April 2006, 06:48
do the following:
1. Read the docs
2. I feel your constructors are not in the correct form:


QFont ()
QFont ( const QString & family, int pointSize = -1, int weight = -1, bool italic = false )
QFont ( const QFont & font, QPaintDevice * pd )
QFont ( const QFont & font )


These are from my qt4 doc...i don't think this has changed since qt3...but you can confirm

3. Read a Qt book...whenever you create a QApplication obj, you also need to call show and exec functions:



//include files

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

// your code...

app.show() //actually shows the app window on screen!

return app.exec(); //you don't return 0, when using Qt!!
}


4. you are creating font etc objs on the stack....thus you need to pass them by reference to the constructors above and not by value.

5. You "seldom" need to use C++ in its true form with Qt...eg when you wish to use "cout" etc...try understanding qt and recoding ur prog...

6. If there is no harm upgrade to qt4! please

7. enclose your code in [ code] [ / code] blocks

8. comment your code!!

Chicken Blood Machine
12th April 2006, 07:14
do the following:
2. I feel your constructors are not in the correct form:

His constructors are fine as far as I can see.


3. Read a Qt book...whenever you create a QApplication obj, you also need to call show and exec functions:

You don't - not unless your program has a GUI or you specifically need an event loop.
BTW, QApplication does not have a show() method.:p


4. you are creating font etc objs on the stack....thus you need to pass them by reference to the constructors above and not by value.

They are reference parameters. The way he is passing them is correct.


5. You "seldom" need to use C++ in its true form with Qt...eg when you wish to use "cout" etc...try understanding qt and recoding ur prog...

What on earth is C++'s "True Form"?:( An effective programmer will make use of the best of Qt as a library and use standard C++.


8. comment your code!!
Good advice, but the OP hardly needs comments for the little snippet that he posted.

@Krishnacins It really looks like you need to #include <qfontmetrics.h>

nupul
12th April 2006, 08:12
sorry CBM...my mistake...think i answered too hastily....apologies for the mistake w.r.t show and exec...forgot to mention the need of a GUI/event-loop

nupul
12th April 2006, 08:18
What on earth is C++'s "True Form"?:( An effective programmer will make use of the best of Qt as a library and use standard C++.)

true form...hmmm...i dunno how to put it, lets say he doesn't use qt at all in it! ;)
but what you said is technically correct...i'll remember that!



Good advice, but the OP hardly needs comments for the little snippet that he posted.


It'd just help someone understand...why he's doing whatever he wishes to do...no matter how small the code!

Chicken Blood Machine
12th April 2006, 17:37
sorry CBM...my mistake...think i answered too hastily....apologies for the mistake w.r.t show and exec...forgot to mention the need of a GUI/event-loop

No worries. I just wanted to make sure that the poster didn't get confused.