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. How To Connect Emitter From MainWindow To AnotherWindow
Forum Updated to NodeBB v4.3 + New Features

How To Connect Emitter From MainWindow To AnotherWindow

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 398 Views
  • 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.
  • E Offline
    E Offline
    Eshya
    wrote on last edited by
    #1

    I have a main program in MainWindow whose computational results I emit to SecondWindow

    mainwindow.h

    private:
       secondwindow *_secondwindow;
    signals:
        void sendLabel(QString stringDisplay);
    

    secondwindow.h

    private:
       MainWindow *_mainW;
    private slots:
        void changeDisplay(QString display);
    

    mainwindow.cpp

    void MainWindow::secondwindow(){
    
       _secondwindow = new secondwindow(this)
        _secondwindow->showMaximized();
    
        emit sendLabel("400");
    
    
    }
    

    secondwindow.cpp

    secondwindow::secondwindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::secondwindow)
    {
        ui->setupUi(this);
        connect(mainW, SIGNAL(sendLabel()),this,SLOT(changeDisplay()));
    
    }
    
    void secondwindow::changeDisplay(QString display){
         ui->lcdNumber->display(display);
    }
    

    But I get an error because the main window is not recognized

    error: ‘MainWindow’ does not name a type; did you mean ‘QMainWindow’?
       27 |     MainWindow *_mainW;
          |     ^~~~~~~~~~
          |     QMainWindow
    

    What is the correct way to emit variable from main window to another window ?

    jsulmJ 1 Reply Last reply
    0
    • E Eshya

      I have a main program in MainWindow whose computational results I emit to SecondWindow

      mainwindow.h

      private:
         secondwindow *_secondwindow;
      signals:
          void sendLabel(QString stringDisplay);
      

      secondwindow.h

      private:
         MainWindow *_mainW;
      private slots:
          void changeDisplay(QString display);
      

      mainwindow.cpp

      void MainWindow::secondwindow(){
      
         _secondwindow = new secondwindow(this)
          _secondwindow->showMaximized();
      
          emit sendLabel("400");
      
      
      }
      

      secondwindow.cpp

      secondwindow::secondwindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::secondwindow)
      {
          ui->setupUi(this);
          connect(mainW, SIGNAL(sendLabel()),this,SLOT(changeDisplay()));
      
      }
      
      void secondwindow::changeDisplay(QString display){
           ui->lcdNumber->display(display);
      }
      

      But I get an error because the main window is not recognized

      error: ‘MainWindow’ does not name a type; did you mean ‘QMainWindow’?
         27 |     MainWindow *_mainW;
            |     ^~~~~~~~~~
            |     QMainWindow
      

      What is the correct way to emit variable from main window to another window ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:

      private:
      MainWindow *_mainW;

      You do not need a pointer to MainWindow in second window! A child should usually not know anything about its parent.
      The connection should be done in MainWindow after the creation of the second window instance.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      2
      • jsulmJ jsulm

        @Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:

        private:
        MainWindow *_mainW;

        You do not need a pointer to MainWindow in second window! A child should usually not know anything about its parent.
        The connection should be done in MainWindow after the creation of the second window instance.

        E Offline
        E Offline
        Eshya
        wrote on last edited by Eshya
        #3

        @jsulm Ok, but when i try to do this I got error force closed when show second window

            _secondwindow->showMaximized();
            connect(mainW, SIGNAL(sendLabel()),_secondwindow,SLOT(changeDisplay()));
            emit sendLabel("400");
        

        or

        QObject::connect: No such signal MainWindow::sendLabel() in
        
        jsulmJ 1 Reply Last reply
        0
        • E Eshya

          @jsulm Ok, but when i try to do this I got error force closed when show second window

              _secondwindow->showMaximized();
              connect(mainW, SIGNAL(sendLabel()),_secondwindow,SLOT(changeDisplay()));
              emit sendLabel("400");
          

          or

          QObject::connect: No such signal MainWindow::sendLabel() in
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:

          I got error

          Of course you get an error: please check the signature of your signal and what you have in your connect - they do not match!
          And you also should use the new Qt5 connect syntax.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          E 1 Reply Last reply
          1
          • jsulmJ jsulm

            @Eshya said in How To Connect Emitter From MainWindow To AnotherWindow:

            I got error

            Of course you get an error: please check the signature of your signal and what you have in your connect - they do not match!
            And you also should use the new Qt5 connect syntax.

            E Offline
            E Offline
            Eshya
            wrote on last edited by
            #5

            @jsulm said in How To Connect Emitter From MainWindow To AnotherWindow:

            Qt5 connect syntax.

            Okay, thank you. Silly me because not yet updated

            connect(this,&MainWindow::sendLabel,_secondwindow,&SecondWindow::changeDisplay);
            
            

            I just need to change thic section and done. Thank you

            1 Reply Last reply
            1

            • Login

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