PDA

View Full Version : QGraphicsTextItem in a Widget ,Is it Possible ??



salmanmanekia
23rd July 2008, 11:57
Hi,
I have written this code in my class derived from QWidget,but it shows no text ..!!


class ProgressWidget:public QWidget
{
..
QGraphicsTextItem *txt1;
..
};



void ProgressWidget::instructionStringOne()
{
txt1->setPlainText(str1);
txt1->setFont(fontStr1);
txt1->setTextWidth(TEXT_WIDTH);
txt1->setPos(INSTRUCTION_TEXT_1_X,INSTRUCTION_TEXT_1_Y);
txt1->show();

}

should it work ot this is a mistake....i have also tried different values in setPos but it shows no text..:)

caduel
23rd July 2008, 12:19
You can use QGraphicsItems (and subclasses) only in a QGraphicsView (or -Scene for that matter).

Just use a QLabel.

salmanmanekia
23rd July 2008, 13:54
Thanks,i was also thinking the same...but wasnt sure about it..
any ways when i used the QLabel ,i faced a problem that my text is not fully shown on the screen until and unless i introduce some spacing in the text here is the code..


#define SECOND_TRAINING_COMMAND "Do as you would want to do normally.Be accurate an' 'd avoid mistakes."
QLabel *txt1;

the spacing shown in the above string is necessary or it does not shows the whole string..

str2 = SECOND_TRAINING_COMMAND;
txt2 = new QLabel(this,Qt::SubWindow);
txt2->setText(str2);
txt2->setFont(fontStr2);
txt2->setGeometry(INSTRUCTION_TEXT_2_X,INSTRUCTION_TEXT_ 2_Y,TEXT_WIDTH,TEXT_WIDTH);
txt2->setWordWrap(TRUE);
txt2->show();

salmanmanekia
23rd July 2008, 19:43
any help ?? :confused:

aamer4yu
23rd July 2008, 19:48
What does it show when u dont give the space ??

salmanmanekia
23rd July 2008, 19:52
it just shows the first part of the string i.e the part which is before spaces and and the last word of the string on the next line,..in my case which is

Do as you would want to do normally.Be accurate an
mistakes.

aamer4yu
23rd July 2008, 19:57
What happens if u simply use -


str2 = SECOND_TRAINING_COMMAND;
txt2 = new QLabel(this);
this->setCentralWidget(txt2); // assuming this is QMAinWindow
txt2->setText(str2);

// and from main()
mainWindow->show();


May be somethings wrong when u are setting geometry

salmanmanekia
24th July 2008, 08:28
this seems to be more confusing,in my case i want the text to be print on a widget ,i am not sure how it works on mainwindow :)

salmanmanekia
24th July 2008, 09:35
problem solved but i didnt understand how it works..!!


txt2->setGeometry(INSTRUCTION_TEXT_2_X,INSTRUCTION_TEXT_ 2_Y,TEXT_WIDTH,TEXT_WIDTH);
to clarify ,i just reduce the TEXT_WIDTH from 450 to 350 and it worked...
any idea y ...??

jpn
27th July 2008, 16:21
How about using layouts (http://doc.trolltech.com/4.4/layout.html). Your calculation for TEXT_WIDTH (350px) might happen to "work" on your screen, but just imagine if somebody else has a different font...

salmanmanekia
28th July 2008, 13:45
ya ,this seems logical ,i will work on it ,thanks for pointing it out,