title: QT C++ - How to refer to a widget in another class
body: <p>I'm trying to make a button that adds a widget into a layout.</p>
  
  <p>The widget is in another class entirely from the UI.</p>
  
  <p>Normally I'd make a pointer to the other class (without the UI, with the widget) which would be <code>InkSpot *ink</code> (class name being InkSpot) then I would say <code>ui-&gt;paintAreaLayout-&gt;addWidget(ink-&gt;widget);</code> but that line right there causes the application to crash, if I remove it and add something not referencing that class then it wont crash.</p>
  
  <p>I'll provide most of the code so that you can better see what I'm trying to do. The test button is at the bottom of the first code block which is the one in question:</p>
  
  <p>inkpuppet.cpp</p>
  
  <pre><code>#include "inkpuppet.h"
  #include "ui_inkpuppet.h"
  #include "newdialog.h"
  #include "inkspot.h"
  
  #include &lt;Qt&gt;
  #include &lt;QtCore&gt;
  #include &lt;QtGui&gt;
  #include &lt;QtWidgets&gt;
  #include &lt;QDialog&gt;
  #include &lt;QMainWindow&gt;
  
  
  InkPuppet::InkPuppet(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::InkPuppet)
  {
      ui-&gt;setupUi(this);
  
      //connect the frame range boxes to the timeslider
      connect(ui-&gt;lowerFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMinimum(int)));
      connect(ui-&gt;upperFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMaximum(int)));
  
      //connect the menu items
      connect(ui-&gt;actionNew, SIGNAL(triggered()), this, SLOT(actionNew()));
  
      //connect test
      connect(ui-&gt;testButton, SIGNAL(clicked()), this, SLOT(testButton()));
  }
  
  InkPuppet::~InkPuppet()
  {
      delete ui;
  }
  
  void InkPuppet::setMinimum(int value)
  {
      ui-&gt;timeSlider-&gt;setMinimum(value);
  }
  
  void InkPuppet::setMaximum(int value)
  {
      ui-&gt;timeSlider-&gt;setMaximum(value);
  }
  
  void InkPuppet::actionNew()
  {
      NewDialog *dialog = new NewDialog;
      dialog-&gt;setModal(true);
      dialog-&gt;show();
  }
  
  void InkPuppet::testButton()
  {
      ui-&gt;testButton-&gt;setText("working");
      //ui-&gt;paintAreaLayout-&gt;addWidget(ink-&gt;label);
      //qDebug() &lt;&lt; ui-&gt;testButton;
      ui-&gt;paintAreaLayout-&gt;addWidget(ink-&gt;widget);
  }
  </code></pre>
  
  <p>inkpuppet.h</p>
  
  <pre><code>#ifndef INKPUPPET_H
  #define INKPUPPET_H
  
  #include "inkspot.h"
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
  class InkPuppet;
  }
  
  class InkPuppet : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit InkPuppet(QWidget *parent = 0);
      ~InkPuppet();
      InkSpot *ink;
  
  private slots:
      void setMinimum(int value);
      void setMaximum(int value);
      void actionNew();
      void testButton();
  
  public:
      Ui::InkPuppet *ui;
  };
  
  #endif // INKPUPPET_H
  </code></pre>
  
  <p>inkspot.cpp</p>
  
  <pre><code>#include "inkspot.h"
  #include "inkpuppet.h"
  #include "ui_inkpuppet.h"
  
  #include &lt;QtCore&gt;
  #include &lt;QtGui&gt;
  #include &lt;QWidget&gt;
  #include &lt;QPainter&gt;
  #include &lt;QPaintEvent&gt;
  
  
  void InkSpot::paintEvent(QPaintEvent *event)
  {
      QWidget *widget = new QWidget();
      widget-&gt;setFixedSize(100, 150);
      QLabel *label = new QLabel();
      label-&gt;setText("test");
      widget-&gt;layout()-&gt;addWidget(label);
      //InkPuppet.ui-&gt;paintAreaLayout-&gt;addWidget(widget);
  
      QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only
      char *brushProto;
      uchar *brushData;
  
      brushInput = new QFile("x:\\Development\\InkPuppet\\brush.raw"); //open the raw file
      brushInput-&gt;open(QIODevice::ReadOnly);
      QDataStream in;
      in.setDevice(brushInput);
      int size = brushInput-&gt;size(); //set size to length of raw file
  
      brushProto = new char[size];
      in.readRawData(brushProto, size); //read file into prototype
      brushData = new uchar[size];
  
      for(int i = 0; i &lt; size; ++i)
      {
          brushData[i] = (uchar)brushProto[i]; //copy char to uchar array
      }
  
      QImage test(brushData, 128, 128, QImage::Format_Indexed8);
      QImage test2(128, 128, QImage::Format_ARGB32);
  
      QVector&lt;QRgb&gt; vectorColors(256); //create color table
      for(int c = 0; c &lt; 256; c++)
      {
          vectorColors[c] = qRgb(c, c, c);
      }
  
      test.setColorTable(vectorColors);
  
      for(int iX = 0; iX &lt; 100; ++iX)
      {
          for(int iY = 0; iY &lt; 100; ++iY)
          {
              test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255)));
          }
      }
  
      //final conversion for stencil and color brush
      QPixmap testPixmap = QPixmap::fromImage(test2);
      QPixmap testPixmap2 = QPixmap::fromImage(test);
  
      QPainter painter(this);
  
      painter.drawPixmap(150, 50, 100, 100, testPixmap);
      painter.drawPixmap(50, 50, 100, 100, testPixmap2);
  
      delete[] brushProto;
      delete[] brushData;
      delete brushInput;
  }
  </code></pre>
  
  <p>inkspot.h</p>
  
  <pre><code>#ifndef INKSPOT_H
  #define INKSPOT_H
  
  #include &lt;QObject&gt;
  #include &lt;QWidget&gt;
  #include &lt;QPainter&gt;
  #include &lt;QPaintEvent&gt;
  #include &lt;QLabel&gt;
  
  namespace Ui {
  class InkSpot;
  }
  
  class InkSpot : public QWidget
  {
      Q_OBJECT
  public:
      explicit InkSpot(QWidget *parent = 0);
      ~InkSpot();
      void draw(QPainter *painter);
      QWidget *widget;
      QLabel *label;
  signals:
  
  public slots:
  
  protected:
      void paintEvent(QPaintEvent *event);
  
  private:
      Ui::InkSpot *ui;
  
  };
  
  #endif // INKSPOT_H
  </code></pre>