C++ /Qt Proper way to access ui from another class in qt //Edited

前端 未结 1 572
感动是毒
感动是毒 2021-01-20 04:07

I\'m trying to access the ui member which is private in the MainWindow class.

I would like to update a lineEdit (Xvaldisp) when I release the mousebutton (with the

相关标签:
1条回答
  • 2021-01-20 04:43

    The root of your problem is that you seem to be misunderstanding the concept of pointers. Specifically, calling new is not the only way to obtain a pointer - a pointer is just a variable that holds the address of some object (or function). The new operator returns a pointer to a dynamically-allocated object, but there are other ways too, at least three of which are relevant to you:
    1) Have someone else give you a pointer, for example as a function parameter;
    2) Use & to take the address of an object.
    3) Use this to get a pointer to the object you are currently working with.

    Now that we have that out of the way, take a look at your code. MainWindow has two slots:

    class MainWindow : public QMainWindow {
        Q_OBJECT
        ...
    public slots:
       void on_Button_clicked();
       void displayMessage();
    

    Slots are member functions - they are called on an object.

    When you create an object of type MainWindow, the on_Button_clicked slot of your MainWindow object is automatically connected to the clicked signal of Button (via the meta-object compiler, a Qt-specific thing, because of the particular naming convention that you used).

    What happens to the other one? Here's your code:

    void GLWidget::mouseReleaseEvent(QMouseEvent *e){  
         MainWindow *mwd;  
         mwd=  new MainWindow;  
         QObject::connect(this, SIGNAL(valueCh()), mwd ,SLOT(displayMessage()) );  
         emit valueCh();  
         delete mwd;  
    }
    

    Rather than connecting to the slot of the original MainWindow object, you are creating a new object, calling its function, and promptly destroying it. So, while the slot does get called, it doesn't get called on the object that is actually your gui.

    This is happening because you figured you needed a pointer to a MainWindow object, and used new to get one. The problem is, that pointer didn't point to the object you actually care about (i.e. your actual gui).

    One (inelegant) solution is to not create a new MainWindow; instead, pass the address of the original MainWindow to the widget, and use that address (pointer) in the connect statement.

    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.

    Change your MainWindow constructor to the following:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->glWidget, SIGNAL(mouseReleaseEvent()),this,SLOT(displayMessage()));
    
    }
    

    and get rid of everything but the emit statement inside GLWidget::displayMessage.

    0 讨论(0)
提交回复
热议问题