Passing QList as a parameter for slot



  • In my form I have two buttons, each button must be connected to a different slot. And each slot should receive as parameter, the reference to an object QList. eg

    @QList list = new QList();

    void MyClass::save(QList *qlits) // Slot Save
    {
    }

    void MyClass::test(QList *qlist) // Slot test
    {
    }
    @

    How to make the sign of a button "Save" send 'list' reference for the slot 'save' ?

    There are claims that this can be done with QSignalMapper, but the documentation is not very clear on how I can do this.



  • maybe @QObject::connect()@ will do it for you.. see the documentation about slot and signal


  • Lifetime Qt Champion

    Hi,

    QSignalMapper won't help you here especially for a QList *. First thing, unless you are modifying your list, there's no reason to pass a pointer to it, use a const reference. Qt's container are implicitly shared so there's no unneeded copy done.

    Depending no where this list comes from, you will have to use an intermediate slot but are you sure you need that ?

    Can you show what you would like to achieve ?



  • This topic is related to another need mentioned in my other "topic":http://qt-project.org/forums/viewthread/46517/.

    Putting it all together, what I need is to catch all QLineEdits of a form and pass to a slot, to be processed. Below my current code

    @Configconnec::Configconnec(QSettings *settings, QObject *parent) : QObject(parent)
    {

    DynamicQtWidgets *dyqtwi = new DynamicQtWidgets(this); // GUI Generator

    QDialog *connecdiag = dyqtwi->createWidget("configconnec.ui");   // Creating GUI
    

    QDialogButtonBox *qdbb_okcancel = connecdiag->findChild("qdbb_okcancel", Qt::FindDirectChildrenOnly);
    QPushButton *qpb_test = connecdiag->findChild("qpb_test", Qt::FindDirectChildrenOnly);

    QList<QLineEdit*> lineedits = connecdiag->findChildren<QLineEdit*>(QString(), Qt::FindDirectChildrenOnly); //Get all lineedits and store in QList

    connect(qdbb_okcancel, SIGNAL(accepted()), this, SLOT(saveParameters()));
    connect(qpb_test, SIGNAL(clicked()), this, SLOT(testParameters()));
    }

    void Configconnec::saveParameters() /Slot to save parameters in disc/
    {
    }

    void Configconnec::testParameters() /Slot to test informed parameters/
    {
    }
    @

    The signal 'accepted' of QDialogButtonBox should send 'lineedits' to 'saveParameters' slot.
    Within saveParameters, QLineEdits objects must be accessed to get content.

    [quote author="SGaist" date="1409602337"]Hi,

    QSignalMapper won't help you here especially for a QList *. First thing, unless you are modifying your list, there's no reason to pass a pointer to it, use a const reference. Qt's container are implicitly shared so there's no unneeded copy done.

    Depending no where this list comes from, you will have to use an intermediate slot but are you sure you need that ?

    Can you show what you would like to achieve ?[/quote]



  • Ok. now you have all the QLineEdit in you lineEdits - QList. did you tried something like
    @void Configconnec::saveParameters(){
    for (int var = 0; var < lineEdits.count(); ++var) {
    lineEdits.value(var)->text(); // here you hold the value of each QLineEdit
    /process each value/
    }
    }@



  • How to know which QLineEdit will be accessed? Each content will have a different destination, how to know which one I am accessing?

    The saveParameters slot will have access to lineedits? 'lineedit' is in scope of function.
    I would have to make it a class member, and I did not want to do that. In this case, I prefer to leave 'dyqtwi' in class scope. Thus a QList would not be necessary.

    [quote author="arsinte_andrei" date="1409644836"]Ok. now you have all the QLineEdit in you lineEdits - QList. did you tried something like
    @void Configconnec::saveParameters(){
    for (int var = 0; var < lineEdits.count(); ++var) {
    lineEdits.value(var)->text(); // here you hold the value of each QLineEdit
    /process each value/
    }
    }@[/quote]



  • you have to have some unique name or some identifier.
    @lineEdits.value(var).objectName@
    try to get the value with @QString objectName() const @ this will return a QString - is unique.

    also set a unique id with
    @void setObjectName(const QString & name)@
    an like that you know what are you writing and where


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.