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. connect signal from subclass to father's slot

connect signal from subclass to father's slot

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 535 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.
  • J Offline
    J Offline
    Josz
    wrote on last edited by Josz
    #1

    Hi,

    I have a method in MainWindow called debug. MainWindow contains an object called inputline and into inputLine, there is a QPushbutton called sendButton.
    The question is, can I write the connection between sendButton (in the inputLine subclass) and the debug slot in MainWindow from the inputLine constructor?
    How should I write the connection?

    //in inputline subclass
    inputLine::inputLine(QWidget *parent) : QWidget(parent)
    {
        QHBoxLayout *layOut = new QHBoxLayout(this);
        sendButton.setText("Text");
        connect(&sendButton, &QPushButton::clicked,
                 parent,          //<- don't work
                ui::debug);   //<- don't work
    }
    
    //in MainWindow
    
    #include "inputline.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent), ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        inputLine *iL = new inputLine(this);
    
    /*    connect(&iL->sendButton, &QPushButton::clicked,
                this, &MainWindow::debug); */ //This worked, but it's outside from object
    }
    
    
    void MainWindow::debug()
    {
       qDebug() << "Debugeando: " << iL->readInfo() << endl;
    }
    
    

    please anybody so kind to help me?

    Thanks in advance

    1 Reply Last reply
    0
    • Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @Josz Hi,

      You can redirect the signal from the button to a signal of inputLine (by the way, usually class name starts with uppercase. That's not mandatory but it's a common practice. Better to name your class InputLine):

      //in inputline.h
      class InputLine : public QWidget{ //I guess it inherits from QWidget
      ...
      signals: //replicate signal of QPushButton
          void clicked();
      ...
      }
      
      //in mainwindow.cpp
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent), ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          InputLine *iL = new InputLine(this);
      
          connect(&iL, &InputLine::clicked, this, &MainWindow::debug);
      }
      
      J 1 Reply Last reply
      1
      • Gojir4G Gojir4

        @Josz Hi,

        You can redirect the signal from the button to a signal of inputLine (by the way, usually class name starts with uppercase. That's not mandatory but it's a common practice. Better to name your class InputLine):

        //in inputline.h
        class InputLine : public QWidget{ //I guess it inherits from QWidget
        ...
        signals: //replicate signal of QPushButton
            void clicked();
        ...
        }
        
        //in mainwindow.cpp
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent), ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            InputLine *iL = new InputLine(this);
        
            connect(&iL, &InputLine::clicked, this, &MainWindow::debug);
        }
        
        J Offline
        J Offline
        Josz
        wrote on last edited by
        #3

        @Gojir4 Thank you very much for all tips.

        Then I assume that connect inside the object is not possible, but your solution, allows me make private the variables.

        I wil try ;-)

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

          Hi,

          In addition to what @Gojir4 wrote, doing what you originally planned would quickly become a nightmare to maintain because as soon you change the "father" class, you'd have to either ensure that it contains the slot or modify the your InputLine class to match the new "father" class. Doing so you break encapsulation. For all it does InputLine shouldn't care about where the signal goes, that's not its responsibility.

          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