No such slot button QT

孤街醉人 提交于 2019-12-11 19:24:47

问题


for(i=0; i<height; i++)
{
    for(j=0; j<width; j++)
    {
        button[i][j] = new QPushButton("Empty", this);
        button[i][j]->resize(40, 40);
        button[i][j]->move(40*j, 40*i);
        connect(button[i][j], SIGNAL(clicked()), this, SLOT(changeText(button[i][j])));
    }
}

If i changed function changeText with function (fullScreen for example) it works but when i use a slot defined by me (changeText) this Error Appears and i don't know how to solve it

QObject::connect: No such slot buttons::changeText(&button[i][j])

and this is the function changeText:

void buttons::changeText(QPushButton* button)
{
    button->setText("Fish");
}

NOTE: in the header file i defined the slot like this :

class buttons : public QWidget

    Q_OBJECT
public slots:
    void changeText(QPushButton* button);

回答1:


  1. slot can have less arguments then signal but type of arguments it has must match exactly with types of arguments in connected signal.
  2. you can't have dynamic slot like that.
  3. probably what you need is a QSignalMapper.

here is sample:

QSignalMapper *map = new QSignalMapper(this);
connect (map, SIGNAL(mapped(QString)), this, SLOT(changeText(QString)));
for(i=0; i<height; i++)
{
    for(j=0; j<width; j++)
    {
        button[i][j] = new QPushButton("Empty", this);
        button[i][j]->resize(40, 40);
        button[i][j]->move(40*j, 40*i);
        connect(button[i][j], SIGNAL(clicked()), map, SLOT(map()));
        map->setMapping(button[i][j], QString("Something%1%2").arg(i).arg(j));
    }
}

Probably you can remove a table.




回答2:


If the SIGNAL doesn't provide certain parameter, the SLOT can't recieve it. The signal clicked() doesn't provide any parameter. SLOTs receiving it shouldn't have any, either. In any case, you can have a SLOT receiving less parameters than the SIGNAL provides (ignoring some others), but not otherwise. You can, however, get to know the sender of the signal, cast it to QPushButton* and work on it:

void buttons::changeText()
{
    QPushButton *pb = qobject_cast<QPushButton *>(sender());
    if (pb){
        pb->setText("fish");
    } else {
        qDebug() << "Couldn't make the conversion properly";
    }
} 



回答3:


QButtonGroup is a class that has been designed as a handy collection for buttons. It give you direct access to the button which triggered the slot. It also provide you the possibility to register button with a given id. This can be useful if you want to retrieve easily some meta information from the button id.

QButtonGroup* buttongrp = new QButtonGroup();

for(i=0; i<height; i++)
{
    for(j=0; j<width; j++)
    {
        button[i][j] = new QPushButton("Empty", this);
        button[i][j]->resize(40, 40);
        button[i][j]->move(40*j, 40*i);
        buttongrp->addButton(button[i][j], i << 16 + j);
    }
}

QObject::connect(buttongrp, SIGNAL(buttonClicked(int)), 
                      this, SLOT(getCoordinates(int)));
QObject::connect(buttongrp, SIGNAL(buttonClicked(QAbstractButton  *)),
                   this, SLOT(changeText(QAbstractButton  * button)));

...

void MyObject::changeText(QAbstractButton * button)
{
    button->setText("Fish");
}

void MyObject::getCoordinates(int id){
  int i = id >> 16;
  int j = ~(i << 16) & id;
  //use i and j. really handy if your buttons are inside a table widget
}

Usually you don't need to connect to both slots. For the id I assumed that height and width are less that 2^16.

Retrospectively, It seems to me you are reimplementing some of the functions of the button group.



来源:https://stackoverflow.com/questions/17137797/no-such-slot-button-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!