Anthony Tran
Anthony Tran

Reputation: 40

QConnect find no such slot on QCombobox by Qt Creater

Qtcreator keeps telling:

**QObject::connect: No such slot QWidget::handleHeroChange(int) in ../Testing/Window.cpp:92**

What did i do wrong?

THE CODE:

//Window.h
    class Window : public QWidget
    {
        QGraphicsPixmapItem* avatar;
        QGraphicsTextItem* heroNameItem;
        QGraphicsTextItem* heroStrItem;
        QGraphicsTextItem* heroDexItem;
        QGraphicsTextItem* herointelItem;
      public:
        Window(QWidget *parent = 0);
      public slots:
        void handleHeroChange(int);
    };

//Window.cpp
    Window::Window(QWidget *parent)
        : QWidget(parent)
    {

       QGridLayout *grid = new QGridLayout(this);
       QComboBox *comboHeroClass = new QComboBox();
        comboHeroClass->addItem("Witcher");
        comboHeroClass->addItem("Maurander");
        comboHeroClass->setCurrentIndex(1);
        grid->addWidget(comboHeroClass, 2,3,1,1);
        QComboBox::connect(comboHeroClass, SIGNAL(currentIndexChanged(int)),this, SLOT(handleHeroChange(int)));
    }

    void Window::handleHeroChange(int index){
        QPixmap myImage;
        if(index == 0){

        }else if(index == 1){

        }
    }

Upvotes: 0

Views: 928

Answers (1)

Jablonski
Jablonski

Reputation: 18514

class Window : public QWidget
{
    Q_OBJECT

Add Q_OBJECT macro without semicolon to your class and rebuild your project.

Without this macro moc cannot find your class and doesn't create your slot(and some other things) That's why you get your error: you see your slot but it doesn't exist and you get runtime error

Upvotes: 1

Related Questions