My question is basically the same as this one, but applied to the Qt C++ framework.
I am implementing a popup window by inheriting QWidget with flags Qt::QPopup | Qt::QWindow. I would like this window to be moveable and resizeable, I'm currently achieving this by using the mouse events in the following code:
void TextPopup::mousePressEvent(QMouseEvent* event)
{
offset = event->pos();
QWidget::mousePressEvent(event);
}
void TextPopup::mouseMoveEvent(QMouseEvent* event)
{
if(event->buttons() & Qt::LeftButton)
if(resizeMode) {
QPoint p = mapToGlobal(event->pos()) - geometry().topLeft();
resize(p.x(), p.y());
} else
move(mapToParent(event->pos() - offset));
else {
QPoint diff = geometry().bottomRight() - mapToGlobal(event->pos());
if(diff.x() <= 6 && diff.y() <= 6) {
if(!resizeMode) {
setCursor(Qt::SizeFDiagCursor);
resizeMode = true;
}
} else {
if(resizeMode) {
setCursor(Qt::SizeAllCursor);
resizeMode = false;
}
}
}
}
void TextPopup::mouseReleaseEvent(QMouseEvent* event)
{
offset = QPoint();
QWidget::mouseReleaseEvent(event);
}
I have a few problems with this. For one, I'm guessing there's a better way to do it. And more importantly, I would like the resize symbol at the bottom right as in this image (taken from the post mentioned above). Any suggestions for achieving this?
You can add the resize grip by calling QDialog's or QStatusBar's function setSizeGripEnabled (or directly in QtCreator form designer).
For custom widgets the simplest way probably is to use QSizeGrip. I didn't use it myself but you can check the Qt source code on git for QStatusBar or QDialog.
You can use
QSizeGrip
in a layout inside your widget :The
QSizeGrip
class provides a resize handle for resizing top-level windows. When you set the widget flagQt::SubWindow
, then the user can resize it using the size grip.If you don't want
QSizeGrip
you may take a look at this solution :frameless.h
:frameless.cpp
:And just create an instance and put your
QWidget
: