PDA

View Full Version : Trouble with QGraphicsTextItem events



Flippik
30th July 2008, 12:57
Hi2all! I need to work some event when user clicks or gets mouse over the QGraphicsTextItem. I'm trying to do it this way,but nothing happens/events do not work.


// myScene.h
#include <QSound>
#include <QGraphicsScene>

class myScene : public QGraphicsScene
{
Q_OBJECT
public:
móScene(QObject *parent = 0);
QSound *menuSound,*cSound;

private slots:
void start();
};
//myScene.cpp
myScene::myScene(QObject *parent) : QGraphicsScene(parent)
{
QPalette p;
setBackgroundBrush(QPixmap(":/data/bg.png"));
QGraphicsTextItem* stLabel3 = addText(trUtf8("smth"));
//stLabel3->setHtml(trUtf8("smth"));
stLabel3->setFlag(QGraphicsItem::ItemIsMovable);
stLabel3->setPos(275,260);
stLabel3->setDefaultTextColor(QColor(0,43,0,127));
stLabel3->setFont(QFont("Comic Sans MS", 24, QFont::Bold,QFont::StyleItalic));
stLabel3->adjustSize();
stLabel3->setZValue(1);
QTransform transform = stLabel3->transform();
transform.rotate(80.0, Qt::YAxis);
transform.scale(2., 1.5);
stLabel3->setTransform(transform);
stLabel3->setTextInteractionFlags(Qt::LinksAccessibleByMouse );
connect(stLabel3,SIGNAL(linkActivated(const QString&)),this,SLOT(start()));


menuSound = new QSound("data/sound/menu.wav");
cSound = new QSound("data/sound/mus_w1.wav");
menuSound->play();
}


void myScene::start()
{
menuSound->stop();
cSound->play();
}

salmanmanekia
30th July 2008, 13:08
I am not sure..but i would have declared my QGraphicsItem in the header file instead of constructor ....:)

Flippik
30th July 2008, 13:52
There is nothing with it...

salmanmanekia
30th July 2008, 14:01
hmm..so only the event is not working or the whole screen is empty .!!

Flippik
30th July 2008, 15:29
Only the event. QGraphicsTextItem creates. When I select it, dotted frame round QGraphicsTextItem is created, but nothing more happens.
The same event with QPushButton works well.
connect(button,SIGNAL(clicked()),this,SLOT(start() ));
But I need just QGraphicsTextItem.

kghose
30th July 2008, 17:39
Hi Flippik,

I've done something similar with QGraphicsPixmapItem. The code is at

http://chhobi.cvs.sourceforge.net/chhobi/ChhobiQT/photo.h?view=markup

The class is called ThumbnailItem.

The file photo.cpp has the implementation.

Perhaps that example will help.

-K

aamer4yu
30th July 2008, 18:04
connect(stLabel3,SIGNAL(linkActivated(const QString&)),this,SLOT(start()));

Dont you think SIGNAL and SLOT should have same signature ??;)

salmanmanekia
30th July 2008, 18:30
Dont you think SIGNAL and SLOT should have same signature ??

correct me if i am wrong but slot can have shorter signature than signal...:cool:

Benne Gesserit
30th July 2008, 23:52
Hi ,
is the signal realy emitted?

void QGraphicsTextItem::linkActivated ( const QString & link ) [signal]
This signal is emitted when the user clicks on a link on a text item that enables Qt::LinksAccessibleByMouse or Qt::LinksAccessibleByKeyboard. link is the link that was clicked.

Maybe it s not emitted if you click on text that is not a link .
Hope it helps.

Flippik
31st July 2008, 07:27
How can I check if the signal is emitted?
I've tried to do it as a link(stLabel3->setHtml("<a href=http://www.ya.ru>hello!!!</a>")),but nothing has changed..I've read the Assistant help for QGraphicsTextItem class reference and found only two signals that could be generated(linkActivated, linkHovered). Is it really to do this way or my efforts are useless?
kghose: I've not found anything useful for me...

Benne Gesserit
1st August 2008, 22:20
If the reference says that linkActivated is emitted when......that means that by default it s emitted when some event occurs ......
Ok ,I am doing my own test.... linkHovered is emited if you set Html to the item (I did :

setHtml("<A href=../imagenes/bosque.gif>mapa del bosque encantado.</A>");

)
With plain text or html without links ,nothing happens.
Same thing with linkActivated .
Could it be that your slot is private?Also make sure the names of signals and slots are ok .
Hope you fix it.

Flippik
4th August 2008, 10:48
Benne Gesserit: 10x u a lot. This trouble was 'cause my function was private, when i had changed it to public, everything was OK.
And the next question: how can I get off a grey frame, that is created when I choose a QGraphicsTextItem?

Benne Gesserit
4th August 2008, 23:33
My pleasure .I am very happy to help you . :)
I have also been working with QGraphicsTextItem recently and found the same problem : I didnt like that grey frame .But it wasnt enough disturbing for me ,so I didnt spend time solving that .
I dont know what s the best solution .And I am pretty sure this one I will post isnt the best solution,but you can try it.
I guess somewhere you call QGraphicsTextItem:: paint() ,or you just leave it all to QGraphicsTextItem .What I did is reimplementing paint to "temporary disable somethings" :

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
QWidget *widget){
setFlag(QGraphicsItem::ItemIsSelectable , false);
setFlag(QGraphicsItem::ItemIsFocusable , false);
QGraphicsTextItem::paint(painter ,option ,widget);
setFlag(QGraphicsItem::ItemIsSelectable , true);
setFlag(QGraphicsItem::ItemIsFocusable , true);

}

:D ,I find it very rude ,and I dont know if it can have collateral efects .Maybe not since it s just while the item is being painted.But I dont know .I was just triying to disable this in QGraphicsTextItem:: paint()

if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
qt_graphicsItem_highlightSelected(this, painter, option);

I dont know how much this works.I tested it I little bit and I didnt see the the grey frame ,but maybe the interaction with the item fails .
Hopefully someone else with more experience can give us (I want to know too)a better solution .