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;
}
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;
}