Qt:调整无边框小部件的大小
我的问题基本上与这个问题相同,但适用于Qt C++ 框架。
我通过继承带有标志 Qt::QPopup | 的 QWidget 来实现弹出窗口Qt::QWindow。我希望这个窗口是可移动和可调整大小的,我目前通过在以下代码中使用鼠标事件来实现这一点:
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);
}
我对此有一些问题。一方面,我猜有更好的方法来做到这一点。更重要的是,我想要右下角的调整大小符号,如图所示](取自上面提到的帖子)。对于实现这一目标有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想要
QSizeGrip
您可以看看这个解决方案:frameless.h
:frameless.cpp
:只需创建一个实例并放置您的
QWidget
: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
:您可以通过调用 QDialog 或 QStatusBar 的函数 setSizeGripEnabled 添加调整大小夹点(或直接在 QtCreator 表单设计器中)。
对于自定义小部件,最简单的方法可能是使用 QSizeGrip。我自己没有使用它,但你可以在 git 上检查 Qt 源代码 QStatusBar 或 QDialog。
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.
您可以在小部件内的布局中使用
QSizeGrip
:QSizeGrip
类提供了用于调整顶级窗口大小的调整大小句柄。当您设置小部件标志Qt::SubWindow
时,用户可以使用大小手柄调整它的大小。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.