PDA

View Full Version : QSignalMapper question



Arend
26th November 2012, 10:55
Hello,

I am trying to understand QSignalMapper, I found this piece of code, see below.
It compiles correct, but its not working, change 'signal' to 'slot' was the answer.
Bu I can't get it working.

Regards,
Arend (absolute beginner)


MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

theMapper = new QSignalMapper(this);
QGridLayout *theLayout = new QGridLayout();

int count = 0;
for (int t = 0; t < 10 ; t++)
{
count++;
QString buttonName;
buttonName.setNum(count);
buttonName.prepend("Button ");
QPushButton *theButton = new QPushButton(buttonName);
theLayout->addWidget(theButton,count,1,1,1);
connect(theButton, SIGNAL(clicked()), theMapper, SLOT(map()));
theMapper->setMapping(theButton,buttonName);
}

centralWidget()->setLayout(theLayout);
connect(theMapper,SIGNAL(mapped(const QString &)),this,SIGNAL(clicked(const QString &)));

}

void clicked(const QString &text)
{

qDebug() << text;
}

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

high_flyer
26th November 2012, 12:00
1. Please use code tags in the future.
2. What is the question?
3. Define "working".
4. What is it that you are trying to achieve?
5. If I understand your code correctly the use of a signal mapper is not needed.

amleto
26th November 2012, 12:07
showing the header would be useful - is void clicked(const QString &text) defined as a slot?

Arend
26th November 2012, 13:34
No void clicked(const QString &text) is defined as a signal


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QtGui>
#include <QGridLayout>
#include <QSignalMapper>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

signals:
void clicked(const QString &text);

protected:
void changeEvent(QEvent *e);

private:
Ui::MainWindow *ui;
QPushButton *theButton;
QGridLayout *theLayout;
QSignalMapper *theMapper;
};

#endif // MAINWINDOW_H

amleto
26th November 2012, 13:43
didn't we already mention code tags??

If clicked is a signal, then why do you have this


void clicked(const QString &text)
{

qDebug() << text;
}
??

You can't have code for a signal.