Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get access to Second Dialog methods from MainWindow and vice versa



  • Hi Guys;
    I am new to cpp and Qt but very enthusiastic about it.
    My question is how for example get the text inside a TextEdit of the MainWindow to appear on a TextEdit in the Second Window?
    I have tried very hard --even created getters and setters but to no avail.
    Either I can not access "private" ui from one-another or If I try to use a getter method it complains that it has to be applied on an object.
    What is the correct approach in this case in particular and in intra-window signals and slots in general in Qt?
    Thanks for bearing with me......
    Best Regards,
    Andreas



  • Hi and welcome :)

    @alamahant said in Get access to Second Dialog methods from MainWindow and vice versa:

    What is the correct approach in this in particular and in intra window signals and slots in general in Qt?

    Yes exactly this... You can pass the text (as QString) to your second class or window using the signal and slot mechanism.

    This is a great introduction (well, because it's the documentation) :-)
    https://doc.qt.io/qt-5/signalsandslots.html

    Getters are possible too, if your first window is e.g. a QDialog - type. Then you could check the exit status (accepted or rejected) and access the users dialog input with getters from your second (Main-) window-class.

    @alamahant said in Get access to Second Dialog methods from MainWindow and vice versa:

    I can not access "private" ui from one-another

    This is neither a good approach nor needed at all :)



  • Pl45m4
    Thanks a lot for your kind reply.
    It cleared many things in my mind.
    So basically i should use a "connect clause" like this:

    connect(sender,&signal,this,&slot)
    

    For example in my case I have MainWindow which contains a TextEdit(named display)where after opening a file its contents appear.
    And I have a FindandReplace Window that appears when I click an entry in the "Edit" menu I have created in The MainWindow.The second window also has a TextEdit(named "frDisplay").

    I can get the text fromMainWindow with something like:

    QString content=ui->display->toPlainText();
    

    So how would my "connect" statement look like?
    Maybe something like:

    connect(<menu-entry-button-name>,&on_triggered(),this,&<slot-originating-from-MainWindow-to-copy-TextEdit-to-FindAndReplace-window-TextEdit>)
    

    Or something similar............
    But I seem unable to properly express it in the correct terms...
    Any help would be fantastically welcome..........

    Thanks a lot,
    :)



  • @alamahant said in Get access to Second Dialog methods from MainWindow and vice versa:

    For example in my case I have MainWindow which contains a TextEdit(named display)where after opening a file its contents appear.
    And I have a FindandReplace Window that appears when I click an entry in the "Edit" menu I have created in The MainWindow.The second window also has a TextEdit(named "frDisplay").

    Is your FR-Window alive the whole time and you just show it on menuEntry-click or do you create and destroy it every time? Is the second window a QDialog or do you have two MainWindows?

    However, there are actually three ways to pass the text:

    You can

    • use Signals & Slots (as mentioned before)
    • use setters
    • or pass text with your constructor

    If you create a new instance of FrWindow every time you trigger the QAction in your menu, I would pass the QString inside the constructor and set it to your FrDisplay in your FrWindow itself.

    OR you do something like this: (to reraise the setter / getter thing)


    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "findandreplacewndw.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // This is what a connection looks like (to bind the function to the menu action)
        connect(ui->actionFind_Replace, &QAction::triggered, this, &MainWindow::openFrWindow);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::openFrWindow()
    {
        FindAndReplaceWndw frDialog(this);
        frDialog.setTextToTextDisplay(ui->textEdit_display->toPlainText());
        frDialog.exec();
    
    }
    

    FR Window / Dialog header

    public:
        // FR constr. + FR destr.
         // setter
        void setTextToTextDisplay(QString text);
    

    FR.cpp

    void FindAndReplaceWndw::setTextToTextDisplay(QString text)
    {
        ui->textEdit_frDisplay->insertPlainText(text);
    }
    

    To pass the text with constructor, you just need to modify your constructors signature to something like this:

    class FindAndReplaceWndw : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit FindAndReplaceWndw(QString text, QWidget *parent = nullptr); // added first parameter
        ~FindAndReplaceWndw();
    

    and in FR.cpp you just take the text and set it to your UI-element.

    FindAndReplaceWndw::FindAndReplaceWndw(QString text, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::FindAndReplaceWndw)
    {
        ui->setupUi(this);
        ui->textEdit_frDisplay->insertPlainText(text);
        // Done :)
    }
    

    Then you call your constructor like this:

    void MainWindow::openFrWindow()
    {
        FindAndReplaceWndw frDialog(ui->textEdit_display->toPlainText(), this);
        frDialog.exec();
    }
    

Log in to reply