qt connect cilds按钮

时间:2012-09-09 18:48:23

标签: qt parent-child connect

我有

class MainWindow : public QMainWindow  { 

    Q_OBJECT

  public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

  private slots:
    void getData();

  private:
    Ui::MainWindow *ui;
    Dialog  *second;
};

class Dialog: public QDialog  {
   Q_OBJECT

  public:
    explicit Dialog(QWidget *parent = 0); QDialog * dialog;

    QPushButton *pushButton;
    QPushButton *pushButton_2;

};

我可以将类Dialog中的按钮与MainWindow类中的函数getData()连接(Dialog是Mainwindow类的子类)

我试过

connect(*second->pushButton, SIGNAL(clicked()), this,
                             SLOT(getData()));

但我得到了

error: no matching function for call to
‘MainWindow::connect(QPushButton&, const char [11], MainWindow* const, const char [11])’

我如何连接它们?

1 个答案:

答案 0 :(得分:0)

如果对话框是QMainWindow子类的子对象(就像它一样),你应该有类似的东西:

MainWindow::MainWindow(...)
{
  ....

  m_dialog = new Dialog(this);// in .h file it is defined as: "Dialog *m_dialog;"
  ....

  connect(m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));
}

你不必写:

  connect(*m_dialog->pushButton, SIGNAL(clicked()), this,SLOT(getData()));

查看this qt代码:

Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
                 &b, SLOT(setValue(int)));

a.setValue(12);     // a.value() == 12, b.value() == 12
b.setValue(48);     // a.value() == 12, b.value() == 48

你可以看到你必须使用指针而不是QObject。