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. Retrieve information from mainwindow and display the information on newwindow::issues

Retrieve information from mainwindow and display the information on newwindow::issues

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 865 Views 1 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.
  • QT_QT_QTQ Offline
    QT_QT_QTQ Offline
    QT_QT_QT
    wrote on last edited by
    #1

    Greetings,

    Regarding to the title above, i m trying to use SIGNAL and SLOT method for implementation . Here my codes:

    ***mainwindow.h****
    
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QObject>
    #include <QString>
    #include "newwindow.h"
    
    class mainWindow
    {
        int i_value;
        newWindow *myNewWindow();
    
    
    public:
        mainWindow();
        void setI_Value(int x);
        int getI_Value();
        void changeValueTo(int y);
    
    
    signals:
        void dataAvailable(const int &y);
    
    
    };
    
    #endif // MAINWINDOW_H
    
    
    ***newwindow.h***
    
    #ifndef NEWWINDOW_H
    #define NEWWINDOW_H
    
    #include <QObject>
    
    class newWindow
    {
    public:
        newWindow();
    slots:
        void getDataFromMainWindow(int &y);
    };
    
    #endif // NEWWINDOW_H
    
    
    ***mainwindow.cpp***
    
    #include "mainwindow.h"
    #include "newwindow.h"
    #include <QObject>
    
    using namespace std;
    
    mainWindow::mainWindow()
    {
        i_value = 10;
        QObject::connect(this,SIGNAL(dataAvailable(int)),myNewWindow,SLOT(getDataFromMainWindow(int)));
    
    }
    void mainWindow::setI_Value(int x)
    {
       i_value = x;
    }
    
    int mainWindow::getI_Value()
    {
        return i_value;
    }
    
    void mainWindow::changeValueTo(int &y)
    {
        if (y!=i_value){
            i_value = y;
            emit dataAvailable(y);
    
       }
    }
    
    
    ***newwindow.cpp***
    
    #include "newwindow.h"
    #include "mainwindow.h"
    #include <QObject>
    
    
    newWindow::newWindow()
    {
    
    }
    
    void newWindow::getDataFromMainWindow(int y)
    {
        int xyz = z;
        qDebug()<< xyz;
    }
    
    

    Do spend some time to understand the codes, its quite simple to understand . Hoping you will get the full picture after you gain the understanding from my code. Now, when i compile them , i get a lot of error which me also dont understanding what is happening . Someone here please share with me your knowledge on this approach. Spend so many days to solve this issue. Without solving this issue, i can not proceed to my next task!Thank you in advance!

    1 Reply Last reply
    0
    • Andy314A Offline
      Andy314A Offline
      Andy314
      wrote on last edited by
      #2

      Whats that: newWindow *myNewWindow(); ?

      I cannot understand your code. Mainwindow has not a base class. Is is not really a window !?

      You can use signals and slots only in subclasses of a QObject and you must use the Q_OBJECT macro.

      class SubClass : public QObject
      { Q_OBJECT
      signals:
      slots:
      }

      QT_QT_QTQ 1 Reply Last reply
      0
      • Andy314A Andy314

        Whats that: newWindow *myNewWindow(); ?

        I cannot understand your code. Mainwindow has not a base class. Is is not really a window !?

        You can use signals and slots only in subclasses of a QObject and you must use the Q_OBJECT macro.

        class SubClass : public QObject
        { Q_OBJECT
        signals:
        slots:
        }

        QT_QT_QTQ Offline
        QT_QT_QTQ Offline
        QT_QT_QT
        wrote on last edited by QT_QT_QT
        #3

        @Andy314

        Hi ,

        The thing that you asked is to create a new object from NewWidow, an object from created different class. The purpose of this is i want to "put" this object in the connect function . something like this :

        QObject :: connect (this, SIGNAL(dataAvailable(int y)),myNewWindow,SLOT(getDataFromMainWindow(int y)));

        the signal is emitted from main window , and the search for the slots in new Window .

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

          Please read http://doc.qt.io/qt-5.6/signalsandslots.html
          As @Andy314 already said the class newWindow must be a subclass of QObject and have Q_OBJECT macro in it in order to use signals/slots!

          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