Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ /Qt Proper way to access ui from another class in qt //Edited
    text
    copied!<p>I'm trying to access the ui member which is private in the MainWindow class.</p> <p>I would like to update a lineEdit (Xvaldisp) when I release the mousebutton (with the amount the mouse moved)from a gl widget. </p> <p>After searching a bit around I found that I need to create a function/Method in mainwindow then access it through a pointer to Mainwindow from my GLWidget</p> <p><strong>The problem:</strong></p> <p>The lineEdit remains blank, The method( displaymessage() ) that should update it seems to get called.</p> <p>To check that I've created a string(str) to see if displaymessage was getting called, this string gets updated with a new value when displaymessage() gets called.</p> <p>The on_Button_clicked() method below displaymessage() also updates the same lineEdit when a pushbutton is clicked and works just fine it displays the content of str </p> <p>Here's my code:</p> <p><strong>glwidget.h</strong></p> <pre><code>#ifndef GLWIDGET_H #define GLWIDGET_H #include &lt;QGLWidget&gt; #include &lt;QTimer&gt; #include &lt;QMouseEvent&gt; #include "mainwindow.h" #include &lt;QObject&gt; #include &lt;QLineEdit&gt; class GLWidget : public QGLWidget { Q_OBJECT public: explicit GLWidget(QWidget *parent = 0); void mousePressEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); void initializeGL(); void paintGL(); void resizeGL(int w, int h); private: QTimer timer; QPoint pointMpressed; QPoint diff; protected: signals: void valueCh(); }; #endif // GLWIDGET_H </code></pre> <p><strong>mainwindow.h</strong></p> <pre><code>#ifndef MAINWINDOW_H #define MAINWINDOW_H #include &lt;QMainWindow&gt; #include &lt;QLineEdit&gt; #include &lt;QObject&gt; #include "glwidget.h" namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void on_Button_clicked(); void displayMessage(); protected: void changeEvent(QEvent *e); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H </code></pre> <p><strong>glwidget.cpp.</strong></p> <pre><code>#include "glwidget.h" #include &lt;GL/glut.h&gt; GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent) { connect (&amp;timer,SIGNAL(timeout()),this,SLOT(updateGL())); timer.start(16); } void GLWidget::mousePressEvent(QMouseEvent *e){ pointMpressed=e-&gt;pos(); } void GLWidget::mouseReleaseEvent(QMouseEvent *e){ diff=(e-&gt;pos())- pointMpressed ; //calculate position difference between click and release MainWindow *mwd; mwd= new MainWindow; // mwd-&gt;displayMessage(); //tried this first then with signals and slots below same result QObject::connect(this, SIGNAL(valueCh()), mwd ,SLOT(displayMessage()) ); emit valueCh(); delete mwd; } void GLWidget::initializeGL(){ } void GLWidget::resizeGL(int w, int h){ } </code></pre> <p><strong>mainwindow.cpp</strong></p> <pre><code>#include "mainwindow.h" #include "ui_mainwindow.h" #include "glwidget.h" QString str="none activated"; //I used this string to check if the methods were getting called MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui-&gt;setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::changeEvent(QEvent *e) { QMainWindow::changeEvent(e); switch (e-&gt;type()) { case QEvent::LanguageChange: ui-&gt;retranslateUi(this); break; default: break; } } void MainWindow::displayMessage(){ //this method should update the lineEdit (Xvaldisp) the lineEdit however remains blank but str gets updated ui-&gt;Xvaldisp-&gt;setText(str); str ="displayMessage hs rn"; //displaymessage has run } void MainWindow::on_Button_clicked(){ // this is a pushbutton(Button) that once pushed also updates the same lineEdit(Xvaldisp) this works just fine If I clicked and released the mouse before on the GLWidget it would show the by displaymessage() updated string else it would be the orginal value ui-&gt;Xvaldisp-&gt;setText(str); } </code></pre>
 

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