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

如何获取qt中的SelectionChange事件

  •  7
  • Naruto  · 技术社区  · 15 年前

    我有一个继承自 QWidget ,现在在该类中,我将创建一个 QListView 对象并填充要查看的项。 当列表视图中的项目选择发生更改时,我希望 selectionChange 事件。

    我怎样才能做到这一点?请简单地告诉我。

    2 回复  |  直到 6 年前
        1
  •  10
  •   Angie Quijano Nejat    8 年前

    当您有一个视图时,您将有一个用于选择项目的模型。它叫做 QItemSelectionModel .

    例如,使用 QListView ,您可以通过以下方式获取SelectionModel:

    QItemSelectionModel* selectionModel() const;
    

    现在,从这个模型中,您可以连接许多信号:

    void currentChanged ( const QModelIndex & current, const QModelIndex & previous )
    void currentColumnChanged ( const QModelIndex & current, const QModelIndex & previous )
    void currentRowChanged ( const QModelIndex & current, const QModelIndex &    previous )
    void selectionChanged ( const QItemSelection & selected, const QItemSelection & deselected )
    

    我想这对你有点帮助!

        2
  •  0
  •   Ronny Brendel    15 年前

    http://doc.trolltech.com/4.6/qlistwidget.html 您可能想使用qlistwidget而不是view,我不记得具体的原因,但是这个类有您想要使用的这些信号。


    http://doc.trolltech.com/4.6/qlistwidget.html#itemSelectionChanged 这是您必须连接的信号。

    在类声明中插入一个槽:

     private slots:
         void selChanged();
    

    用您希望在选择更改时执行的操作填充此槽。 将信号连接到类中的某个位置上的这个插槽-可能是在QWidget派生的构造函数中。

     connect(yourListWidget, SIGNAL(itemSelectionChanged()), this, SLOT(selChanged()));
    

    就是这样