QSignalMapper and SegFault



  • Hi guys!

    I'm a little embarrassed to ask but I've been looking for an answer to my problem for 3 days...
    It's certainly a silly thing but I can't see it :(

    OK, here's the problem.
    I try to map the elements of a QVector<QPushbutton*> to QSignalMapper*.
    The deal is to send the QPushButton*'s rank in the QVector to a SLOT.

    below is my .h:
    @#ifndef PLANNING_H
    #define PLANNING_H

    #include <QDialog>
    #include <QVBoxLayout>
    #include <QFrame>
    #include <QScrollArea>
    #include <QSignalMapper>
    #include "mainstep.h"

    namespace Ui {
    class Planning;
    }

    class Planning : public QDialog
    {
    Q_OBJECT

    public:
    explicit Planning(QWidget *parent = 0);
    ~Planning();

    private:
    Ui::Planning *ui;

    QVector<QPushButton*> boutons;
    QSignalMapper* signalMapper;
    QVector<MainStep*> mainSteps;
    

    public slots:
    void addMainStep();
    void delMainStep();
    void clicked(int i);
    };

    #endif // PLANNING_H@

    below is my .cpp:
    @#include "planning.h"
    #include "ui_planning.h"
    #include "mainstep.h"
    #include <QDebug>
    #include <QObject>

    Planning::Planning(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Planning)
    {
    ui->setupUi(this);
    this->show();

    QSignalMapper* signalMapper = new QSignalMapper(this);
    
    QObject::connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(clicked(int)));
    QObject::connect(ui->pushButton_addMainStep, SIGNAL(clicked()), this, SLOT(addMainStep()));
    QObject::connect(ui->pushButton_delMainStep, SIGNAL(clicked()), this, SLOT(delMainStep()));
    
    Planning::addMainStep();
    

    }

    Planning::~Planning()
    {
    delete ui;
    }

    void Planning::addMainStep()
    {
    QString bouton_legend = "√Čtape " + QString::number(boutons.size()+1);

    boutons.append(new QPushButton(bouton_legend));
    
    boutons.last()->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    boutons.last()->setMaximumHeight(100);
    ui->verticalLayout_MainStepsList->addWidget(boutons.last());
    ui->verticalLayout_MainStepsList->setAlignment(Qt::AlignTop);
    
    QObject::connect(boutons.last(), SIGNAL(clicked()), signalMapper, SLOT(map()));
    signalMapper->setMapping(boutons.last(), boutons.size());
    
    mainSteps.append(new MainStep);
    
    }
    

    void Planning::delMainStep()
    {
    if (boutons.size()<2) return;
    delete boutons.last();
    boutons.removeLast();

    delete mainSteps.last();
    mainSteps.removeLast();
    

    }

    void Planning::clicked(int i)
    {
    qDebug() << "Clicked " << i;
    //ui->verticalLayout_frame_parameters->addWidget(mainSteps.value(i));
    //mainstep->show();
    }

    @

    The crash occurs at line #47 of the .cpp.

    It is not a problem of creation of the QPushButtons because, the do appear in the window if I comment the two faulty lines (#47-78).

    As I told you, I have seen many websites with the same problems but, in general, it was simply a lack of variable declarations (mapper, etc.)
    I do not see these problems in my case.

    I do not know if these infos are sufficient.
    Feel free to ask for more details

    Thank you all

    (and sorry for my english, to those who reached the end of my post)

    EDIT:of course the program compiles and the the compiler does not complain


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are shadowing signalMapper in the constructor so your member variable is not initialized



  • Hi,
    thank you for your quick reply

    What do you mean by shadowing?

    How should I change this line?
    @
    QSignalMapper* signalMapper = new QSignalMapper(this);@

    thanks


  • Lifetime Qt Champion

    "Here":http://en.wikipedia.org/wiki/Variable_shadowing is an explanation of variable shadowing

    @signalMapper = new QSignalMapper(this);@



  • ok fine
    That's great
    i will try it tomorrow

    Thank you much


Log in to reply
 

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