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. Qt Signal, passing parameters to slot.
QtWS25 Last Chance

Qt Signal, passing parameters to slot.

Scheduled Pinned Locked Moved Unsolved General and Desktop
signalparameters
5 Posts 3 Posters 7.4k 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.
  • C Offline
    C Offline
    cherple
    wrote on 4 Nov 2015, 02:47 last edited by p3c0 11 Apr 2015, 06:30
    #1

    Here's the situation, I have a database which I am reading from, and a struct which I created that will read the data from the DB and copied into my struct. A QList<struct> is used, and this QList stores a list of structs(which is each record in the DB). Inside this DB, there is a field which stores a pointer that points to a variable which I want to connect to, and when the value changed, I want to pass the struct to my slot. Is it possible? My code structure is as shown below:

    /* -- struct = each record in the DB -- */

    typedef struct{
    //my struct here
    }condition;
    

    /* -- QList stores struct, QList stores the entire DB records -- */

    QList<condition> conditionList;
    

    /* -- check if var with within lower and upper limit, if so return true -- */

    bool compareLimit(condition cond, int var){
         if (var >= cond.lowerLimit && var <= cond.upperLimit)
         return true;
    else return false;
    }
    
    void storeToStruct(){
         for (int x=0; x<DB.length(); x++){
              conditionList.append(DB.record(x);
              connect(conditionList.at(x).var, SIGNAL(valueChanged()), this, SLOT(compareLimit()));
         }
    }
    

    So what is happening here is every time a record is being read from the database, I want to run a connect statement which will connect a 'var' to 'valueChanged()', and call the generic slot function 'compare'. Problem is I have no idea how do I pass the particular record into the slot function.

    Will anyone please give me some suggestions or advice? Any suggestions/advice is greatly appreciated. This is a dead-end for me T_T

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Nov 2015, 07:31 last edited by
      #2

      What is var? If it is not derived from QObject then it cannot have any signals.

      Your struct needs to inherit from QObject and you have to add Q_OBJECT makro at the beginning of your struct. Only classes/structs derived from QObject can have signals. Then you add valueChanged() signal to your struct. In compareLimit() slot you can call sender() to get the object which emited the signal (an instance of your struct in this case).
      connect(conditionList.at(x), SIGNAL(valueChanged()), this, SLOT(compareLimit()));

      You should read http://doc.qt.io/qt-5.5/signalsandslots.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sneubert
        wrote on 4 Nov 2015, 08:15 last edited by
        #3

        This is usually accomplished by using a QSignalMapper (http://doc.qt.io/qt-5/qsignalmapper.html).

        1 Reply Last reply
        0
        • C Offline
          C Offline
          cherple
          wrote on 4 Nov 2015, 08:31 last edited by
          #4

          Okay, sorry, I didn't point this out. Let's just say 'var' is a custom type created by someone else and it is able to have signal. It has been tested and proven to work.

          What I am trying to achieve now is when I'm in the for-loop, a connect statement is called, and compareLimit() will be called when value is changed. Problem is compareLimit() doesn't know which upper and lower limit to compare 'var' against, because there is a QList of struct.

          I want conditionList.at(x).var to be compared against conditionList.at(x).lowerLimit and conditionList.at(x).upperLimit.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sneubert
            wrote on 4 Nov 2015, 08:59 last edited by
            #5

            As long as var inherits QObject you can do as followed.

            QSignalMapper *sigMap = new QSignalMapper();
            connect(sigMap,SIGNAL(mapped(QObject *)),this,SLOT(compareLimit(QObject*)));
            
            void storeRoStruct()
            {
                for (int x=0; x<DB.length(); x++){
                      conditionList.append(DB.record(x);
                      connect(conditionList.at(x).var, SIGNAL(valueChanged()), sigMap, SLOT(map()));
                      sigMap->setMapping(conditionList.at(x).var, conditionList.at(x).var);
                 }
            }
            

            And in the Slot make a dynamic_cast to var.

            1 Reply Last reply
            0

            1/5

            4 Nov 2015, 02:47

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved