PDA

View Full Version : QSignalMapper



axisdj
15th September 2010, 23:27
here is what i have, just doing some test to get familiar with QT Creator. This compiles but when I click one of the buttons the clicked signal does not trigger. Any Ideas?



#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




#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


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("Sequence");
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;
}

void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}

wysota
16th September 2010, 01:20
Make your "clicked" a slot, not a signal.

axisdj
16th September 2010, 01:38
Thanks you so much, the QT documentation(QSignalMapper) I used, stated to use signal... changed to slot everything works now. Much appreciated

wysota
16th September 2010, 01:39
Thanks you so much, the QT documentation(QSignalMapper) I used, stated to use signal...
No, it didn't, read again.

axisdj
16th September 2010, 01:44
I must be missing something, I am a complete newbie, but here is the sample code from the QSignalMapper



class ButtonWidget : public QWidget
{
Q_OBJECT

public:
ButtonWidget(QStringList texts, QWidget *parent = 0);

signals:
void clicked(const QString &text); ///Uses Signals???

private:
QSignalMapper *signalMapper;
};
The only function that we need to implement is the constructor:

ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
: QWidget(parent)
{
signalMapper = new QSignalMapper(this);

QGridLayout *gridLayout = new QGridLayout;
for (int i = 0; i < texts.size(); ++i) {
QPushButton *button = new QPushButton(texts[i]);
connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
signalMapper->setMapping(button, texts[i]);
gridLayout->addWidget(button, i / 3, i % 3);
}

connect(signalMapper, SIGNAL(mapped(const QString &)),
this, SIGNAL(clicked(const QString &))); ////Notice it says SIGNAL????

setLayout(gridLayout);
}

wysota
16th September 2010, 01:48
This is a completely different situation. Notice that in the docs the "clicked" method is not implemented. It is a signal which is then connected to some other slot (which is not part of the example). Since you implemented "clicked" this can't be a signal (and the compiler should refuse to build such a program). It would be ok to use a signal if you moved the body of "clicked" to another method (slot), most probably in a different object.

axisdj
16th September 2010, 01:52
Thanks again for taking the time to explain, i really appreciate it.:)