简体   繁体   English

QSignalMapper的用法; 多个QPushButton分配给一个信号

[英]QSignalMapper Usage; Multiple QPushButtons assigned to one signal

Edit: The code below has been updated to the working solution. 编辑:下面的代码已更新为工作的解决方案。

As an attempt to introduce myself to QT, I am trying to design a chessboard out of 64 QPushButtons. 为了向自己介绍QT,我尝试使用64个QPushButtons设计一个棋盘。 I know this might not be the best way to do this, but I believe starting with the basics is a good way to learn. 我知道这可能不是最好的方法,但是我相信从基础开始是学习的好方法。 Anyways, all of the 64 pushbuttons will essentially do the same thing. 无论如何,所有64个按钮实际上都将执行相同的操作。 All of the button's clicked signals will call the same function, passing to the function the associated QPushButton. 按钮的所有单击信号都将调用相同的函数,并将关联的QPushButton传递给该函数。 Instead of individually creating each QPushButton in qtcreator & qtdesigner, and individually creating each on_clicked signal function for each button, I am trying to apply a QSignalMapper. 我正在尝试应用QSignalMapper,而不是分别在qtcreator和qtdesigner中创建每个QPushButton,并为每个按钮单独创建每个on_clicked信号函数。 This is what I have so far: 这是我到目前为止的内容:

chess.h chess.h

#ifndef CHESS_H
#define CHESS_H

#include <QMainWindow>
#include <QSignalMapper>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>

namespace Ui {
class Chess;
}

class Chess : public QMainWindow
{
    Q_OBJECT

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

public slots:
    void On_Clicked(int location);

private:
    Ui::Chess *ui;

    QPushButton *buttons[64];
};

#endif // CHESS_H

chess.cpp chess.cpp

#include "chess.h"
#include "ui_chess.h"

Chess::Chess(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Chess)
{
    QSize button_size(100, 100);
    ui->setupUi(this);

    QGridLayout *layout = new QGridLayout;
    QSignalMapper *signalMapper = new QSignalMapper(this);
    connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(On_Clicked(int)));

    for (int i = 0; i < 64; i++) {
        QString t = QString::number(i);
        buttons[i] = new QPushButton(t, this);
        buttons[i]->setMinimumSize(button_size);
        signalMapper->setMapping(buttons[i], i);
        connect(buttons[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
        layout->addWidget(buttons[i], i / 8, i % 8);
    }

    QWidget* central_widget = new QWidget(this);
    central_widget->setLayout(layout);
    setCentralWidget(central_widget);

}

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

void Chess::On_Clicked(int location) {
    QMessageBox msgbox;
    msgbox.setText(QString::number(location) + " was pushed");
    msgbox.exec();

}

This creates the following output: 这将创建以下输出:

在此处输入图片说明

I was hoping the behavior of clicking on any buttons would bring up a QMessageBox saying that the button was pushed, as defined in the On_Clicked function. 我希望单击任何按钮的行为都会弹出一个QMessageBox,说该按钮已按下,如On_Clicked函数中所定义。 Obviously I am misunderstanding something but I cannot figure it out. 显然我误会了一些东西,但是我无法弄清楚。 Is the QSignalMapper designed for this type of behavior? QSignalMapper是否设计用于此类行为? I thought it was after reading this example. 我以为是看了这个例子。

Thanks! 谢谢!

On_Clicked is a signal, thus you need to connect a slot to it. On_Clicked是信号,因此您需要将插槽连接到它。 Or just change On_clicked into slot and connect signal mapped(int) to it (use SLOT keyword then). 或者只是将On_clicked更改为插槽,然后将映射的信号(int)连接到插槽(然后使用SLOT关键字)。

And notice that On_Clicked function you defined is not a class method which you would need. 并请注意,您定义的On_Clicked函数不是您需要的类方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM