Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as I understand, you have two classes: <code>MainWindow</code> and <code>Dialog</code>. As <code>MainWindow::myPosReg</code> is protected, you can only access it in <code>Dialog</code>, if <code>Dialog</code> derives from <code>MainWindow</code> or if it is a friend.</p> <p>The following design should work:</p> <pre><code>class MainWindow : public QMainWindow { [...] protected: DateReg *myPosReg; friend class Dialog; }; class Dialog { [...] MainWindow* myMainWindow; void someFunction() { myMainWindow-&gt;myPosReg-&gt;setValue([...]); } }; </code></pre> <p>As an alternative, I would suggest a more Qt-ish way by using signals and slots. When the spin-boxes in <code>Dialog</code> change, emit a signal to which <code>MainWindow</code> listens.</p> <p>We start with the dialog:</p> <pre><code>class Dialog : public QDialog { [...] Dialog() { [...init ui...] connect(ui-&gt;spinBoxA, SIGNAL(valueChanged(int)), this, SLOT(onSpinBoxValueChanged())); connect(ui-&gt;spinBoxB, SIGNAL(valueChanged(int)), this, SLOT(onSpinBoxValueChanged())); connect(ui-&gt;spinBoxC, SIGNAL(valueChanged(int)), this, SLOT(onSpinBoxValueChanged())); } signals: void valuesChanged(int a, int b, int c); private slots: void onSpinBoxValueChanged() { emit valuesChanged(ui-&gt;spinBoxA-&gt;value(), ui-&gt;spinBoxB-&gt;value(), ui-&gt;spinBoxC-&gt;value()); } }; </code></pre> <p>So the dialog has an internal slot <code>onSpinBoxValueChanged()</code>, which is called when one of the spinboxes changes its value. The sole purpose of this slot is to emit another signal <code>valuesChanged</code> to which we'll connect the main window (i.e. the dialog's internal slot combines three values into one signal).</p> <p>The main window could be the following:</p> <pre><code>class MainWindow : public QMainWindow { [...] DateReg *myPosReg; public slots: void setValues(int a, int b, int c) { myPosReg-&gt;setValues(a, b, c); } }; </code></pre> <p>And the code, which connects both</p> <pre><code>int main() { [...] MainWindow* mainWindow = new MainWindow; Dialog* dialog = new Dialog; connect(dialog, SIGNAL(valuesChanged(int,int,int)), mainWindow, SLOT(setValues(int,int,int))); } </code></pre> <p>When the user changes the value in a spinbox, the following chain-reaction happens:</p> <pre><code>QSpinBox::valueChanged(int) -&gt; Dialog::onSpinBoxValueChanged() -&gt; Dialog::valuesChanged() -&gt; MainWindow::setValues() -&gt; DateReg::setValues() </code></pre> <p>The advantage is that you decouple <code>MainWindow</code> and <code>Dialog</code>: <code>Dialog</code> just signals that the values have changed but does not care what happens with them and <code>MainWindow</code> changes its <code>myPosReg</code> but does not care, where the values are coming from.</p>
 

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