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. QSignalMapper and SegFault
Qt 6.11 is out! See what's new in the release blog

QSignalMapper and SegFault

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k 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.
  • Z Offline
    Z Offline
    zigma12
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zigma12
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          @signalMapper = new QSignalMapper(this);@

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            zigma12
            wrote on last edited by
            #5

            ok fine
            That's great
            i will try it tomorrow

            Thank you much

            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