Qt 中具有固定大小的不可调整大小的 QDialog?

发布于 2024-07-16 10:43:39 字数 698 浏览 9 评论 0原文

我有一个 Qt 对话框应用程序。 现在我不希望该对话框可调整大小。 我不知道如何实现这一目标。 我尝试了很多方法,但当对话框启动时,仍然可以调整该对话框的大小。

我应该设置什么属性来禁用对话框/小部件调整大小。

我也尝试过,

setSizePolicy(QSizePolicy::Fixed);

但我收到一条错误消息,

source\nimcac_settingsMain.cpp(36) : error C2248:
**'QSizePolicy::QSizePolicy' : cannot access private member declared in class 'QSizePolicy'**
        p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(177) : see declaration of 'QSizePolicy::QSizePolicy'
        p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(34) : see declaration of 'QSizePolicy'

请帮我解决这个问题。

I have a Qt dialog application. Now I dont want that dialog to be resizeable. I am not sure how to achieve this. I tried a bunch of things but still when the dialog launches this dialog can be resized.

What is the property that i should set to disable the dialog/Widget resize.

I also tried

setSizePolicy(QSizePolicy::Fixed);

But i get an error saying..

source\nimcac_settingsMain.cpp(36) : error C2248:
**'QSizePolicy::QSizePolicy' : cannot access private member declared in class 'QSizePolicy'**
        p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(177) : see declaration of 'QSizePolicy::QSizePolicy'
        p:\ThirdPartyExports\Qt\export\4.3\4.3.1f14\include\QtGui\../../src\gui\
kernel\qsizepolicy.h(34) : see declaration of 'QSizePolicy'

Kindly help me out with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

安人多梦 2024-07-23 10:43:39

您收到的编译错误是因为您尝试将 QSizePolicy::Policy 传递给 setSizePolicy(QSizePolicy),但没有从 QSizePolicy::Policy< 进行隐式转换。 /code> (这是一个维度的策略)到 QSizePolicy (这是一个包含每个维度(高度、宽度)一个 Policy 等内容的类)。 不过,QSizePolicy 无论如何都不适用于顶级小部件(窗口)。

setFixedSize() 仅在您提前知道对话框的大小时才有效(通常您不会,因为更改字体大小和语言)。 您可以这样做

window()->setFixedSize( window()->sizeHint() );

,但最好使用

window->layout()->setSizeConstraint( QLayout::SetFixedSize );

它让布局确定对话框的大小,但不允许调整大小,我认为这就是您所要求的。

The compile error you get is because you try to pass a QSizePolicy::Policy to setSizePolicy(QSizePolicy), but there's no implicit conversion from QSizePolicy::Policy (which is the policy for one dimension) to QSizePolicy (which is a class containing, among other things, one Policy per dimension (height, width)). QSizePolicy doesn't work on top-level widgets (windows) anyway, though.

