SkripT
10th January 2006, 15:24
Hi all, I have subclassed QScrollArea because I want to draw a selection rectangle with QRubberband when the user clicks the left mouse button on the viewport. With the code below, the program does exactly what I want (the rubberband is well drawn respect the mouse icon and the scrollbars of the scroll area moves when the user is making a selection and is at the margins of the viewport). The only problem is that the rubberband is painted respect the scrollarea and what I want is paint it respect the widget of the scroll area. So now the rubber band is always painted in the same position even if the scroll bars are moved. Can anybody tell me how to I have to modify the code below to draw the rubberband respect the position of the widget of the scroll area? Thanks
void FotoAreaEditorFotos::mousePressEvent(QMouseEvent *event)
{
if (event -> button() != Qt::LeftButton) return;
iniSeleccioX = event -> globalX();
iniSeleccioY = event -> globalY();
areaSeleccio = new QRubberBand(QRubberBand::Rectangle);
areaSeleccio -> setGeometry(iniSeleccioX, iniSeleccioY, 1, 1);
areaSeleccio -> show();
}
void FotoAreaEditorFotos::mouseMoveEvent(QMouseEvent *event)
{
int x, y, w, h;
QScrollBar *scrollBar;
if (event ->buttons() == Qt::LeftButton)
{
int auxX = event -> globalX();
int auxY = event -> globalY();
QPoint posMax = mapToGlobal(QPoint(viewport() -> width() - 1,
viewport() -> height() - 1));
QPoint posMin = mapToGlobal(QPoint(0 , 0));
if (auxX < iniSeleccioX)
{
if (auxX <= posMin.x())
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
x = posMin.x();
}
else
x = auxX;
w = iniSeleccioX - x + 1;
}
else
{
if (auxX >= posMax.x())
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
w = posMax.x() - iniSeleccioX + 1;
}
else
w = auxX - iniSeleccioX + 1;
x = iniSeleccioX;
}
if (auxY < iniSeleccioY)
{
if (auxY < posMin.y())
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
y = posMin.y();
}
else
y = auxY;
h = iniSeleccioY - y + 1;
}
else
{
if (auxY > posMax.y())
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
h = posMax.y() - iniSeleccioY + 1;
}
else
h = auxY - iniSeleccioY + 1;
y = iniSeleccioY;
}
areaSeleccio -> setGeometry(x, y, w, h);
}
}
void FotoAreaEditorFotos::mouseReleaseEvent(QMouseEvent *event)
{
if (event -> button() == Qt::LeftButton)
delete areaSeleccio;
}
void FotoAreaEditorFotos::mousePressEvent(QMouseEvent *event)
{
if (event -> button() != Qt::LeftButton) return;
iniSeleccioX = event -> globalX();
iniSeleccioY = event -> globalY();
areaSeleccio = new QRubberBand(QRubberBand::Rectangle);
areaSeleccio -> setGeometry(iniSeleccioX, iniSeleccioY, 1, 1);
areaSeleccio -> show();
}
void FotoAreaEditorFotos::mouseMoveEvent(QMouseEvent *event)
{
int x, y, w, h;
QScrollBar *scrollBar;
if (event ->buttons() == Qt::LeftButton)
{
int auxX = event -> globalX();
int auxY = event -> globalY();
QPoint posMax = mapToGlobal(QPoint(viewport() -> width() - 1,
viewport() -> height() - 1));
QPoint posMin = mapToGlobal(QPoint(0 , 0));
if (auxX < iniSeleccioX)
{
if (auxX <= posMin.x())
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
x = posMin.x();
}
else
x = auxX;
w = iniSeleccioX - x + 1;
}
else
{
if (auxX >= posMax.x())
{
scrollBar = horizontalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
w = posMax.x() - iniSeleccioX + 1;
}
else
w = auxX - iniSeleccioX + 1;
x = iniSeleccioX;
}
if (auxY < iniSeleccioY)
{
if (auxY < posMin.y())
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() - scrollBar -> singleStep());
y = posMin.y();
}
else
y = auxY;
h = iniSeleccioY - y + 1;
}
else
{
if (auxY > posMax.y())
{
scrollBar = verticalScrollBar();
scrollBar -> setValue(scrollBar -> value() + scrollBar -> singleStep());
h = posMax.y() - iniSeleccioY + 1;
}
else
h = auxY - iniSeleccioY + 1;
y = iniSeleccioY;
}
areaSeleccio -> setGeometry(x, y, w, h);
}
}
void FotoAreaEditorFotos::mouseReleaseEvent(QMouseEvent *event)
{
if (event -> button() == Qt::LeftButton)
delete areaSeleccio;
}