<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Adam</title>
    <description>The latest articles on DEV Community by Adam (@adamcoding-2012).</description>
    <link>https://dev.to/adamcoding-2012</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4025613%2F5b9a7cb0-9535-40f5-96c9-3de7ae65f71b.png</url>
      <title>DEV Community: Adam</title>
      <link>https://dev.to/adamcoding-2012</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adamcoding-2012"/>
    <language>en</language>
    <item>
      <title>Why do we need classes in PySide6?</title>
      <dc:creator>Adam</dc:creator>
      <pubDate>Tue, 14 Jul 2026 00:21:24 +0000</pubDate>
      <link>https://dev.to/adamcoding-2012/why-do-we-need-classes-in-pyside6-egi</link>
      <guid>https://dev.to/adamcoding-2012/why-do-we-need-classes-in-pyside6-egi</guid>
      <description>&lt;p&gt;While we can build simple applications without using classes using PySide6, But in big applications and Massive coding systems We should use Classes But why?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To understand why do we need classes in PySide6 We should first see the Python code First
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;PySide6.QtWidgets&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;QApplication&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QWidget&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QPushButton&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;QLineEdit&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;QWidget&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
       &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

       &lt;span class="n"&gt;button1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QPushButton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Button 1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

       &lt;span class="nb"&gt;input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QLineEdit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;QApplication&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="n"&gt;window&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MainWindow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
   &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exec&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before talking about why do we need &lt;strong&gt;Classes for PySide6&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Let's Explain the code first line by line&lt;/p&gt;

&lt;h1&gt;
  
  
  The imports
&lt;/h1&gt;

&lt;p&gt;first thing we make the imports we do need: &lt;code&gt;from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;QApplication&lt;/code&gt; Is the simply the application we will make, Like empty app on the RAM it do nothing but it's on the RAM if it's alone&lt;/p&gt;

&lt;p&gt;And the &lt;code&gt;QWidget&lt;/code&gt; Is the Blank screen That will be placed on the Empty Application in the RAM&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;QPushButton&lt;/code&gt; Is like any button we are saying in any app Like the &lt;strong&gt;Subscribe&lt;/strong&gt; button on YouTube or like Post button on Twitter&lt;/p&gt;

&lt;p&gt;&lt;code&gt;QLineEdit&lt;/code&gt; is the input bar, Like the input bar of ChatGPT where you put on it your prompt or like The input bar in WhatsApp Where you type any thing on it to send it to your friends&lt;/p&gt;




&lt;h1&gt;
  
  
  The class
&lt;/h1&gt;

&lt;p&gt;And finally The thing &lt;strong&gt;You clicked on the post for&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First thing we define the &lt;code&gt;class&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How can we define it?&lt;/li&gt;
&lt;li&gt;Why do we need to define it?&lt;/li&gt;
&lt;li&gt;Why do even we want it?&lt;/li&gt;
&lt;li&gt;Who created it? (NOOO IAM JUST KIDDING)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can simply define the class in python by just typing &lt;code&gt;class&lt;/code&gt; That's it just &lt;code&gt;class&lt;/code&gt; then the name of it For Example &lt;code&gt;MainWindow&lt;/code&gt; and then a little semi-colon &lt;code&gt;:&lt;/code&gt; OR EVEN WE GIVE IT A &lt;strong&gt;&lt;em&gt;Parents&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;And Why do we need to define it, &lt;strong&gt;For simply use it&lt;/strong&gt; BRILLIANT RIGHT?&lt;/p&gt;

&lt;p&gt;And we want the classes in PySide6 for give it a parents &lt;code&gt;QWidget&lt;/code&gt; or even &lt;code&gt;QMainWindow&lt;/code&gt;, And we will explain both of them right now but before it Let's explain first what does parents means &lt;/p&gt;

&lt;p&gt;Imagine a superhero became to old and will die soon But he need to make last thing in his life which is giving all of his superpowers to his son, And to do this he need to say: I am giving all of my powers and knowledge and memory and intelligence and every single thing in me to my son, And he can do every single thing using my superpowers and nothing ever can stop him.&lt;/p&gt;

&lt;p&gt;That's is literally the same thing in parent's when you want to give all of an module, Library knowledge, functions, or even another classes to an class you simply need to make a parentheses &lt;code&gt;()&lt;/code&gt; sticking to the name of the class so it be: &lt;code&gt;class MainWindow()&lt;/code&gt; And inside those parentheses , you need to write the library name which is in our case &lt;code&gt;QWidget&lt;/code&gt; And as we said &lt;code&gt;QWidget&lt;/code&gt; is an literally Blank paper, But do not underestimate this blank sheet of paper. Because in it's the main thing for everything, yeah it's Blank but not dust, In using this Blank thing we give it as a parent for our class so our class become the little new superhero who toke everything from his dad, so when we make &lt;code&gt;self.setWindowTitle()&lt;/code&gt; We can give to our class a title, But what what is self? And what is even &lt;code&gt;def __init__()&lt;/code&gt; and also what is &lt;code&gt;super()._init__()&lt;/code&gt;??!!!&lt;/p&gt;

&lt;p&gt;We will understand everything right now &lt;/p&gt;

&lt;p&gt;the &lt;code&gt;def __init__(self):&lt;/code&gt; is a special method that Python automatically calls whenever you create an object from the class (WE will explain objects soon), It's called the constructor it's run automatically everything inside the class automatically and the &lt;code&gt;self&lt;/code&gt; inside the &lt;code&gt;__init__()&lt;/code&gt; is  simply referring to the class it self (And BTW you can name it anything you want, But the important thing is to make it at index 1 or the first parameter inside &lt;code&gt;def __init__(self)&lt;/code&gt;, So simply &lt;code&gt;self&lt;/code&gt;, Means that the class say: ME YEAH IT'S ME WHO WANT TO BE HIS TITLE IS SIMPLE WINDOW by simply making self.setWindowTitle("SIMPLE WINDOW"), and it's the same thing that goes on the size of the window, and it's icon and every single thing&lt;/p&gt;

&lt;p&gt;And do not thing &lt;code&gt;self&lt;/code&gt; power stops their, No and never there is a lot of another powers, If we talked about it, it's will take days&lt;/p&gt;

&lt;p&gt;And lastly &lt;code&gt;super().__init__()&lt;/code&gt; This one is simple as death, it's just saying: MY PARENT (&lt;code&gt;QWidget&lt;/code&gt;) WANT TO MAKE THINGS TO SET HIS SELF UP, that's it so simple but so important because without it the PySide6 will raise an error because the PARENT should set his self up Before saying: &lt;strong&gt;I am giving all of my powers and knowledge and memory and intelligence and every single thing in me to my son, And he can do every single thing using my superpowers and nothing ever can stop him.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And now the question Is half answered &lt;/p&gt;

&lt;p&gt;And the another half is simple objects what is object? it's an varible taking your class name for example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;object1 = MainWindow()&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;This is the object and that object holding the class and the class holds the code of your window, sometimes backend, or even your entire application, &lt;/p&gt;

&lt;p&gt;IF NOTHING IS CLEAR YOU CAN COMMENT BELOW AND I WILL RESPOND AS QUICKLY AS POSSIBLE&lt;/p&gt;

&lt;p&gt;And BTW who created the classes in python is called: Guido van Rossum&lt;/p&gt;

</description>
      <category>pyside6</category>
      <category>python</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