setFixedSize() only works if you know the size of the dialog in advance (and usually you don't, what with changing font sizes and languages). You can do

window()->setFixedSize( window()->sizeHint() );

but it's much better to use

window->layout()->setSizeConstraint( QLayout::SetFixedSize );

That lets the layout determine the size of the dialog, but doesn't allow resizing, which I assume is what you were asking for.

上课铃就是安魂曲 2024-07-23 10:43:39

我不知道你是否已经尝试过,但是 QWidget::setFixedSize 应该做你想做的

I don't know if you already tried it, but QWidget::setFixedSize should do what you want

唔猫 2024-07-23 10:43:39
this->setFixedSize(this->width(),this->height());
this->setFixedSize(this->width(),this->height());
南街女流氓 2024-07-23 10:43:39

您需要更改对话框的windowFlags并将其设置为Qt::MSWindowsFixedSizeDialogHint。

这只适用于Windows

有关更多信息,请参阅此示例:
http://doc.qt.digia.com/4.5/widgets-windowflags。 html

You need to change the windowFlags of the dialog and set it to Qt::MSWindowsFixedSizeDialogHint.

This only works in windows.

For more information please see this example:
http://doc.qt.digia.com/4.5/widgets-windowflags.html

三生一梦 2024-07-23 10:43:39

在 QT Creator 的 UI 编辑器中,单击属性窗口中的顶部对象,然后滚动到布局部分的底部。 您应该看到layoutSizeConstraint 属性。

layoutSizeConstraint 设置为 SetFixedSize

On QT Creator, in the UI editor, click on the top object in the properties window, then scroll at the bottom in the Layout part. You should see the layoutSizeConstraint property.

Set the layoutSizeConstraint to SetFixedSize.

剪不断理还乱 2024-07-23 10:43:39

如果您使用 QtCreator(当然是),您可以将 Horizo​​ntalsizePolicy 属性设置为固定,并将垂直策略也设置为固定。 然后你可以将maximumSize设置为你想要的尺寸。 窗口不会再次最大化。

If you use QtCreator (of course you are) you can set the property HorizontalsizePolicy to fixed and Vertical Policy also to Fixed. Then you can set the maximumSize to the dimensions you want. The window will not maximise again.

在代码中你可以做这样的事情,

Dialog->resize(581, 292);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(WaterLevelEditorDialog->sizePolicy().hasHeightForWidth());
Dialog->setSizePolicy(sizePolicy);
Dialog->setMinimumSize(QSize(581, 292));
Dialog->setMaximumSize(QSize(581, 292));
Dialog->setSizeGripEnabled(false);

在QtCreator中执行如下操作,

  1. 选择对话框小部件
  2. 在对象窗口中找到对话框小部件
  3. 在对象窗口中,右键单击对话框对象弹出菜单
  4. 选择“大小约束”-> 菜单中的“设置最大尺寸”
  5. 再次右键单击对话框对象,弹出菜单
  6. 选择“尺寸约束” -> “设置最小尺寸”
  7. 在属性窗口中 ,
    • 确保“sizePolicy”->“水平策略”的值为“固定”
    • 确保“sizePolicy”->“Vertical Policy”的值为“Fixed”
    • 确保未选中“sizeGripEnabled”

In code you can do something like this,

Dialog->resize(581, 292);
QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(WaterLevelEditorDialog->sizePolicy().hasHeightForWidth());
Dialog->setSizePolicy(sizePolicy);
Dialog->setMinimumSize(QSize(581, 292));
Dialog->setMaximumSize(QSize(581, 292));
Dialog->setSizeGripEnabled(false);

In QtCreator do as follows,

  1. Select the dialog box widget
  2. Locate the dialog box widget in Object Window
  3. In Object Window, right click on dialog box object to popup a menu
  4. Select "Size Constraints" -> "Set Maximum Size" from the menu
  5. Right click again on dialog object to popup a menu
  6. Select "Size Constraints" -> "Set Minimum Size"
  7. In Property Window,
    • ensure "sizePolicy"->"Horizontal Policy" has value "Fixed"
    • ensure "sizePolicy"->"Vertical Policy" has value "Fixed"
    • ensure "sizeGripEnabled" is not checked
自由如风 2024-07-23 10:43:39

从 Qt 文档来看,setSizePolicy() 方法要么采用零个参数,要么采用两个参数,但不能是一个参数。 这就是您收到此编译错误的原因。 根据我的实验,如果不设置固定大小。 这个方法没有什么用。 窗口的大小仍然可以调整。

From the Qt documentation, setSizePolicy() method either takes zero argument or two arguments but cannot be one argument. That's why you get this compilation error. From my experiment, if you don't set the fixed size. This method has no use. The window can still be resizable.

╰つ倒转 2024-07-23 10:43:39

更简单的方法是将最大大小设置为 0。

this->setMaxiumSize(QSize(0, 0));

An easier way is to set the maxium size to 0.

this->setMaxiumSize(QSize(0, 0));
简单爱 2024-07-23 10:43:39

如果您正在 QML 中设计 UI 并使用 QDeclarativeView 启动,请尝试以下代码。

QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
.
.
.
//To make the window non-resizable
viewer->setFixedSize(viewer->width(),viewer->height());

这里QmlApplicationViewer是从QDeclarativeView派生的。

In case you are designing UI in QML and launching using QDeclarativeView, try the below code.

QScopedPointer<QmlApplicationViewer> viewer(QmlApplicationViewer::create());
.
.
.
//To make the window non-resizable
viewer->setFixedSize(viewer->width(),viewer->height());

Here QmlApplicationViewer is derived from QDeclarativeView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文