Qt mousemoveevent + Qt::LeftButton - qt

Quick question, why does:
void roiwindow::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseMoveEvent(event);
qDebug() << event->button();
}
return 0 instead of 1 when I'm holding down the left mouse button while moving the cursor around in a graphicscene. Is there anyway to get this to return 1 so I can tell when a user is dragging the mouse across the graphicscene. Thanks.

Though Spyke's answer is correct, you can just use buttons() (docs). button() returns the mouse button that caused the event, which is why it returns Qt::NoButton; but buttons() returns the buttons held down when the event was fired, which is what you're after.

You can know if the left button was pressed by looking at the buttons property:
if ( e->buttons() & Qt::LeftButton )
{
// left button is held down while moving
}
Hope that helped!

The returned value is always Qt::NoButton for mouse move events. You can use Event filter to solve this.
Try this
bool MainWindow::eventFilter(QObject *object, QEvent *e)
{
if (e->type() == QEvent::MouseButtonPress && QApplication::mouseButtons()==Qt::LeftButton)
{
leftbuttonpressedflag=true;
}
if (e->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(e);
if(leftbuttonpressedflag && mouseEvent->pos()==Inside_Graphics_Scene)
qDebug("MouseDrag On GraphicsScene");
}
return false;
}
And also don't forget to install this event filter in mainwindow.
qApplicationobject->installEventFilter(this);

Related

QT QLineEdit focusout

