title: Qt GUI Easiest way to access MainWindow from another class
body: <p>I am doing a blackjack program and I am keeping track of the cards in the players hand in another class ("hand.h") than the main window class. </p>
  
  <p>In the hand class, for every card that I collect, I am also creating a QLabel that grabs the proper card image for the card and also sets the coordinates for where the card should appear on the main window. </p>
  
  <p>The problem is that I am not able to create the QLabel based on the MainWindows object that is originally created at the main function. Is there any easy way that I am able to get that information fairly easily?  Thanks for your help!</p>
  
  <p>I have tried using QGuiApplication::topLevelWindows(), but haven't came to luck with using that. Here is my function that I am using.</p>
  
  <pre><code>    #include &lt;QRect&gt;
      #include &lt;QApplication&gt;
      #include &lt;iostream&gt;
      #include &lt;QLabel&gt;
      #include "mainwindow.h"
      #include &lt;QMainWindow&gt;
      #include &lt;QWindowList&gt;
      #include &lt;QWidgetList&gt;
      #include "ui_mainwindow.h"
  
      void Test() {
  
      QList&lt;QWindow*&gt; Main_Window = QGuiApplication::topLevelWindows();
       for (int i = 0; i &lt; Main_Window.size(); ++i) {
          if(Main_Window.objectName() == "mainWindow") // name is OK
                  break;
          }
      QMainWindow* mainWindow = static_cast&lt;QMainWindow*&gt;(Main_Window);
  
  
      QLabel* temp;
      temp = new QLabel(Main_Window);
      temp-&gt;setPixmap(QString("Ten of Clubs.png"));
      temp-&gt;setGeometry(290, 300, 350, 390);
      temp-&gt;show();
  
      }
  </code></pre>
  
  <p>Here is the main.cpp file that creates the mainwindow</p>
  
  <pre><code>    int main(int argc, char *argv[])
      {
        srand(time(NULL));
        QApplication a(argc, argv);
        MainWindow w;
  
        w.show();
        return a.exec();
      }
  </code></pre>
  
  <p>I found the iterating code online and have been having issues from it.
  I am having issues while trying to iterate through the list, but I have no idea how to identify the list and the error says that there is no objectName() function. Also, in the static cast line, there is an error that says that I cannot convert an QList to type QMainWindow. Any help would be greatly appreciated.</p>