I have a QScene object in QWidget and inside QWidget I have QGraphicsView. I convert images to QPixMap give it to QScene as an element and I defined touch events in QGraphicsView class. In QGraphicsView's creator method I enabled touch events with:
viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
and I am managing touch event by overriding ViewPortEvent method:
bool DicomView::viewportEvent(QEvent *event)
{
if(event->type() == QEvent::TouchBegin)
{
QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
.......
return QGraphicsView::viewportEvent(event);
}
PS: DicomView is type of QGraphicsView.
My problem is when I run the application I can get the touch inputs from the view but when get to QView can not get touch inputs from QPixMap. I tried putting the methods inside QScene instead of QGraphicsView but QScene does not have a ViewPortEvent method. What am I supposed to do?
Related
I've overrode the eventFilter method of the MainWindow of the application
bool MainWindow::eventFilter(QObject *obj, QEvent *event){
if(event->type()== QEvent::MouseButtonRelease){
cout<<"CATCH"<<endl;
}
return QObject::eventFilter(obj,event);
}
I can get all events thrown by QWidgets except events arose by QPushButton and Widgets that implement the click event,I mean if I click on the background I can get the release event,if i click
on a QLabel or a QWidget Container i still get the event, but I can't get mouse events from QPushButton, QCalendar, GroupBox etc...
I've tried to promote (I'm using QtCreator) the QPushButton overriding both the methods
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
But even this does not work because these methods are not called when clicking on the QPushButton.
So I've overrode the filter method of the button and I can catch only graphic events like PaletteChangeEvent or RepaintingEvent.
What am I doing wrong?
You have to install an EventFilter in your buttons and QWidgets to receive the events that are sent to those objects before them.
By doing that you can define a method that receive all the events that are sent to those objects and you can catch the ones you want.
Read this to see an example: Event Filters
I am trying to call some function (or slot) when the mouse leaves the space of my QListView (tableView). Normally, you could use the leaveEvent() function. So for example I could write
void MainWindow::leaveEvent(QEvent * event){
qApp->quit();
}
This works as intended. When the mouse leaves the MainWindow widget, the application quits. However, what if I wanted to quit the application when the mouse leaves my QListView object which is INSIDE of my MainWindow widget?
How do I reimplement a function for this QListView when it was created within Qt Creator's form designer?
Here is what I have (unsuccessfully) tried:
void Ui::tableView::leaveEvent(){
qApp->quit();
}
And below, I have tried using leaveEvent() as a signal, and it says leaveEvent is undefined (can you even use events as SIGNALs?)
connect(ui->tableView, SIGNAL(leaveEvent(QEvent *event)), this, SLOT(testSlot()));
Basically, I am trying to call some function when the mouse leaves my tableView which was created with Qt Creator's form designer. The QListView class seems to have a mouseEntered() SIGNAL, but not mouseLeave() SIGNAL.
Subclass QListView and reimplement the leaveEvent (example):
class MyListView : public QListView
{
Q_OBJECT
void MyListView::leaveEvent(QEvent *e){
QListView::leaveEvent(e);
anyOtherAction();
}
}
I have created the Ui screen named HomeScreen.ui in Qt. This screen have no of different widget like QPushButton, QLabel etc. I want change the default navigation, for that I was trying to capture the keyEvents using keyPressedEvent function
void HomeScreen::keyPressEvent(QKeyEvent *event )
{
int keyCode = event->key();
qDebug() << keyCode;
}
but this is captuting event only when the focus is on homescreen, if the focus is on any child widget like push button keyPressEvent is not getting called. I want to capture event on child widgets so that i can write the navigation for them.
Can anybody tell me how to do that?
Is there a way to show a popup when the user right clicks on an empty portion of the scene?
I'm new at Qt and I've tried slots and subclassing, but to no avail.
No such slot and, respectively:
"error: 'QMouseEvent' has not been declared"
when trying to implement the onMouseRelease event.
QGraphicsView is the widget used for displaying the contents of the QGraphicsScene. So the correct place to implement the context menu (popup menu) is the QGraphicsView.
You need to reimplement the contextMenuEvent function is your own class inherited from QGraphicsView:
void YourGraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
QMenu menu(this);
menu.addAction(...);
menu.addAction(...);
...
menu.exec(event->globalPos());
}
See also the Qt's Menus Example.
You can re-implement the contextMenuEvent method of the QGraphicsScene class, which will give you access to the scene coordinates as well as the screen coordinates (as opposed to QGraphicsView, which also works but doesn't have this information):
void YourGraphicsScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
// event->scenePos() is available
QMenu menu(this);
menu.addAction(...);
menu.addAction(...);
...
menu.exec(event->screenPos());
}
In my program, I'd like to have mouseMoveEvent(QMouseEvent* event) called whenever the mouse moves (even when it's over another window).
Right now, in my mainwindow.cpp file, I have:
void MainWindow::mouseMoveEvent(QMouseEvent* event) {
qDebug() << QString::number(event->pos().x());
qDebug() << QString::number(event->pos().y());
}
But this seems to only be called when I click and drag the mouse while over the window of the program itself. I've tried calling
setMouseTracking(true);
in MainWindow's constructor, but this doesn't seem to do anything differently (mouseMoveEvent still is only called when I hold a mouse button down, regardless of where it is). What's the easiest way to track the mouse position globally?
You can use an event filter on the application.
Define and implement bool MainWindow::eventFilter(QObject*, QEvent*). For example
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
statusBar()->showMessage(QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y()));
}
return false;
}
Install the event filter when the MainWindows is constructed (or somewhere else). For example
MainWindow::MainWindow(...)
{
...
qApp->installEventFilter(this);
...
}
I had the same problem, further exacerbated by the fact that I was trying to call this->update() to repaint the window on a mouse move and nothing would happen.
You can avoid having to create the event filter by calling setMouseTracking(true) as #Kyberias noted. However, this must be done on the viewport, not your main window itself. (Same goes for update).
So in your constructor you can add a line this->viewport()->setMouseTracking(true) and then override mouseMoveEvent rather than creating this filter and installing it.