Home > Back-end >  Inheriting singals from child widgets
Inheriting singals from child widgets

Time:06-26

I created a Custom widget in QT, i use it in my main program but i cant use the signals from a QLineEdit, how can i do it?, this is mi code:

 search = new Search_browser(ui->widget);
 connect(search, SIGNAL(returnPressed()), this, SLOT(set_page()));

Search_browser has a QLineEdit, that is why im trying to use returnPressed() signal, this should call a function in my MainWaindow to set a page but i get this error:

QObject::connect: No such signal Search_browser::returnPressed()

Header file of the custom widget:

class Search_browser : public QWidget
{
    Q_OBJECT
    public:
        Search_browser(QWidget *parent = nullptr);
        ~Search_browser();
        QString get_url();
    public slots:
        void replyFinished(QNetworkReply *reply);
        void get_request();

    private:
        QLineEdit *lineEdit;
        QCompleter *completer;
        QStringList wordList;
};

i think that i have to implement myself the signal but i dont know how.

CodePudding user response:

You aggregate QLineEdit in Search_browser so now it's incapsulated (private), to make it accessable from outside world (from MainWindow) you can do one of two things: add accessor to it and connect to returned value.

class Search_browser : public QWidget {
...
QLineEdit *getLineEdit() {return lineEdit;}
};

connect(search->getLineEdit(), SIGNAL(returnPressed()), this, SLOT(set_page()));

Or add signal to Search_browser and forward it.

class Search_browser : public QWidget {
signals:
void returnPressed();
};

Search_browser::Search_browser() {
...
connect(lineEdit,SIGNAL(returnPressed()),this,SIGNAL(returnPressed()));
}
  •  Tags:  
  • c qt
  • Related