I have created a set of wizard pages inherited from qwizardpage. in one of my wizard page i have a graphics view and in that using graphics scene and a qrect a rectangle has been created and now i am trying to catch the callback event of mouse move when the mouse is in the graphicsview area, but outside this area only call back is happening. But mouse press event is functioning properly. could anyone suggest me any solution.
WizardPage::WizardPage(QWidget* parent)
{
m_pScene = new QGraphicsScene(this);
graphicsView->setScene(m_pScene);
m_pRect = new QRect(-25, 25, 100, 40);
m_pScene->addRect(*m_pRect);
setMouseTracking(true);
}
void EgoLeverWizardPage::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "move";
}
Related
How to create it in qt?
When you click on button - should be shown popup widget and its width should be = button width.
And if main window (main form) drag to another place on the screen - popup widget should continuously follow the button (must be attached to the bottom border of the button).
before click image:
after click image:
Create widget, don't put it any layout, set it's parent to button's parent (lets call it "host"), set window flags to Qt::Window | Qt::FramelessWindowHint
mPopup = new QWidget(this);
mPopup->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
Override host's resizeEvent and moveEvent and adjust popup's geometry there using button's geometry.
void Host::adjustPopup() {
if (!mPopup->isVisible()) {
return;
}
QRect rect = mButton->geometry();
QPoint bottomLeft = this->mapToGlobal(rect.bottomLeft());
mPopup->setGeometry(QRect(bottomLeft, QSize(rect.width(),200)));
}
void Host::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
adjustPopup();
}
void Host::moveEvent(QMoveEvent *event)
{
QWidget::moveEvent(event);
adjustPopup();
}
full source: button-popup
I am creating a video application.
Upon starting this app, you would see a VideoWidget looping a playlist along with other widgets in the screen. By clicking the VideoWidget, the VideoWidget will go to fullscreen mode and a volume slider will be laid over it. It would go to show normal if clicked again in fullscreen mode.
To do this I created 2 classes. First, I created a main class that would contain all widgets including the Video widget. Second, I created a custom VideoWidget class. I instatiated my Qslider in this VideoWidget class and I instantiated a VideoWidget Object in my main class whose object is instantiated in main.cpp.
I got what I expect it to do. Except that the slider would not update its position immediately. It would only update position if you click to show normal then click to go back fullscreen. The volume change but the position of slider in UI does not change while in fullscreen.
I would like to ask what am I doing wrong? What should I do so that the slider position would update in UI?
Code Snippet:
in VideoWidget.h
class VideoWidget : public QVideoWidget
{
Q_OBJECT
QVideoWidget* videoWidget;
QMediaPlaylist* playlist;
QMediaPlayer *player;
public:
VideoWidget();
QSlider* slider;
};
In VideoWidget.cpp
VideoWidget::VideoWidget()
: videoWidget(new QVideoWidget(this)),
slider(new QSlider(Qt::Horizontal, this))
{
/*QMediaplaylist *playlist, QMediaPlayer *player instantiated here*/
slider->hide();
slider->setGeometry(300,735,600,20);
slider->setRange(0, 100);
slider->setValue(player->volume());
connect(slider, &QSlider::valueChanged, player, &QMediaPlayer::setVolume);
}
void VideoWidget::changeEvent(QEvent *event)
{
if(event->type() == QEvent::WindowStateChange)
slider->setVisible(windowState() == Qt::WindowFullScreen);
QWidget::changeEvent(event);
}
enter code here
void VideoWidget::resizeEvent(QResizeEvent* event) {
videoWidget->resize(size());
event->accept();
}
void VideoWidget::mousePressEvent(QMouseEvent *event)
{
this->setFullScreen(!isFullScreen());
event->accept();
}
In the MainWidget.cpp
mainwidget::mainwidget(QWidget *parent)
: QWidget(parent)
{
videoWidget = new VideoWidget(); // the video container
videoWidget->setFixedSize(500, 300);
QBoxLayout *displayLayout = new QHBoxLayout;
displayLayout->addWidget(videoWidget, 2);
QBoxLayout *layout = new QVBoxLayout;
layout->addLayout(displayLayout);
setLayout(layout);
videoWidget->setGeometry(100,100,300,400);
videoWidget->show();
}
edit:
This is the app at startup playing a video of my hand.
When I click the Video,
The video sets to fullscreen and the slider appears. The slider can control the volume of mediaplayer but the problem is, it won't move when dragged.
You're very confusing in ways you describe what the problem is, but if I get you right that QSlider doesn't track the mouse but you can change volume with it, presumably with a click?
You had connected valueChangedsignal , which is emitted only after sliderReleased() if tracking property is false. You have to handle Pressed\Moved\Released group of signals if you want to adjust volume continuously, or you can use built-in function of QSlider (which usually is enough):
slider->setTracking(true);
I have a custom QGraphicsScene in which I have a mouseMoveEvent(QGraphicsSceneMouseEvent *event);
When I hover on the scene with the mouse, the mouseMoveEvent gets properly fired. However when I hover with a mouse button pressed, then it does not get fired anymore.
This is how I setup the whole scene in the main window:
scene = new NodeScene(this); -> My Custom QGraphicsScene class
scene->setSceneRect(QRectF(0, 0, 5000, 5000));
QHBoxLayout *layout = new QHBoxLayout;
view = new QGraphicsView(scene);
layout->addWidget(view);
view->setDragMode(QGraphicsView::RubberBandDrag);
view->setMouseTracking(true);
QWidget *widget = new QWidget;
widget->setLayout(layout);
setCentralWidget(widget);
scene->setCentralWidget(widget);
And here is the code where I do handle mouse events (it's for Maya execution):
void NodeScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Move");
QGraphicsScene::mouseMoveEvent(event);
}
void NodeScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Press");
QGraphicsScene::mousePressEvent(event);
}
void NodeScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
MGlobal::displayInfo("Release");
QGraphicsScene::mouseReleaseEvent(event);
}
Any idea how I can get the mouseMoveEvents even when a mouse button is pressed ?
It sounds like you're not implementing all of the mouse events, just the mouseMoveEvent.
When overriding a mouse event, you should handle all of them (move, press and release events).
You can then set a boolean in the press event to know whether or not the mouse button is held down when entering the mouseMove event.
Found the issue by comparing my code to other examples, and it is due to the setDragMode(QGraphicsView::RubberBandDrag);
line. The code should by default be on QGraphicsView::NoDrag and the RubberBandDrag be enabled only upon press.
Hello in my application i'm making a QGraphicsView and set the scene on it:
QGraphicsProxyWidget *rotateItemIcon;
HoverFilter *hv = new HoverFilter(); // my hover filter class
connect(hv,SIGNAL(SignalHover(QObject*)),this,SLOT(ObjectHover(QObject*)));
connect(hv,SIGNAL(SignalHoverLeave(QObject*)),this,SLOT(ObjectHoverLeave(QObject*)));
ui->TestIcon->installEventFilter(hv);
...
scene = new QGraphicsScene(this);
scene->setSceneRect(0, 0, 661, 255);
ui->TestIcon->setParent(NULL);
rotateItemIcon = scene->addWidget(ui->TestIcon); // here i add my control to the scene and receive QGraphicsProxyWidget object
rotateItemIcon->setTransformOriginPoint(ui->TestIcon->width()/2,
ui->TestIcon->height()/2);
ui->graphicsViewFive->setScene(scene); //QGraphicsView on my form
ui->graphicsViewFive->show();
my HoverFilter.cpp
#include "hoverfilter.h"
#include "QDebug"
HoverFilter::HoverFilter()
{
}
bool HoverFilter::eventFilter( QObject *dist, QEvent *event )
{
if( event->type() == QEvent::Enter )
{
emit SignalHover(dist);
return true;
}
if( event->type() == QEvent::Leave )
{
emit SignalHoverLeave(dist);
return true;
}
return false;
}
rotateItemIcon is my QGraphicsProxyWidget and the problem is that it has weird boundaries, i need to implement some animation on hover of my control TestIcon, (i done that using event filter) mouse enter and mouse leave fires when i drag my mouse on a random places, not only on my TestIcon control. If do not add my control to the QGraphicsScene hover detection works fine, so i assume this is a scene/proxywidget problem. Is there a way i can set size or boundaries to QGraphicsProxyWidget to stop that?
I'm not sure if I completely understand your question, but it sounds like you're have a widget placed in a scene via a QGraphicsProxyWidget and when you try to detect if your mouse is over the widget you want it to animate.
By 'weird boundaries' I assume you mean the extents to which the proxy widget is accepting the mouse as being over the widget. If so, I suggest either calling setGeometry on the QGraphicsProxyWidget to set its position and dimensions, or inherit from QGraphicsProxyWidget and implement the boundingRect function to define the area of the proxy widget.
I'm having troubles forcing a repaint/update of my Qt Widget (it extends the QGraphicsView class). What I want is a rectangular selection box to be drawn which will highlight the target selection area as the user presses and moves the mouse.
The basic workflow:
MousePressEvent sets the making_selection_box flag, and stores the start point (working).
MouseMoveEvent checks to see if the display needs to be updated. If it does, it tries to do so (not working).
MouseReleaseEvent handles gets the resulting selection box and handles it accordingly. making_selection_box is reset. Screen should be updated to remove the selection box artifact (not working).
The overrided mouseMoveEvent:
void QSchematic::mouseMoveEvent(QMouseEvent *event)
{
if(making_selection_box)
{
// get selection box
qDebug() << "updating selection box";
curr_selection_end = event->pos();
repaint(box(drag_select_start, curr_selection_end));
}
// propogate event
QGraphicsView::mouseMoveEvent(event);
}
My overrided paintEvent:
void QSchematic::paintEvent(QPaintEvent *event)
{
qDebug() << "paintEvent";
if(making_selection_box)
{
qDebug() << "drawing selection box";
QPainter painter(viewport());
painter.setPen(Qt::black);
painter.drawRect(box(drag_select_start, curr_selection_end));
painter.end();
}
// propogate event
QGraphicsView::paintEvent(event);
}
Box is just a small helper function I wrote to create the correct QRect for different selection box start/end points.
static QRect box(const QPoint& p1, const QPoint &p2)
{
int min_x = p1.x();
int min_y = p1.y();
int max_x = p2.x();
int max_y = p2.y();
if(max_x < min_x)
{
max_x = min_x;
min_x = p2.x();
}
if(max_y < min_x)
{
max_y = min_y;
min_y = p2.y();
}
return QRect(min_x, min_y, max_x - min_x, max_y - min_y);
}
I've verified that mouseMoveEvent is being triggered correctly when the user presses a button and moves the mouse around.
I've also verified that paintEvent is being called by the system when I perform various standard operations such as resize the window, minimize/maximize it, etc.
I've verified that the method I'm using to paint to my widget will work correctly with other paintEvent triggers, I just can't manage to trigger a repaint in my code.
I've also tried forcing the update by using the update() method instead of repaint(), but no luck.
As a side note, am I going about creating this selection box functionality the wrong/hard way? Is there a better way to get a selection box without having to manually implement the mouse listeners and painting code?
I'm testing with Qt 4.8.4 on Windows 7 x64, using the Visual Studio 2010 MSVC compiler.
After looking through the QGraphicsScene API I found an easy workaround for having to manually manage the selection box: The drag mode needs to be set to RubberBandDrag.
edit:
To further expand my answer which allows painting on the QGraphicsView for other purposes, it's the viewport which needs to receive the update/redraw, not my QGraphicsView object.
void QSchematic::mouseMoveEvent(QMouseEvent *event)
{
if(making_selection_box)
{
// get selection box
qDebug() << "updating selection box";
curr_selection_end = event->pos();
viewport()->repaint(box(drag_select_start, curr_selection_end));
}
// propogate event
QGraphicsView::mouseMoveEvent(event);
}