QDockWidget初始宽度

发布于 2024-07-06 15:55:19 字数 69 浏览 13 评论 0原文

如何设置 QDockWidget 的初始宽度?

我已经实现了 sizeHint 函数,但是接下来怎么办?

How do I set the initial width of a QDockWidget?

I have implemented the sizeHint function but what next?

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

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

发布评论

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

评论(2

忆梦 2024-07-13 15:55:19

QDockWidget 的文档说:

QDockWidget 充​​当其子窗口小部件的包装器,通过 setWidget() 设置。 自定义尺寸提示、最小和最大尺寸以及尺寸策略应在子窗口小部件中实现。 QDockWidget 将尊重它们,调整其自身的约束以包括框架和标题。 不应在 QDockWidget 本身上设置大小约束,因为它们会根据它是否停靠而变化; 停靠的 QDockWidget 没有框架和较小的标题栏。

因此,大小提示取自您放入停靠小部件中的任何内容。 您是否尝试过设置QDockWidget子级的大小?

但是,我同意 Marius 的观点,最好的办法可能是在应用程序启动时使用 QSettings 保存和恢复所有停靠窗口的宽度。 看一下 QMainWindow::saveState

除了从 saveState 而不是从各个函数获取数据之外,我的保存函数看起来与 马吕斯

The documentation for QDockWidget says:

A QDockWidget acts as a wrapper for its child widget, set with setWidget(). Custom size hints, minimum and maximum sizes and size policies should be implemented in the child widget. QDockWidget will respect them, adjusting its own constraints to include the frame and title. Size constraints should not be set on the QDockWidget itself, because they change depending on wether it is docked; a docked QDockWidget has no frame and a smaller title bar.

So the size hint is taken from whatever you put in the dock widget. Have you tried setting the size of the QDockWidget's child?

But, I agree with Marius, the best thing to do is probably to use QSettings to save and restore the widths of all the dock windows when the application starts. Have a look at QMainWindow::saveState.

Apart from getting the data from saveState rather than from individual functions, my save function looks very similar to the one given by Marius.

独木成林 2024-07-13 15:55:19

如果您希望它的宽度与上次程序运行时的宽度相同,您应该查看设置。 Qt 文档在此处提供了有关如何使用设置的示例。

我就是这样做的:

void Application::readSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  libraryDock->setFloating(settings.value("docked").toBool());
  libraryDock->resize(settings.value("size", QSize(1, 1)).toSize());
  libraryDock->move(settings.value("pos", QPoint(200, 200)).toPoint());
  addDockWidget((Qt::DockWidgetArea)settings.value("dockarea", Qt::RightDockWidgetArea).toInt(), libraryDock);
  settings.endGroup();
}

void Application::writeSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  settings.setValue("dockarea", dockWidgetArea(libraryDock));
  settings.setValue("docked", libraryDock->isFloating());
  settings.setValue("size", libraryDock->size());
  settings.setValue("pos", libraryDock->pos());
  settings.endGroup();
}

If you want it to have the same width as the same last time the program was running, you should look into settings. The Qt documentation has an example on how to use settings here.

This is how I have done it:

void Application::readSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  libraryDock->setFloating(settings.value("docked").toBool());
  libraryDock->resize(settings.value("size", QSize(1, 1)).toSize());
  libraryDock->move(settings.value("pos", QPoint(200, 200)).toPoint());
  addDockWidget((Qt::DockWidgetArea)settings.value("dockarea", Qt::RightDockWidgetArea).toInt(), libraryDock);
  settings.endGroup();
}

void Application::writeSettings() {
  QSettings settings("Company Name", "Application Name");
  settings.beginGroup("LibraryDock");
  settings.setValue("dockarea", dockWidgetArea(libraryDock));
  settings.setValue("docked", libraryDock->isFloating());
  settings.setValue("size", libraryDock->size());
  settings.setValue("pos", libraryDock->pos());
  settings.endGroup();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文