Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The root of your problem is that you seem to be misunderstanding the concept of pointers. Specifically, calling <code>new</code> is <em>not</em> the only way to obtain a pointer - a pointer is just a variable that holds the address of some object (or function). The <code>new</code> operator returns a pointer to a dynamically-allocated object, but there are other ways too, at least three of which are relevant to you:<br> 1) Have someone else give you a pointer, for example as a function parameter;<br> 2) Use <code>&amp;</code> to take the address of an object.<br> 3) Use <code>this</code> to get a pointer to the object you are currently working with.</p> <p>Now that we have that out of the way, take a look at your code. <code>MainWindow</code> has two slots:</p> <pre><code>class MainWindow : public QMainWindow { Q_OBJECT ... public slots: void on_Button_clicked(); void displayMessage(); </code></pre> <p>Slots are member functions - they are called on an <strong>object</strong>.</p> <p>When you create an object of type <code>MainWindow</code>, the <code>on_Button_clicked</code> slot <strong>of your <code>MainWindow</code> object</strong> is automatically connected to the <code>clicked</code> signal of <code>Button</code> (via the meta-object compiler, a Qt-specific thing, because of the particular naming convention that you used).</p> <p>What happens to the other one? Here's your code:</p> <pre><code>void GLWidget::mouseReleaseEvent(QMouseEvent *e){ MainWindow *mwd; mwd= new MainWindow; QObject::connect(this, SIGNAL(valueCh()), mwd ,SLOT(displayMessage()) ); emit valueCh(); delete mwd; } </code></pre> <p>Rather than connecting to the slot of the <strong>original <code>MainWindow</code> object</strong>, you are creating a <em>new</em> object, calling <em>its</em> function, and promptly destroying it. So, while the <code>slot</code> does get called, it doesn't get called on the object that is actually your gui.</p> <p>This is happening because you figured you needed a pointer to a MainWindow object, and used <code>new</code> to get one. The problem is, that pointer didn't point to the object you actually care about (i.e. your actual <code>gui</code>).</p> <p>One (inelegant) solution is to not create a <code>new MainWindow</code>; instead, pass the address of the original <code>MainWindow</code> to the widget, and use that address (pointer) in the <code>connect</code> statement.</p> <p>The much better solution is to skip all that stuff and use signals and slots the way they were intended, so that individual widgets don't have to know anything about the objects they are being connected to.</p> <p>Change your <code>MainWindow</code> constructor to the following: </p> <pre><code>MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-&gt;setupUi(this); connect(ui-&gt;glWidget, SIGNAL(mouseReleaseEvent()),this,SLOT(displayMessage())); } </code></pre> <p>and get rid of everything but the <code>emit</code> statement inside <code>GLWidget::displayMessage</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload