Qt:带有可移动小部件的窗口

发布于 2024-12-14 23:48:51 字数 158 浏览 1 评论 0原文

我想创建一个具有可变数量的自定义小部件的窗口,每个小部件都可以是一组一些简单的对象。小部件的行为应类似于对话框窗口:它们可以移动到父窗口内的任何位置,可以重叠等。但是,它们不能移出窗口。用 QT3 实现这样的事情最合适的方法是什么?我尝试以主窗口作为父窗口创建窗口/对话框,但这样可以将小部件移出窗口。

I want to create a window with a variable number of custom widgets, each of the widgets can be a group of some simple objects. Widgets should behave similar to dialog windows: they can be moved anywhere inside the parent window, can overlap etc. However, they can't be moved out of the window. What would be the most appropriate way to implement something like this with QT3? I tried creating windows/dialogs with the main window as parent, but this way the widgets can be moved out of the window.

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

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

发布评论

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

评论(1

櫻之舞 2024-12-21 23:48:51

您也许可以使用拖放功能。这是我唯一能想到的。

Qt 附带了一个名为 Fridge Magnets 的示例。基本上,他们创建了一个名为 DragWidget 的类,它是所有其他小部件的宿主。在示例中,他们创建了大量带有单词的 QLabels,用户可以拖动这些单词来创建句子。

主要工作是实现以下功能。

void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);

这个例子是用Qt4引入的,但它应该给你一个想法,代码是这里 基本上,当按下鼠标

时,鼠标下的控件将被隐藏,并且图像将放置在拖动数据中。当用户将其放置在控件中的某个位置时,该示例将关闭前一位置的控件并创建一个新的 QLabel 并将其放置在用户放置的位置。您可能应该更改它以将控件移动到该位置。

该示例使用的一些概念与 3.3 中的概念略有不同,因此 这里是 3.3 中的拖放示例,

主要问题是当用户拖动时显示什么。在第一个示例中,他们使用了一个很好的技巧来构建标签的图像,因此看起来确实像是用户正在拖动小部件。

显然,在 Qt4 中使用 MDI 功能会容易得多,但如果这不是一个选择,这是我唯一能想到的。

希望有帮助。

You might be able to make use of the drag-and-drop functionality. That's the only thing I can think of.

There is an example that comes with Qt called Fridge Magnets. Basically they created a class called DragWidget which is the host of all the other widgets. In the example they create a slew of QLabels with words on them which the user can drag around to create sentences.

The main job is to implement the following functions.

void dragEnterEvent(QDragEnterEvent *event);
void dragMoveEvent(QDragMoveEvent *event);
void dropEvent(QDropEvent *event);
void mousePressEvent(QMouseEvent *event);

This example was introduced with Qt4, but it should give you the idea, the code is here

Basically, when the mouse is pressed the control under the mouse is hidden, and an image is placed in the drag data. When the user drops it somewhere in the control the example closes the one at the previous location and creates a new QLabel and puts it where the user dropped. You should probably change that to just move your control to that location.

The example makes use of a few concepts that are a little different than what was available in 3.3, so here is a drag and drop example from 3.3

The main issue is what to show when the user is dragging around. In the first example, they use a nice trick to build an image of the label, so it really looks like the user is dragging the widget around.

Obviously, it would be much easier to use the MDI functionality in Qt4, but if that's not an option, this is the only thing I can think of.

Hope that helps.

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