Вопрос

I want to assign three button's slot to a signal.

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QSignalMapper *signalmapper;
    Ui::MainWindow *ui;
    void test(int);

signals:
    void clickmee(int);

public slots:
    void setclick1(int);
    void setclick2(int);
    void setclick3(int);
};

I do this in constructor:

signalmapper = new QSignalMapper(this);
signalmapper->setMapping(ui->pushButton, 10);
signalmapper->setMapping(ui->pushButton1, 10);
signalmapper->setMapping(ui->pushButton1, 10);
connect(signalmapper, SIGNAL(mapped(int)),this,SLOT(clickmee(int)));

but when I run program in wrote:

QObject::connect: No such slot MainWindow::clickmee(int) in ..\Qsignal2\mainwindow.cpp:11 QObject::connect: (receiver name: 'MainWindow')

How can I assign slots to signal ? How can I send parameter to a slot ?

EDIT: I want to do this:

void  setclick1(int x)
{
    if(x== 10)
    {
        // ....
    }
    else
    {
        // ...
    }
}

but this function not firing. How can I do that?

Это было полезно?

Решение

you should try

connect(signalmapper, SIGNAL(mapped(int)),this, SIGNAL(clickmee(int)));

I'm not sure if this is still allowed in Qt4 or Qt5, but it was valid in Qt3. It simply resends the signal signalmapper.mapped(x) to this.clickmee(x)

If this doesn't work, you should probably write a real slot, connect it to signalamapper.mapped and emit the clickmee there. Something like this:

public slots:
 void clickmee_slot(int x) {
  emit clickmee(x);
 }

and in the constructor:

connect(signalmapper, SIGNAL(mapped(int)),this, SLOT(clickmee_slot(int)));
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top