PDA

View Full Version : Need some help with QGraphicsTextItem



ayanda83
16th April 2015, 14:52
Hello again, I want to be able to write text in the QGraphicsscene. I want to be able to do this by clicking button that calls a slot that adds a QGraphicsTextItem in the scene and from there i can start typing. Here is my code below. this is the slot function.
void MainWindow::on_font_btn_clicked()
{
text = new TextItem();
text->setParentItem(page_1);
text->setFont(fontCombo->currentFont());
text->setZValue(1000.0);
scene->addItem(text);
}
but for some reason this is not working. NB: this text is to be written within the "rect" of the parent item. Am I missing something here?

ayanda83
17th April 2015, 12:11
Does anybody have any idea what the problem could be here?

wysota
17th April 2015, 13:04
What exactly is "not working"?

ayanda83
17th April 2015, 13:24
The ultimate goal is to be able write some text over a QGraphicsItem (i.e. the parent item to the QGraphicstextItem) but that is not happening with the code I posted above. How it is suppose to work is that, I need to be able to able click a button (i.e. the add text button) and then click on a position on scene to place the cursor and then start typing but none of that is happening with the above code. the above code is implemented in a slot function for the add text button.

Thanking you in advance.

wysota
17th April 2015, 13:37
"none of that"? You mean you are not able to click a button, you are not able to click on the scene or you are not able to start typing? Your code above only adds an item without text to the scene. It does nothing of what you mentioned.

ayanda83
17th April 2015, 14:49
I mean when I click on the button "Add Text" and then click on a position on the scene, I then expect to see a blinking text cursor on that position which will then allow me to start typing the text. That is what I am aiming for. At the moment, when I click on the "Add Text" but and then click on the scene, Nothing happens (i.e. NO TEXT CURSOR IS ADDED TO THE SCENE).

wysota
17th April 2015, 15:00
Why would a text cursor be added to the scene? You are adding an item to the scene but you are not telling the item that you'd like to edit it. QGraphicsItem::setFocus() gives focus to an item.