0

以下代码设置复选框列表。

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();

这个想法是,当用户选中或取消选中一个项目时,它会发出一个信号。

在头文件中,我在私有插槽中包含以下内容:

void cbsate(int state);

在 cpp 文件中,我声明了这一点:

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");
    }
}

我现在收到一个错误

没有在类“MainWindow”中声明的“void MainWindow::cbstate(int)”成员函数

有任何想法吗?我这样做对吗?谢谢。

4

2 回答 2

2

听起来好像您没有正确地将方法 cbstate 添加到头文件中。如果您从头文件中复制并粘贴它,那么您有一个错字:

void cbsate(int state);

应该:

void cbstate(int state);
于 2013-08-06T10:34:50.390 回答
1

要解决连接问题,请使用:

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

请注意,使用的是stateChanged,而不是statechanged

于 2016-11-30T13:11:46.463 回答