PyQt QWidget的绝对位置(PyQt absolute position of QWidget)

编程入门 行业动态 更新时间:2024-10-27 12:31:29
PyQt QWidget的绝对位置(PyQt absolute position of QWidget)

我有一个QDialog ,它有一个布局,在这个布局中有多个QWidgets 。 现在我有一个按钮,如果用户按下这个按钮,我想显示一个“工具提示”,它显示更多信息。 这个“工具提示”必须包含一个布局。

为此我想使用一个绝对位置的QWidget 。 当我把这个问题中提到的代码放在一起时,解决方案对我来说不起作用。 我也尝试使用QtWidget.raise_()函数,但QWidget没有被显示。

我将我的代码剥离到以下示例中:

# creating content layout = QtWidgets.QVBoxLayout() for i in range(0, 10): layout.addWidget(QtWidgets.QLabel("line {} - bla bla bla bla bla bla".format(i))) widget = QtWidgets.QWidget() widget.setLayout(layout) # creating dialog and apply the layout dialog = QtWidgets.QDialog() dialog.setLayout(layout) # creating the absolute positioned widget absolute_layout = QtWidgets.QVBoxLayout() absolute_layout.addWidget(QtWidgets.QLabel("Absolute positioned QWidget")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute = QtWidgets.QWidget(dialog) absolute.setLayout(absolute_layout) # show the absolute widget and move it absolute.show() absolute.move(10, 10) absolute.raise_() dialog.show()

该对话框与layout的内容正确显示,但absolute不显示。

我究竟做错了什么?

I have a QDialog which has a layout, in this layout there are multiple QWidgets. Now I have a button, if the user presses this button I want to display a "tooltip" which displays some more information. This "tooltip" has to contain a layout.

For this I wanted to use a QWidget with an absolute position. When I put together the code mentioned in this question the solution does not work for me. I have also tried to use the QtWidget.raise_() function but the QWidget is not being displayed.

I stripped down my code to the following example:

# creating content layout = QtWidgets.QVBoxLayout() for i in range(0, 10): layout.addWidget(QtWidgets.QLabel("line {} - bla bla bla bla bla bla".format(i))) widget = QtWidgets.QWidget() widget.setLayout(layout) # creating dialog and apply the layout dialog = QtWidgets.QDialog() dialog.setLayout(layout) # creating the absolute positioned widget absolute_layout = QtWidgets.QVBoxLayout() absolute_layout.addWidget(QtWidgets.QLabel("Absolute positioned QWidget")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute_layout.addWidget(QtWidgets.QLabel("With some widgets in itself")) absolute = QtWidgets.QWidget(dialog) absolute.setLayout(absolute_layout) # show the absolute widget and move it absolute.show() absolute.move(10, 10) absolute.raise_() dialog.show()

The dialog is shown correctly with the content in the layout but the absolute is not being shown.

What am I doing wrong?

最满意答案

通过使用...

absolute = QtWidgets.QWidget(dialog)

你absolute是一个dialog的孩子。 因此, absolute的几何形状总是在一定程度上由dialog 。 如果你想能够指定一个小部件的绝对几何使用...

absolute = QtWidgets.QWidget()

So with the help of @G.M. I found out that the solution is really easy:

absolute = QtWidgets.QWidget() absolute.setParent(dialog)

I was expecting that the QWidget::QWidget(QWidget * parent = 0, Qt::WindowFlags f = 0) constructor is calling the QWidget::setParent(QWidget * parent) method internally (which is not true?).

更多推荐

本文发布于:2023-07-14 17:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106052.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位置   QWidget   PyQt   absolute   position

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!