PDA

View Full Version : Multiple QTextEdit instanciations and EventFilter



Yaoming
13th February 2014, 15:53
Hello there !
I've got a QPushButton linked to a slot that instantiates a QTextEdit each time you click on it (in fact, this slot only manages a boolean, my textEdit is instantiated in the mousePressEvent. The user is able to put it wherever he wants). My problem is that I need to use an eventFilter and it only works once. For example if I instantiate 3 textEdits, it'll work on only one of them (this is not related to the order in which they were instantiated)...
I've got the same problem for a KeyPressEvent which is also in my eventFilter function. And in my eventFilter, my textEdit only detects right clicks, how can I do to make it detect the left click as well?
(I've tried to do a static-cast of the event to QMouseEvent and make a condition on the button pressed but it's not working ).
Here's my code:


//Instantiation
void ProjetGeometrie::mousePressEvent(QMouseEvent* evt)
{
if(clickTxt) //if the boolean is true (the slot connected to the pushButton manages that)
{
txt = new QTextEdit(this); //I instantiate my textEdit (declared in the .h)
txt->installEventFilter(this);
txt->move(evt->pos().x(), evt->pos().y()); //the textedit will be created at the position of the click
setMouseTracking(true); //to allow the textEdit to be moved without having to keep the left button pressed
txt->setContextMenuPolicy(Qt::NoContextMenu); //to disable the right-click context menu
txt->show();
}
}

//eventFilter
if(obj == txt && e->type() == QEvent::MouseButtonPress) //if the user clicks on the textEdit
{
QMouseEvent* me = static_cast <QMouseEvent*> (e);
if(!txtSelectionne)//if the textEdit wasn't already "selected" then we select it
txtSelectionne = true;

else //if it was, we "deselect" it
txtSelectionne = false;
return true;
}

if(obj == txt && e->type() == QEvent::KeyPress)
{
QKeyEvent* ke = static_cast <QKeyEvent*> (e);
if(ke->key() == Qt::Key::Key_Delete)
{
delete txt; //deletes the textEdit
txt = NULL;
return true;
}
}

return false;

//MouseMoveEvent
void ProjetGeometrie::mouseMoveEvent(QMouseEvent* evt)
{
if(txtSelectionne) //if a textedit is selected, then we make it follow the mouse until the user clicks a second time (without having to keep the left button pressed)
txt->move(evt->pos().x(), evt->pos().y());

update();
}



To sum up, I've got two questions: - How to make my eventFilter work on every single textEdit created?
- How to detect a left-button click on a textEdit?
Thanks in advance for your replies,
Cheers.

anda_skoa
13th February 2014, 19:19
I am not sure what you are having problems with.

You install the event filter on all text edits created in mousePressEvent.
You only check events for the last created one.

What exactly is not working?

Cheers,
_

Yaoming
13th February 2014, 21:14
If I have three text edits, the event filter only works for one of them.
The eventfilter is there to tell my mouseMoveEvent when to start making the textEdit move. This only works on the first textEdit I right-click on. I can click on the others as many times as I want, they won't move.

anda_skoa
13th February 2014, 21:25
Your event filter explicitly checks for one object.
It gets the events for all objects it is installed on but just returns false for most of them.

Cheers,
_

Yaoming
14th February 2014, 19:22
Why does it return false event when I do the right-click (normally launching the eventfilter)? I don't understand...

anda_skoa
14th February 2014, 20:13
I am afraid I don't understand the question.

The event filter is returning false unless it hits one of the two if conditions.

Cheers,
_

Yaoming
14th February 2014, 20:56
Never mind, thanks for your answers. I'll try and find the solution by myself. Too hard to explain on paper.

Yaoming
15th February 2014, 14:04
I've found something new about my problem today. With the code I gave, the event filter only works on the last textedit created (if I have 5 textEdits, it'll only work on the fifth one). As if there was an override somewhere.
I don't know where that might come from, any ideas?

anda_skoa
15th February 2014, 16:20
I've found something new about my problem today. With the code I gave, the event filter only works on the last textedit created (if I have 5 textEdits, it'll only work on the fifth one).


Fun fact: I said that in comment #2



I don't know where that might come from, any ideas?

Fun fact: I explained that in comment #2 and comment #4

Cheers,
_

Yaoming
15th February 2014, 19:26
Sorry but I don't know is how to correct it. I understand what you're saying but how to make the event filter check all the textEdits? I'm lost.

Yaoming
16th February 2014, 09:34
I read "If multiple event filters are installed on a single object, the filter that was installed last is activated first" in the documentation. Is there any way to change that?

anda_skoa
16th February 2014, 12:38
Sorry but I don't know is how to correct it. I understand what you're saying but how to make the event filter check all the textEdits? I'm lost.

Your check compare the event receiver object "obj" with a pointer stored in a member variable called "txt".
That variable can obviously only point to one of the text edits.

Remove that part of the check.

Cheers,
__

Yaoming
16th February 2014, 14:28
Thanks, I didn't think about that^^