I have a user interface with:
N.2 QGraphicsView
N.3 QCheckBoxes as shown below:
I am using QWheelEvent and the key CTRL to either zoom in-out inside QGraphicsView (left). This part works well.
I am using QWheelEvent and ALT key to zoom in-out inside QGraphicsView (right). This part has a problem because it is not zooming at all.
When CTRL and ALT are both pressed, both QGraphicsviews should zoom in-out in the same point where I have the mouse but the zoom move both images in strange directions.
The check boxes are set in the following way: As soon as I run the interface the left QGraphicsView is in the Drag Mode already. As I click the checkbox then the QGraphicsView becomes selectable and can do a bunch of different operations. The same for the right QGraphicsView. When the Zoom checkbox is seleceted it does zoom if I draw a rectangle in the area of interests in the right direction of the view. However it does not reset if I draw a rectangle in the left direction of the view:
So in addition to the QCheckbox I want to be able to zoom in-out using QWheelEvent and the CTRL and ALT key and I am not sure if the checkboxes are affecting my QWheelEvent in any way. Or maybe not and the problem is only the way I implemented QWheelEvent.
I am not sure what I am not doing wrong, I read this source and I found it useful enough however it didn't solve the problem. Also this was useful for understanding QGraphicsView::scale() function.
please shed light on this matter. Below the most important source code that describes the problem:
mainwindow.h
private:
bool ctrl_down = false; // to check if CTRL is pressed
bool alt_down = false; // to check if ALT is pressed
protected:
void keyPressEvent(QKeyEvent *event);
void wheelEvent(QWheelEvent *event);
void keyReleaseEvent(QKeyEvent *event);
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->leftView->installEventFilter(this);
ui->rightView->installEventFilter(this);
}
void MainWindow::onRubberBandUpdate(const QRect &viewportRect, const QPointF &fromScenePoint, const QPointF &toScenePoint)
{
if(viewportRect.isNull() && fromScenePoint.isNull() && toScenePoint.isNull())
{
if(ui->zoomSelect->isChecked())
{
ui->leftView->fitInView(QRectF(start, end), Qt::KeepAspectRatio);
ui->rightView->fitInView(QRectF(start, end), Qt::KeepAspectRatio);
}
else
{
// Additional operations
}
}
else
{
start = fromScenePoint;
end = toScenePoint;
}
}
void MainWindow::on_leftSidePan_toggled(bool checked)
{
if(ui->leftSidePan->isEnabled())
{
if(checked)
{
ui->leftView->setDragMode(QGraphicsView::RubberBandDrag);
ui->leftSidePan->setText("Left: Select");
}
else
{
ui->leftView->setDragMode(QGraphicsView::ScrollHandDrag);
ui->leftSidePan->setText("Left: Drag");
}
}
}
void MainWindow::on_rightSidePan_toggled(bool checked)
{
if(ui->rightSidePan->isEnabled())
{
if(checked)
{
ui->rightView->setDragMode(QGraphicsView::RubberBandDrag);
ui->rightSidePan->setText("Right: Select");
}
else
{
ui->rightView->setDragMode(QGraphicsView::ScrollHandDrag);
ui->rightSidePan->setText("Right: Drag");
}
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Control) { ctrl_down = true; }
else if(event->key() == Qt::Key_Alt) { alt_down = true; }
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
if(ctrl_down) // Only left zoom
{
ui->leftView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->leftView->verticalScrollBar()->blockSignals(true);
ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QPointF pos = ui->leftView->mapToScene(event->pos());
double DELTA = 1.0 + event->angleDelta().y() / 1200.0;
QTransform xform = ui->leftView->transform();
xform.translate(pos.x(), pos.y()); // origin to spot
xform.scale(DELTA, DELTA); // scale
xform.translate(-pos.x(), -pos.y()); // spot to origin
ui->leftView->setTransform(xform);
ui->leftView->update();
event->accept();
}
else if(alt_down) // Only right zoom
{
ui->rightView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->rightView->verticalScrollBar()->blockSignals(true);
ui->rightView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
QPointF posr = ui->rightView->mapToScene(event->pos());
double DELTAR = 1.0 + event->angleDelta().y() / 1200.0;
QTransform yform = ui->rightView->transform();
yform.translate(posr.x(), posr.y()); // origin to spot
yform.scale(DELTAR, DELTAR); // scale
yform.translate(-posr.x(), -posr.y()); // spot to origin
ui->rightView->setTransform(yform);
ui->rightView->update();
event->accept();
}
if(alt_down && ctrl_down) // Both zoom
{
ui->leftView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->rightView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
ui->leftView->verticalScrollBar()->blockSignals(true);
ui->leftView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
ui->rightView->verticalScrollBar()->blockSignals(true);
ui->rightView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
// For the Left View
static const double scaleFactorTot = 1.1;
static double currentScaleTot = 1.0; // stores the current scale value.
static const double scaleMinTot = .1; // defines the min scale limit.
// For the Right View
static const double scaleFactorTotR = 1.1;
static double currentScaleTotR = 1.0; // stores the current scale value.
static const double scaleMinTotR = .1; // defines the min scale limit.
if(event->delta() > 0)
{
ui->rightView->scale(scaleFactorTotR, scaleFactorTotR);
currentScaleTotR *= scaleFactorTotR;
ui->leftView->scale(scaleFactorTot, scaleFactorTot);
currentScaleTot *= scaleFactorTot;
}
else if (currentScaleTot > scaleMinTot && currentScaleTotR > scaleMinTotR)
{
ui->rightView->scale(1 / scaleFactorTotR, 1 / scaleFactorTotR);
currentScaleTotR /= scaleFactorTotR;
ui->leftView->scale(1 / scaleFactorTot, 1 / scaleFactorTot);
currentScaleTot /= scaleFactorTot;
}
}
}
void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Control) { ctrl_down = false; }
if(event->key() == Qt::Key_Alt) { alt_down = false; }
}


Aucun commentaire:
Enregistrer un commentaire