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.
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.