利用Qt元对象系统获取界面上发生更改的控件代码

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QMetaMethod slot = metaObject()->method(
                         metaObject()->indexOfSlot("functChanged()"));
    foreach (QWidget* w, findChildren<QWidget*>()) {
      const QMetaObject *mo = w->metaObject();
      if (!mo->userProperty().isValid() || !mo->userProperty().hasNotifySignal())
        continue;
      connect(w, mo->userProperty().notifySignal(), this, slot);
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::functChanged()
{
    qDebug() << sender();
    qDebug() << "this is a signal!!!";
}
发布了1 篇原创文章 · 获赞 0 · 访问量 8

猜你喜欢

转载自blog.csdn.net/huoshifu/article/details/104626190