miile7
miile7

Reputation: 2403

PyQt absolute position of QWidget

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?

Upvotes: 0

Views: 3837

Answers (2)

miile7
miile7

Reputation: 2403

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?).

Upvotes: 0

G.M.
G.M.

Reputation: 12929

By using...

absolute = QtWidgets.QWidget(dialog)

you have made absolute a child of dialog. Hence the geometry of absolute will always be governed to some extent by dialog. If you want to be able to specify the absolute geometry of a widget use...

absolute = QtWidgets.QWidget()

Upvotes: 1

Related Questions