Custom Widgets in PyQt and PySide: Floating and Absolute Positioned Widget, Menu, and Container in QT

Posted by


In PyQt and PySide, you can create custom widgets/layouts that have floating/absolute positioning using the QWidget class. This allows you to create more dynamic and interactive UIs for your applications. In this tutorial, we will walk through the steps to create a floating/absolute positioned widget/menu/container in PyQt.

Step 1: Import the necessary modules
First, you need to import the necessary modules for PyQt or PySide. You can do this by adding the following lines of code to your script:

from PyQt5 import QtWidgets, QtCore

Step 2: Create the main window
Next, you need to create the main window for your application. You can do this by adding the following code:

app = QtWidgets.QApplication([])
main_window = QtWidgets.QMainWindow()
main_window.setGeometry(100, 100, 800, 600)
main_window.setWindowTitle("Floating Widget Tutorial")

Step 3: Create the floating/absolute positioned widget
Now, we can create the floating/absolute positioned widget. This widget will be a custom widget that will be positioned at a fixed location on the screen. You can do this by creating a custom class that inherits from QtWidgets.QWidget and setting its position using the move method. Here’s an example:

class FloatingWidget(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(200, 200, 200, 200)
        self.setWindowTitle("Floating Widget")
        self.move(300, 300)

Step 4: Display the floating widget
Now that we have created the floating widget, we can display it on the main window. You can do this by creating an instance of the FloatingWidget class and calling the show method. Here’s an example:

floating_widget = FloatingWidget()
floating_widget.show()

Step 5: Run the application
Finally, you need to run the application by calling the exec method on the QApplication instance. Here’s how you can do this:

app.exec_()

That’s it! You have successfully created a floating/absolute positioned widget in PyQt. You can customize the widget further by adding additional features, such as buttons, labels, or input fields. This allows you to create more interactive UIs for your applications.

0 0 votes
Article Rating

Leave a Reply

14 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@SpinnTV
3 hours ago

Download the source code https://www.patreon.com/posts/55278005

@TomaeninaROUABAH
3 hours ago

i wanna display an image on clicking a button can you please show me how to do that

@joyceelnino7616
3 hours ago

I still can't put the widget in float mode but I followed all the instructions…😞😞😞

@Onemorejustine
3 hours ago

1:37 How did you do it?It so amazing!

@mariamkhanam4037
3 hours ago

Bro, how can I add shadow to a frameless window or a widget? Do you have any video on It?😐 pls pls pls

@marzansyoutube1621
3 hours ago

I cant find QCustomSideMenu in Qt Designer😔

@WalterMan
3 hours ago

Is it possible to make the floating widget pop up from the bottom-center or top center?

@joelkklein8760
3 hours ago

Spectacular.. I see a demo app, with main window, the window has frames inside frames widgets. and the user can relocated the frames on window at user convenience. Can you do a tutorial about how relocated teh frames. if you can see the demo.. Its heave about 140Mb review the link https://drive.google.com/file/d/1oTLTW1K7PQBM4JiirYr6IzfrDUdJjxXn/view?usp=sharing

@manjunathneelmath2104
3 hours ago

Why won't you provide the link to the bgm in the description? Please provide the link(I am searching it all over). All bgm in your videos are very difficult to find and they are so good to listen.

@manjunathneelmath2104
3 hours ago

Please make video on QPainter soon.

@ЛюбомирПона
3 hours ago

I can't install pyside6 via pip, but i can another one packages (for example pyside2 etc). Can you make tutorial video about install process (also build from sources). I use python 3.8.*

@samoconnor3633
3 hours ago

Could you reply with a discord link, the one in the description wont work for me

@lassinaouattara1213
3 hours ago

you're the best Sir keep it up

@samoconnor3633
3 hours ago

Excellent this is exactly what I wanted, I will add it to my program as soon as I'm off work, I'm currently building a web app vulnerability scanner and this will be a great addition

14
0
Would love your thoughts, please comment.x
()
x