连接 QSignalMapper QT 5.15.2

Connecting QSignalMapper QT 5.15.2

我正在尝试将两个 QPushButton 映射到 openLanguageDialog(QPushButton *button) 函数。我收到以下错误:

我对如何解决这些错误或它们的含义一无所知,如果有解决方案或解释就好了。

mainwindow.cpp

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

    signalMapper = new QSignalMapper(this)

    QObject::connect(ui->translatedPushButton, &QPushButton::clicked, signalMapper,  &QSignalMapper::map);
    signalMapper->setMapping(ui->translatedPushButton, ui->translatedPushButton);
    QObject::connect(signalMapper, &QSignalMapper::mappedWidget, this, &MainWindow::openLanguageDialog);

    QObject::connect(ui->translationPushButton, &QPushButton::clicked, signalMapper,  &QSignalMapper::map);
    signalMapper->setMapping(ui->translationPushButton, ui->translationPushButton);
    QObject::connect(signalMapper, &QSignalMapper::mappedWidget, this, &MainWindow::openLanguageDialog);

}

void MainWindow::openLanguageDialog(QPushButton *button)
{
    QVector<QOnlineTranslator::Language> currentLang;
    languageDialog = std::make_unique<AddLanguageDialog>(currentLang, this);
    languageDialog->show();
    if (languageDialog->exec() == QDialog::Accepted)
    {
        button->setText(QVariant(languageDialog->languages()[0]).toString());
        setLanguage(languageDialog->languages()[0]);
    }
}

#include <QMainWindow>
#include <QSignalMapper>
#include "addlanguagedialog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
 public slots:
    void openLanguageDialog(QPushButton *button);
private:
    Ui::MainWindow *ui
    QSignalMapper *signalMapper;
    std::unique_ptr<AddLanguageDialog> languageDialog;
};
    }

您遇到以下问题:

  • 连接有问题,因为存在信号和槽重载,编译器无法理解它。

  • 即使发送者是继承自QWidget的QPushButton对象,Qt也不做任何转换(cast)。

  • 不要多次连接

解决方法是:

*.h

public slots:
   void openLanguageDialog(int id);

*.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    signalMapper = new QSignalMapper(this);
    connect(signalMapper, &QSignalMapper::mappedInt, this, &MainWindow::openLanguageDialog);

    connect(ui->translatedPushButton, &QAbstractButton::clicked, signalMapper, QOverload<>::of(&QSignalMapper::map));
    signalMapper->setMapping(ui->translatedPushButton, 0);

    connect(ui->translationPushButton, &QPushButton::clicked, signalMapper,  QOverload<>::of(&QSignalMapper::map));
    signalMapper->setMapping(ui->translationPushButton, 1);
}

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

void MainWindow::openLanguageDialog(int id)
{
    if(QPushButton *button = qobject_cast<QPushButton *>(signalMapper->mapping(id))){
        QVector<QOnlineTranslator::Language> currentLang;
        languageDialog = std::make_unique<AddLanguageDialog>(currentLang, this);
        languageDialog->show();
        if (languageDialog->exec() == QDialog::Accepted)
        {
            button->setText(QVariant(languageDialog->languages()[0]).toString());
            setLanguage(languageDialog->languages()[0]);
        }
    }
}