Accessing Data Between Multiple Forms



  • I am new to Qt and have run into a problem. I currently have two window. A main window and a secondary window.

    MainWindow.h

    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
        void allChecked();
        void openNewWindow();
        void setData(int input)
        {
            stuff = input;
        }
    
    private:
        Ui::MainWindow *ui;
        SecWindow * mNewWindow;
        int stuff;
    };
    

    MainWindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>
    #include <QMessageBox>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_6_clicked()
    {
        openNewWindow();
        this->hide();
    }
    
    void MainWindow::openNewWindow()
    {
        mNewWindow = new SecWindow();
        mNewWindow->setHome(this);
        mNewWindow->show();
    }
    

    SecWindow.h

    #include <QMainWindow>
    #include <QString>
    
    namespace Ui {
    class SecWindow;
    }
    
    class SecWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit SecWindow(QWidget *parent = 0);
        ~SecWindow();
    
        void setHome(QMainWindow * mhome)
        {
            home = mhome;
        }
    
    private:
        Ui::SecWindow *ui;
        QMainWindow * home;
    };
    

    SecWindow.cpp

    #include "secwindow.h"
    #include "ui_secwindow.h"
    
    SecWindow::SecWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::SecWindow)
    {
        ui->setupUi(this);
    }
    
    SecWindow::~SecWindow()
    {
        delete ui;
    }
    
    void SecWindow::on_pushButton_clicked()
    {
        home->show();
        this->close();
    }
    
    
    void SecWindow::on_intButton_clicked()
    {
        QString x;
        x = ui->intLine->text();
        int n = x.toInt();
       
        //This is where I am having problem
        home->world->setData(n);
        home->show();
        this->close;
    }
    

    When I click a button in the SecWindow form, I would like to be able to access the data and functions stored in MainWindow (similar to as if I was already in that window), but I cannot. It may be because home is declared as a QMainWindow in SecWindow, but it also won't allow me to declare it as a MainWindow.

    Any solutions or alternatives to what I am trying to accomplish would be much appreciated!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should take a look at Qt's signals and slots for communicating data between your widgets.

    SecWindow doesn't need to know anything about MainWindow.

    Note that you have a memory leak, you create a new SecWindow each time you call openNewWindow but never delete it.



  • Signals and slots don't quite incorporate everything that I was trying to do but I was able to figure it out by creating a separate database class and passing that class to both windows so that they could both access the same data.

    I thought .close() deletes the window. How does one delete the window then? And if .close doesn't completely delete the window, what is the difference between .hide() and .close()?

    Thanks for your reply!


  • Lifetime Qt Champion

    You can set the Qt::WA_DeleteOnClose widget attribute to make Qt delete your widget when closing.

    Hide just hides the widget. Close will also hide the widget if the event is accepted and trigger the deletion of the widget if WA_DeleteOnClose is set



  • @SGaist I need help with one more thing. I am trying to add an icon to my application but am having no luck. It is a mac application so I added the .icns file to resources and then in the .pro file add ICON = file.icns but this does not work. What am I doing wrong?

    Thanks for all the help.


  • Lifetime Qt Champion

    Setting the application icon in Qt's documentation


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.