简体   繁体   中英

how to write connect statement of lineEdit in different class

How can I make signal and slot of lineEdit which is declare in another class ? LineEdit is declared in Peakdetechtion class and i want to make signal and slot in peaksettingform so how can I do this?

the QLineEdit either has to be accessible from the outside (public or get) or you have to forward the signal you are interested in.

accessible version (incomplete and very dirty)

class Peakdetechtion { // horrible name
public:
  QLineEdit* getLineEdit() { return m_lineEdit; } // don't do it

private:
  QLineEdit* m_lineEdit;
};

class Peaksettingform : public QObject { //horrible name
  Q_OBJECT
public:
  Peaksettingform(Peakdetechtion *p, QObject *parent = 0)
  : QObject(parent) {
    // you can do this from outside and replace 'this' with a pointer to a Peaksettingform object 
    connect(p->getLineEdit(), SIGNAL(textChanged(const QString &)), this, SLOT(handleText(const QString &))); 
}

public slots:
  void handleText(const QString &);
};

signal forwarding

class Peakdetechtion : public QObject { // horrible name
Q_OBJECT
public:
  Peakdetechtion() {
    m_lineEdit = new QLineEdit(); // should have a parent but i am lazy
    connect(m_lineEdit, SIGNAL(textChanged(const QString&)), this, SIGNAL(leTextChanged(const QString&)));
  }

signals:
  void leTextChanged(const QString &);

private:
  QLineEdit* m_lineEdit;
};

class Peaksettingform : public QObject { //horrible name
  Q_OBJECT
public:
  Peaksettingform(Peakdetechtion *p, QObject *parent = 0)
  : QObject(parent) {
    // you can do this from outside and replace 'this' with a pointer to a Peaksettingform object 
    connect(p, SIGNAL(leTextChanged(const QString &)), this, SLOT(handleText(const QString &))); 
}

public slots:
  void handleText(const QString &);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM