代码之家  ›  专栏  ›  技术社区  ›  chappar

插槽函数未被调用

  •  0
  • chappar  · 技术社区  · 16 年前

    #include <QApplication>
    #include <QPushButton>
    #include <QLabel>
    #include <QDialog>
    #include <QObject>
    #include <QHBoxLayout>
    
    int main(int argc, char ** argv)
    {
       QApplication app(argc, argv);
       QDialog *dialog = new QDialog;
       QPushButton *testButton = new QPushButton(QObject::tr("test"));   
       QLabel * testLabel = new QLabel (QObject::tr("test"));
       QHBoxLayout * layout = new QHBoxLayout;
       layout->addWidget(testButton);
       layout->addWidget(testLabel);
       QObject::connect(testButton, SIGNAL(toggled(bool)), testLabel, SLOT(setVisible(bool)));
       dialog->setLayout(layout);
       dialog->show();
       return app.exec();
    }
    

    QObject::connect(testButton, SIGNAL(clicked(bool)), testLabel, SLOT(setVisible(bool))); 它使标签消失。

    那么,为什么它不工作的信号“切换”。我猜,它找不到那个信号。你们能发光吗?

    2 回复  |  直到 16 年前
        1
  •  3
  •   chalup    16 年前

    您需要添加:

    testButton->setCheckable(true);
    

        2
  •  1
  •   Kyle Lutz    16 年前

    QPushButton 不要发出 toggled(bool) QCheckBox 去吧。

    看到第一行了吗 QAbstractButton::toggled

    This signal is emitted whenever a checkable button changes its state.