<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Best practice: QDialog accesing QMainWindow attribute]]></title><description><![CDATA[<p dir="auto">Hi!</p>
<p dir="auto">I'm using PyQt for a project and I have some doubts of how to make things the proper way.</p>
<p dir="auto">Let's say for example that I have a list of names that may change as an attribute of MainWindow, and I want to display those names in a QComboBox that is in a QDialog in another class. What would be the best way to connect them?</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/topic/141755/best-practice-qdialog-accesing-qmainwindow-attribute</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Apr 2026 09:32:44 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/141755.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 26 Dec 2022 23:58:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 14:16:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/izabel">@<bdi>Izabel</bdi></a><br />
Hello and welcome.</p>
<p dir="auto">It's simple: just do <em>not</em> do what most beginners seems to try, which is to have the dialog know anything about or be able to call anything in the MainWindow!  That is the wrong way to go.  Other windows/dialogs/classes should know <em>nothing</em> about your main window, do not even allow yourself to import the MainWindow module inot any other module so you won't be tempted.</p>
<p dir="auto">Assuming you mean only MainWindow knows the list of names to populate the combobox, write yourself a utility "setter" method in the dialog to receive the list to be used and it populates the combobox.  And maybe a "getter" method to return which one selected if wanted by the outside world.  Something like:</p>
<pre><code># MainWindow
import MyDialog

myDialog = MyDialog(self)
myDialog.setComboItems(["abc", "def", ...])
if myDialog.exec():
    chosenItem = myDialog.comboItem()

# MyDialog
def setComboItems(self, items):
    self.combo.addItems(items)

def comboItem(self):
    return self.combo.currentText()
</code></pre>
]]></description><link>https://forum.qt.io/post/741565</link><guid isPermaLink="true">https://forum.qt.io/post/741565</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 27 Dec 2022 14:16:10 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 17:43:19 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">Hahaha that's a really good example. Yeah, I'd definitely prefer to ask them for the information.</p>
<p dir="auto">Thank you so much! :)</p>
]]></description><link>https://forum.qt.io/post/741609</link><guid isPermaLink="true">https://forum.qt.io/post/741609</guid><dc:creator><![CDATA[Izabel]]></dc:creator><pubDate>Tue, 27 Dec 2022 17:43:19 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 17:40:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mpergand">@<bdi>mpergand</bdi></a> Thank you so much! :)</p>
]]></description><link>https://forum.qt.io/post/741608</link><guid isPermaLink="true">https://forum.qt.io/post/741608</guid><dc:creator><![CDATA[Izabel]]></dc:creator><pubDate>Tue, 27 Dec 2022 17:40:39 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 18:04:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/izabel">@<bdi>Izabel</bdi></a><br />
Think of real life.  You're a parent and you want some information from one of your children to store in your bedroom.  Would you rather ask the child for the information and put it there yourself, or have some child rummaging around in your bedroom and hoping s/he doesn't destroy it? ;-)</p>
<p dir="auto">For the rest as <a class="plugin-mentions-user plugin-mentions-a" href="/user/mpergand">@<bdi>mpergand</bdi></a> said.</p>
<p dir="auto">I would suggest you would find your life easier if you try not to call into parent from children.  Qt can also make this easier with its <em>signals/slots</em> mechanism.</p>
]]></description><link>https://forum.qt.io/post/741603</link><guid isPermaLink="true">https://forum.qt.io/post/741603</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 27 Dec 2022 18:04:11 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 16:33:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/izabel">@<bdi>Izabel</bdi></a> said in <a href="/post/741592">Best practice: QDialog accesing QMainWindow attribute</a>:</p>
<blockquote>
<p dir="auto">That would be an example of what I shouldn't do, right?</p>
</blockquote>
<p dir="auto">Nothing is 100% bad or good.<br />
You need to understand what you are doing.<br />
When you pass a reference of your MainWidow to the Dialog, you create a strong dependency between the two objects (tight coupling)<br />
Work well in your case, now suppose you need the same utility dialog somewhere else in your program where you have no access to the Maindow , you're doomed :)</p>
<p dir="auto">By experience, programmers know that creating  tight coupling is bad and can lead to nightmare issues.<br />
And the main avantage of object programming is to produce reusable objects, so you must have this in mind and eliminate all dependencies as much as possible.</p>
]]></description><link>https://forum.qt.io/post/741598</link><guid isPermaLink="true">https://forum.qt.io/post/741598</guid><dc:creator><![CDATA[mpergand]]></dc:creator><pubDate>Tue, 27 Dec 2022 16:33:25 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 15:16:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a><br />
Hi again!</p>
<p dir="auto">I want to be 1000% clear. I've seen a lot of examples of people accessing parent attributes whitin QDialog saving a parent reference or accessing them as self.parent().attribute_name.</p>
<p dir="auto">That would be an example of what I shouldn't do, right?</p>
<p dir="auto">Thanks again.</p>
]]></description><link>https://forum.qt.io/post/741592</link><guid isPermaLink="true">https://forum.qt.io/post/741592</guid><dc:creator><![CDATA[Izabel]]></dc:creator><pubDate>Tue, 27 Dec 2022 15:16:44 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 13:47:04 GMT]]></title><description><![CDATA[<p dir="auto">Thank you so much! As a beginner that  was what I was trying to do at first but I knew it was a bad idea haha.</p>
<p dir="auto">Nice,  I got it! Happy Holidays!</p>
]]></description><link>https://forum.qt.io/post/741588</link><guid isPermaLink="true">https://forum.qt.io/post/741588</guid><dc:creator><![CDATA[Izabel]]></dc:creator><pubDate>Tue, 27 Dec 2022 13:47:04 GMT</pubDate></item><item><title><![CDATA[Reply to Best practice: QDialog accesing QMainWindow attribute on Tue, 27 Dec 2022 14:16:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/izabel">@<bdi>Izabel</bdi></a><br />
Hello and welcome.</p>
<p dir="auto">It's simple: just do <em>not</em> do what most beginners seems to try, which is to have the dialog know anything about or be able to call anything in the MainWindow!  That is the wrong way to go.  Other windows/dialogs/classes should know <em>nothing</em> about your main window, do not even allow yourself to import the MainWindow module inot any other module so you won't be tempted.</p>
<p dir="auto">Assuming you mean only MainWindow knows the list of names to populate the combobox, write yourself a utility "setter" method in the dialog to receive the list to be used and it populates the combobox.  And maybe a "getter" method to return which one selected if wanted by the outside world.  Something like:</p>
<pre><code># MainWindow
import MyDialog

myDialog = MyDialog(self)
myDialog.setComboItems(["abc", "def", ...])
if myDialog.exec():
    chosenItem = myDialog.comboItem()

# MyDialog
def setComboItems(self, items):
    self.combo.addItems(items)

def comboItem(self):
    return self.combo.currentText()
</code></pre>
]]></description><link>https://forum.qt.io/post/741565</link><guid isPermaLink="true">https://forum.qt.io/post/741565</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 27 Dec 2022 14:16:10 GMT</pubDate></item></channel></rss>