C++ 当鼠标悬停在边框上时,如何完全禁用调整窗口大小,包括调整大小图标?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16673074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how can I fully disable resizing a window including the resize icon when the mouse hovers the border?
提问by Klasik
I used:
setFixedSize(size());
to stop the window from resizing, but the resize arrows still appear when the mouse is over the border of the window.
我用过:
setFixedSize(size());
来阻止窗口调整大小,但是当鼠标在窗口的边界上时,调整大小的箭头仍然出现。
Is there a better way to disable window resizing to avoid showing the arrows when crossing the border?
有没有更好的方法来禁用窗口大小调整以避免在越过边界时显示箭头?
回答by Dani?l Sonck
Qt has a windowFlag called Qt::MSWindowsFixedSizeDialogHint
for that. Depending on what you exactly want, you want to combine this flag with Qt::Widget
, Qt::Window
or Qt::Dialog
.
Qt 有一个 windowFlag 调用Qt::MSWindowsFixedSizeDialogHint
它。根据您的确切需求,您希望将此标志与Qt::Widget
,Qt::Window
或结合使用Qt::Dialog
。
void MyDialog::MyDialog()
{
setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
...
}
回答by thuga
Try something like this:
尝试这样的事情:
this->statusBar()->setSizeGripEnabled(false);
If this doesn't work, all you need to do is detect what widget is activating QSizeGrip. You can do this by installing an event filter on your app and try to catch the QSizeGrip's mouseMoveEvent. Then debug its parent widget.
如果这不起作用,您需要做的就是检测正在激活QSizeGrip 的小部件。您可以通过在您的应用程序上安装事件过滤器并尝试捕获 QSizeGrip 的 mouseMoveEvent 来实现此目的。然后调试它的父小部件。
Here's an example of the eventFilter function you could use:
这是您可以使用的 eventFilter 函数的示例:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::MouseMove)
{
QSizeGrip *sg = qobject_cast<QSizeGrip*>(obj);
if(sg)
qDebug() << sg->parentWidget();
}
return false;
}
You could probably catch its show event as well, it's up to you.
你也可以赶上它的表演活动,这取决于你。
回答by Neurotransmitter
One-liner if you know exactly the required size of the window:
如果您确切知道所需的窗口大小,则为单行:
this->setFixedSize(QSize(750, 400));
回答by Aaron
I found that calling setSizeConstraint(QLayout::SetFixedSize) on the layout worked the best for me. Specifically, from a QMainWindow constructor, I called:
我发现在布局上调用 setSizeConstraint(QLayout::SetFixedSize) 对我来说效果最好。具体来说,从 QMainWindow 构造函数中,我调用了:
this->layout()->setSizeConstraint(QLayout::SetFixedSize);
Here's a link to the docs: http://doc.qt.io/qt-4.8/qlayout.html#SizeConstraint-enum
这是文档的链接:http: //doc.qt.io/qt-4.8/qlayout.html#SizeConstraint-enum
(I'm using Qt 4.8.)
(我使用的是 Qt 4.8。)
Since this was also a simple way to solve the OP's question, I thought I would share it for others to consider. It appears that there are many ways to accomplish this in Qt, but not all may be ideal for every situation. I tried several of the other options posted here, but they had various issues or constraints that I wasn't happy with in my own situation.
由于这也是解决OP问题的简单方法,因此我想我会分享给其他人考虑。在 Qt 中似乎有很多方法可以实现这一点,但并非所有方法都适用于每种情况。我尝试了这里发布的其他几个选项,但它们有各种问题或限制,我对自己的情况不满意。
回答by BuvinJ
If using Qt Designer, set your window's "sizePolicy" properties to "Fixed" in the vertical and horizontal directions and set minimum and maximum dimensions to equal values. Then, right-click on the window and select "Remove Status Bar" to get rid of the "size grip" in the bottom-right corner. Or, remove just the size grip via the suggestion from francis (rather than the entire status bar).
如果使用 Qt Designer,请将窗口的“sizePolicy”属性在垂直和水平方向设置为“Fixed”,并将最小和最大尺寸设置为相等的值。然后,右键单击窗口并选择“删除状态栏”以删除右下角的“大小夹点”。或者,通过 francis 的建议(而不是整个状态栏)仅删除尺寸手柄。
回答by Tushar
Use
用
setMinimumSize(QSize(width_px,height_px))
setMinimumSize(QSize(width_px,height_px))
setMaximumSize(QSize(width_px,height_px))
setMaximumSize(QSize(width_px,height_px))
where both methods have samesize.You won't see the resize cursor & the window thus doesn't get resized/maximized.
其中两种方法具有相同的大小。您不会看到调整大小的光标,因此窗口不会被调整大小/最大化。
回答by ramsudharsan
If you wish to obtain the values of width and height from the UI form itself without manually specifying, then you can add the following command inside your project class:
如果您希望在不手动指定的情况下从 UI 表单本身获取宽度和高度的值,那么您可以在您的项目类中添加以下命令:
this->setFixedSize(this->width(), this->height());
You can also set separate parameters for width and height (if required) with:
您还可以为宽度和高度(如果需要)设置单独的参数:
this->setFixedWidth(this->width());
this->setFixedHeight(this->height());
回答by Rachael
This helped me with Qt Creator 3.1.1:
这对我使用 Qt Creator 3.1.1 有帮助:
this->setFixedSize(this->maximumSize());
回答by Boris Ivanov
You can in Qt5 use following code
您可以在 Qt5 中使用以下代码
this->setMinimumSize(sz);
this->setMaximumSize(sz);
Where sz is QSize object.
其中 sz 是 QSize 对象。
回答by GorudoYami
Also you can just do something like:
您也可以执行以下操作:
this->setFixedWidth(int);
this->setFixedHeight(int);
The arrows are gone too.
箭也不见了。