Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Accessing Data Between Multiple Forms

Accessing Data Between Multiple Forms

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jmount
    wrote on last edited by jmount
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jmount
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          1
          • SGaistS SGaist

            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

            J Offline
            J Offline
            jmount
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Setting the application icon in Qt's documentation

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved