找回密码
 立即注册
收起左侧

如何在多文档界面中平铺部件(翻译)

0
回复
7186
查看
[复制链接]
累计签到:1570 天
连续签到:1 天
来源: 转载 2013-5-6 10:03:55 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
原文链接

In Qt you can create multiple document interface (MDI) applications using a QMdiArea as the central widget. The QMdiArea widget serves as a window manager for the MDI windows. There are already functions available for tiling all the child windows into a tile pattern and for cascading them into a cascade pattern.

在Qt中可以通过将QMdiArea作为中心部件来创建多文档界面。QMdiArea部件可以看做是MDI窗口的窗口管理器。而且现在已经有了现成的函数来将所有的子窗口进行平铺或者重叠。

With MDI application it would sometimes be useful to tile the child windows also horizontally and vertically. The QMdiArea does not currently have an API to achieve this functionality but it’s quite simple to arrange the child windows in your application. We will next cover the steps needed to tile the child windows horizontally and vertically. The approach presented here is implemented on top of the MDI Example (http://qt-project.org/doc/qt-5.0/qtwidgets/mainwindows-mdi.html).

但是,在MDI应用程序中有时需要将子窗口进行横向或者纵向平铺。现在QMdiArea还没有现成的API来实现这个功能,不过在程序中排列这些子窗口是很简单的。下面我们将一步一步来实现将子窗口进行水平或者垂直平铺。相关源码可以查看这里


Let’s start by adding the actions in place so that the functionality can be triggered. To the MainWindow we will add one action for each function as private member variable:
  1. QAction *tileVerticalAct;
  2. QAction *tileHorizontalAct;
复制代码
We will also add a private slot for each function:
  1. void tileSubWindowsVertically();
  2. void tileSubWindowsHorizontally();
复制代码
Then we will initialize the actions and connect them to the right slots. We are going to add the following code to the createActions() method that already initializes actions for the application:
  1. tileVerticalAct = new QAction(tr("Tile Vertically"), this);
  2. tileVerticalAct->setStatusTip(tr("Tile the windows vertically"));
  3. connect(tileVerticalAct, SIGNAL(triggered()), this, SLOT(tileSubWindowsVertically()));
  4. tileHorizontalAct = new QAction(tr("Tile Horizontally"), this);
  5. tileHorizontalAct->setStatusTip(tr("Tile the windows horizontally"));
  6. connect(tileHorizontalAct, SIGNAL(triggered()), this, SLOT(tileSubWindowsHorizontally()));
复制代码
And naturally we need to add the actions to the menu in order to launch the tiling. We can do this in the updateWindowMenu() where other actions are added to the menu too:
  1. windowMenu->addAction(tileVerticalAct);
  2. windowMenu->addAction(tileHorizontalAct);
复制代码
Now we are ready to start tiling the child windows. We are going to take a closer look at the tileSubWindowsVertically() slot here. To begin with, we will start by checking that there are child windows in the MDI area to prevent us from trying to do modifications to non-existing windows.
  1. if (mdiArea->subWindowList().isEmpty())
  2.              return;
复制代码
We are going to initialize a rectangle that defines the size of one child window. The width for a child window will naturally be the width of the MDI area. The height of a child window will be based on the height of the MDI area and the amount of child windows. Basically, we will just divide the MDI area height with the amount of child windows. The rectangle can then be used to set the geometry for each child widget in the MDI area.
  1. QPoint position(0, 0);
  2. foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
  3. QRect rect(0, 0, mdiArea->width(), mdiArea->height() / mdiArea->subWindowList().count());
  4. window->setGeometry(rect);
  5. window->move(position);
  6. position.setY(position.y() + window->height());
  7. }
复制代码
While we are setting the geometry for the child widgets we also define the new position for them to have the windows tiled vertically. The child windows are ordered here based on the order of the child windows in the MDI area. By default, this order is the order in which the windows were inserted into the workspace. We will move the first window in the MDI area to the top left corner. While processing the child windows we will adjust the position so that each child window is position below the previously processed one. This is all we need to tile the child windows vertically.




Tiling the child windows horizontally will be almost the same; you only need to adjust the width and x-position of the window. The height of a child window will be the same as the height of the MDI area. The position of a child window will be on the right of the previous child.
  1. void MainWindow::tileSubWindowsHorizontally()
  2. {
  3. if (mdiArea->subWindowList().isEmpty())
  4.                      return;
  5. QPoint position(0, 0);
  6. foreach (QMdiSubWindow *window, mdiArea->subWindowList()) {
  7. QRect rect(0, 0, mdiArea->width() / mdiArea->subWindowList().count(), mdiArea->height());
  8. window->setGeometry(rect);
  9. window->move(position);
  10. position.setX(position.x() + window->width());
  11. }
  12. }
复制代码
Not that hard or what do you think? If you want to give it a try you can get the source code for the example here: Source code for example application . Shall you have any questions related to this you can always contact us in the Qt Support team via the Customer Portal.

参与人数 1人气 +2 收起 理由
Alone + 2 <font style="vertical-align: inh

查看全部评分总评分 : 人气 +2

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了