当尝试在派生的QWizard的构造函数中添加带有自定义Q_PROPERTY的派生QWizardPage时,我得到了以下最小示例的运行时警告:
QObject :: connect:尝试绑定非信号TreeView :: ndex_changed [sic]
在我的原始项目中,字段“index”仍然按预期工作,因此我提供了一个仅发出警告的示例。我怎样才能摆脱这个警告?
我正在使用Qt 5.10.1和gcc 5.3.0(MinGW)。
的CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets)
project(QWizard)
add_executable(${PROJECT_NAME}
main.cpp
TreeView.cpp
Wizard.cpp
WizardPage.cpp
)
target_link_libraries(${PROJECT_NAME}
Qt5::Widgets
)
的main.cpp
#include "Wizard.h"
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication application{argc, argv};
Wizard wizard{};
wizard.show();
return application.exec();
}
TreeView.h
#pragma once
#include <QTreeView>
class TreeView : public QTreeView {
Q_OBJECT
Q_PROPERTY(QModelIndex index READ index NOTIFY index_changed)
public:
explicit TreeView(QWidget* parent = nullptr);
QModelIndex index() const;
signals:
void index_changed();
};
TreeView.cpp
#include "TreeView.h"
TreeView::TreeView(QWidget *parent) : QTreeView(parent) {
connect(this, &QTreeView::clicked, [&]() { emit index_changed(); });
}
QModelIndex TreeView::index() const { return currentIndex(); }
Wizard.h
#pragma once
#include <QWizard>
class Wizard : public QWizard {
public:
explicit Wizard(QWidget* parent = nullptr);
};
Wizard.cpp
#include "Wizard.h"
#include "WizardPage.h"
Wizard::Wizard(QWidget *parent) : QWizard(parent) {
auto *page = new WizardPage{this};
addPage(page);
}
WizardPage.h
#pragma once
#include <QWizardPage>
class WizardPage : public QWizardPage {
public:
explicit WizardPage(QWidget* parent = nullptr);
};
WizardPage.cpp
#include "WizardPage.h"
#include "TreeView.h"
#include <QVBoxLayout>
WizardPage::WizardPage(QWidget *parent) : QWizardPage(parent) {
auto *tree_view = new TreeView{this};
registerField("index*", tree_view, "index", "index_changed");
auto *layout = new QVBoxLayout{this};
layout->addWidget(tree_view);
setLayout(layout);
}
答案 0 :(得分:1)
我认为在registerField()
中您应该使用SIGNAL(index_changed())
代替"index_changed"
通过registerField()
registerField("index*", tree_view, "index", SIGNAL(index_changed()));