0

The following code sets up a list of checkboxes.

QWidget *w = new QWidget(this);
w->setFixedSize(300,200);
QVBoxLayout *vbox = new QVBoxLayout;

foreach(QString filt, filters){
    QCheckBox *checkbox = new QCheckBox(filt, this);
    checkbox->setChecked(true);
    vbox->addWidget(checkbox);

    connect(this, SIGNAL(statechanged(int)), this, SLOT(cbstate(int)));

}

w->setLayout(vbox);
w->show();

The idea is that when the user checks or unchecks an item, it will emit a signal.

In the header file, I have included the following in the private slots:

void cbsate(int state);

And in the cpp file, I have declared this:

void MainWindow::cbstate(int state){
    if(state == 0){
        //unchecked
        QMessageBox::information(this, "Unchecked", "You have unchecked this box");
    }
    else if (state == 2){
        //checked
        QMessageBox::information(this, "Checked", "You have checked this box");
    }
}

I'm now getting an error of

no 'void MainWindow::cbstate(int)' member function declared in class 'MainWindow'

Any ideas? Am I going about this correctly? Thanks.

1
  • You have an error in the way you connect signal and slots try: connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(cbstate(int))); Commented Aug 6, 2013 at 10:49

2 Answers 2

2

It definately sounds as if you have not added the method cbstate to your header file correctly. If you copy and pasted this from your header file, then you have a typo:

void cbsate(int state);

should be:

void cbstate(int state);
Sign up to request clarification or add additional context in comments.

3 Comments

Oops, gutted I made a typo! It's now running without any errors but when I attempt to uncheck a checkbox, I get the following errors: Object::connect: No such signal MainWindow::statechanged(int) in ../Images/mainwindow.cpp:38 Object::connect: (sender name: 'MainWindow') Object::connect: (receiver name: 'MainWindow')
You need to connect the checkbox, not the window, i.e. connect(checkbox, SIGNAL(...), this, SLOT(...));
Sorted, statechanged is stateChanged! Cheers dunc!
1

To solve connect issue use:

connect(this, SIGNAL(stateChanged(int)), this, SLOT(cbstate(int)));

Note that stateChanged is used, not statechanged.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.