I have a QLineedit with mask and qvalidator (subclassed)
How can i prevent to move away the focus if the input isn't match the mask or validator ?
Because neither mask nor qvalidator don't prevent to move away focus from QLineEdit.
And editingfinished isn't work because :
void QLineEdit::editingFinished()
This signal is emitted when the Return or Enter key is pressed or the line edit loses focus. Note that if there is a validator() or inputMask() set on the line edit and enter/return is pressed, the editingFinished() signal will only be emitted if the input follows the inputMask() and the validator() returns QValidator::Acceptable."
void MainWindow:n_lineEdit_editingFinished()
{
if (ui->lineEdit->text() != "1111") ui->lineEdit->setFocus();
}
So mask (an validator) doesn't work together with editingFinsihed signal.
plus i have tried this
bool MainWindow::eventFilter(QObject *filterObj, QEvent *event)
{
if (filterObj == ui->lineEdit ) {
if(event->type() == QEvent::FocusOut) {
if (ui->lineEdit->text() != "1111") { ui->lineEdit-`>setFocus();};
return true;
};
};
return false;
}
thank you Attila
From the Qt's doc:
Note that if there is a validator set on the line edit, the
returnPressed()/editingFinished() signals will only be emitted if the
validator returns QValidator::Acceptable.
But you can set a focus on every event, not only for FocusOut:
bool MainWindow::eventFilter(QObject *filterObj, QEvent *event)
{
if (filterObj == ui->lineEdit )
ui->lineEdit->setFocus();
if(event->type() == QEvent::KeyRelease)
{
QKeyEvent* e = (QKeyEvent*)event;
if(e->key() == Qt::Key_Return
|| e->key() == Qt::Key_Enter)
{
/* do what you want here */
}
}
return QObject::eventFilter(filterObj, event); // usual process other events
}

QPlainTextEdit double click event

I need to capture the double click event on a QPlainTextEdit that is inside a QDockWidget.
In my actual code I have installed an event filter in the QDockWidget, to handle resize operations, and in the QPlainTextEdit, to handle the double click events:
// Resize eventfilter
this->installEventFilter(this);
ui->myPlainTextEdit->installEventFilter(this);
But, although it works for the QDockWidget I am unable to catch the double click event for the QPlainTextEdit:
bool MyDockWidget::eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::Resize && obj == this) {
QResizeEvent *resizeEvent = static_cast<QResizeEvent*>(event);
qDebug("Dock Resized (New Size) - Width: %d Height: %d",
resizeEvent->size().width(),
resizeEvent->size().height());
} else if (obj == ui->myPlainTextEdit && event->type() == QMouseEvent::MouseButtonDblClick) {
qDebug() << "Double click";
}
return QWidget::eventFilter(obj, event);
}
With this code the message "Double click" is never shown. Any idea what is wrong with the code?
QTextEdit inherits a QScrollView and when you double click on the viewport of the QTextEdit, the viewport receives the double click event. You can cross check your current code by double clicking on the edges of the text edit. It will capture the event.
To solve this, add the event filter to the view port in addition to the current event filters you have installed as shown below:
ui->myPlainTextEdit->viewport()->installEventFilter(this);
Next, capture the event using this if statement:
if ((obj == ui->myPlainTextEdit||obj==ui->myPlainTextEdit->viewport()) &&
event->type() == QEvent::MouseButtonDblClick)
{
qDebug() << "Double click"<<obj->objectName();
}
You can capture the click position using QMouseEvent:
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug()<<QString("Click location: (%1,%2)").arg(mouseEvent->x()).arg(mouseEvent->y());

Mouse Events on QGraphicsview and Object Movement

I implemented a Graphicsview in which I load an Image and the user can add different Item such as rectangle or line. The user has also the possibility to move these objects in the scene. Up to here everything is ok.
Now I tried to implement some mouse events in this way:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->graphicsView && enable_mouse_event==true)
{
if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
point_mouse = ui->graphicsView->mapFrom(ui->graphicsView, mouseEvent->pos());
return true;
}
if (event->type() == QEvent::MouseMove)
{
int scroll_x, scroll_y;
scroll_x = ui->graphicsView->horizontalScrollBar()->value();
scroll_y = ui->graphicsView->verticalScrollBar()->value();
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
point_mouse_move = ui->graphicsView->mapFrom(ui->graphicsView, mouseEvent->pos());
QRect rect(point_mouse_move.x()+scroll_x-50, point_mouse_move.y()+scroll_y-50, 100, 100);
zoom_image = image_clone.copy(rect);
ui->zoom_label->setPixmap(QPixmap::fromImage(zoom_image));
return true;
}
}
return false
}
with in MainWindow.cpp
ui->graphicsView->installEventFilter(this);
In this way the press event is recognized while the move event is recognized only on the border of the Qgraphicview object (the property mouseTracking is set to true) and I'm still able to move the other object.
To solve the problem of the move event I tried to modify the code by adding:
->viewport()
to the graphicview object.
In this way both the mouse events (press and move) work correctly but I'm no longer able to move the other objects (rect and line)
Any Idea how can I recognize all the mouse events and at the same time move the other objects??
thanks
The problem was that I was filtering out the events and they never go to the window. Instead of setting the returns true just set the return false!
Thanks to #thuga

Qt 4.4: disabled widgets receiving mouse events

As the title suggests, is there a way for a disabled widget to receive mouse events?
I'm using QWidget::setEnabled() for changing the appearance of widgets but I still want to receive their mouse events. Thanks in advance :)
You can do this with an event filter on the widget in question. See QObject::eventFilter(). Your implementation might look something like this:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (ui->pushButton)
{
if (event->type() == QEvent::MouseButtonRelease)
{
qDebug() << "mouse button";
return true;
} else
{
return false;
}
} else
{
// pass the event on to the parent class
return QMainWindow::eventFilter(obj, event);
}
}
This will work even if the button is disabled.

How to receive mouse press event on clicking QTableWidget?

I'm having a table with 100 datas. I need to receive mouse press event while clicking on the table.
Here is the code:
ui->tableWidget->viewport()->installEventFilter(this);
bool Dbtable::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
debugLog("# TABLE WIDGET IS PRESSED");
return ;
}
}
Now the table is not visible.
Could any one help me in solving this?
have you tried returning QWidget::eventFilter()

Resources