Important: Please read the Qt Code of Conduct -

Signals and slots Connect

  • Hi All,

    If I have two forms, mainview and profilesearch, and am setting up a signal and slot between them.
    What should the connect all line look like?
    The current issue I'm having is i'm using "this" twice in the send object and the receive object. This is the error I get:
    "QObject::connect: No such slot ProfileSearch::printData(QString) in ..\profilesearch.cpp:80
    QObject::connect: (sender name: 'ProfileSearch')
    QObject::connect: (receiver name: 'ProfileSearch')

    This makes sense because I'm trying to connectl from profilesearch to profilesearch. But the class member function printData(QString); belongs to mainview. This brings me to my questions what should I replace the second "this" in the connect line of code? I've tried "MainView", "mainview", "MainView::mainview" Nothing I've tried works. What am I missing here?

    The following code only includes what I think should be stuff pertaining to my question. (I cut the fat)


    private slots:
        void printData(QString);


    MainView::MainView(QWidget *parent) :
        ui(new Ui::MainView)
    void MainView::printData(QString tnum)


    void sendData(QString);


    void ProfileSearch::on_btnOk_clicked()
        connect(this, SIGNAL(sendData(QString)), this, SLOT(printData(QString)));
        emit sendData("test");
        qDebug() << "Ok button clicked.";

  • Lifetime Qt Champion

    -What am I missing here?
    That you need a actual pointer to the widgets. So
    unless you give ProfileSearch a pointer to the MainView, it cant work.

    You should connect in MainView instead.
    Where you create ProfileSearch to show it.

    There you know both objects

    void MainView::ShowProfileSearch() {
    ProfileSearch ps;
    connect(&ps, SIGNAL(sendData(QString)), this, SLOT(printData(QString)));

    Alternatively, you can cast the parent of ShowProfileSearch if its MainView and connect that way. Its slightly ugly though.

  • @mrjj
    Thank you for your quick response! This WORKED!!!!!!!!!!!

    Seriously thank you! I've been struggling with this for far too long.

  • Lifetime Qt Champion

    You are welcome :)

    it takes some time to get used to design/program
    with signals and slots.
    It can be a bit of a challenge to have pointers to both objects in same place but often the mainwindow is a good place.

    Note that its also ok to connect signal to signal.
    This can be used to surface some signals from inside a class to outside world.
    Like if you have a dialog with an TextEdit. The textEdit is private so outside cannot
    connect directly to it.
    You can then connect signal to a new public signal and the outside world can connect to this new signal. ( like TextReady)

    The good thing about this is that if you one day change the TextEdit to a combobox, the rest of the program do not need to change. It will still get the data via the new public signal TextReady.
    In a non trivial program, such encapsulation/hiding of details is
    worth every penny.