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. Signals and slots Connect
Forum Updated to NodeBB v4.3 + New Features

Signals and slots Connect

Scheduled Pinned Locked Moved Solved General and Desktop
signals & slotsconnect slot
4 Posts 2 Posters 2.6k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    Core2
    wrote on last edited by Core2
    #1

    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)

    mainview.h

    private slots:
        void printData(QString);
    

    mainview.cpp

    MainView::MainView(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::MainView)
    {
        ui->setupUi(this);
    }
    void MainView::printData(QString tnum)
    {
            ui->LbltNum->setText(tnum);
    }
    

    profile.h

    signals:
    void sendData(QString);
    

    profilesearch.cpp

    void ProfileSearch::on_btnOk_clicked()
    {
        connect(this, SIGNAL(sendData(QString)), this, SLOT(printData(QString)));
        emit sendData("test");
        qDebug() << "Ok button clicked.";
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      -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)));
      ps.exec();
      }
      

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

      1 Reply Last reply
      3
      • C Offline
        C Offline
        Core2
        wrote on last edited by
        #3

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

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

        mrjjM 1 Reply Last reply
        0
        • C Core2

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

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

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Core2
          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.

          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