我试图避免将某些对象拖放到树中其他对象下方的情况。为此,我想更改一个功能的工作方式。因此,我尝试将QTreeView用作新类的基类。
然而,QT_Creator引发错误:对“ vTree for RoomTree”的未定义引用
为了消除错误,我在QTreeView中添加了我可以找到的每个虚函数,但仍然无法正常工作。
所以有两个问题1:我需要添加什么才能使其正常工作。2:这真的是从QTreeView创建派生类的唯一方法,还是有办法告诉C ++如果该函数不在类中,请使用基类虚函数。
我确实知道我不需要在派生类中说虚函数,但是它使我清楚地说明了要添加的内容以匹配QTreeView中的虚函数
感谢您能提供的任何帮助。
#ifndef ROOMTREE_H
#define ROOMTREE_H
#include <QTreeView>
class RoomTree : public QTreeView
{
public:
RoomTree(QWidget *parent = nullptr);
// ~RoomTree();
virtual ~RoomTree();
virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>()) override{return QTreeView::dataChanged(topLeft, bottomRight, roles);}
virtual QModelIndex indexAt(const QPoint &point) const override{return QTreeView::indexAt(point);}
virtual void keyboardSearch(const QString &search) override{return QTreeView::keyboardSearch(search);}
virtual void reset() override{return QTreeView::reset();}
virtual void scrollTo(const QModelIndex &index, QAbstractItemView::ScrollHint hint = EnsureVisible) override{return QTreeView::scrollTo(index, hint);}
virtual void selectAll() override{return QTreeView::selectAll();}
virtual void setModel(QAbstractItemModel *model) override{return QTreeView::setModel(model);}
virtual void setRootIndex(const QModelIndex &index) override{return QTreeView::setRootIndex(index);}
virtual void setSelectionModel(QItemSelectionModel *selectionModel) override{return QTreeView::setSelectionModel(selectionModel);}
virtual QRect visualRect(const QModelIndex &index) const override{return QTreeView::visualRect(index);}
protected:
virtual void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const{return QTreeView::drawBranches(painter, rect, index);}
virtual void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{return QTreeView::drawRow(painter, option, index);}
virtual void currentChanged(const QModelIndex ¤t, const QModelIndex &previous) override{return QTreeView::currentChanged(current, previous);}
virtual void dragMoveEvent(QDragMoveEvent *event) override{return QTreeView::dragMoveEvent(event);}
virtual int horizontalOffset() const override{return QTreeView::horizontalOffset();}
virtual bool isIndexHidden(const QModelIndex &index) const override{return QTreeView::isIndexHidden(index);}
virtual void keyPressEvent(QKeyEvent *event) override{return QTreeView::keyPressEvent(event);}
virtual void mouseDoubleClickEvent(QMouseEvent *event) override{return QTreeView::mouseDoubleClickEvent(event);}
virtual void mouseMoveEvent(QMouseEvent *event) override{return QTreeView::mouseMoveEvent(event);}
virtual void mousePressEvent(QMouseEvent *event) override{return QTreeView::mousePressEvent(event);}
virtual void mouseReleaseEvent(QMouseEvent *event) override{return QTreeView::mouseReleaseEvent(event);}
virtual QModelIndex moveCursor(QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers) override{return QTreeView::moveCursor(cursorAction, modifiers);}
virtual void paintEvent(QPaintEvent *event) override{return QTreeView::paintEvent(event);}
virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end) override{return QTreeView::rowsAboutToBeRemoved(parent, start, end);}
virtual void rowsInserted(const QModelIndex &parent, int start, int end) override{return QTreeView::rowsInserted(parent, start, end);}
virtual void scrollContentsBy(int dx, int dy) override{return QTreeView::scrollContentsBy(dx, dy);}
virtual QModelIndexList selectedIndexes() const override{return QTreeView::selectedIndexes();}
virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) override{return QTreeView::selectionChanged(selected, deselected);}
virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) override{return QTreeView::setSelection(rect, command);}
virtual int sizeHintForColumn(int column) const override{return QTreeView::sizeHintForColumn(column);}
virtual void timerEvent(QTimerEvent *event) override{return QTreeView::timerEvent(event);}
virtual void updateGeometries() override{return QTreeView::updateGeometries();}
virtual int verticalOffset() const override{return QTreeView::verticalOffset();}
virtual bool viewportEvent(QEvent *event) override{return QTreeView::viewportEvent(event);}
virtual QSize viewportSizeHint() const override{return QTreeView::viewportSizeHint();}
virtual QRegion visualRegionForSelection(const QItemSelection &selection) const override{return QTreeView::visualRegionForSelection(selection);}
};
#endif // ROOMTREE_H
bnaecker是正确的,我要做的就是将Q_OBJECT添加到该类中。由于某种原因,我还必须进行完整的重建。
此作品:
class RoomTree : public QTreeView
{
Q_OBJECT
public:
RoomTree(QWidget *parent = nullptr);
// ~RoomTree();
~RoomTree(){};
};