• 4398阅读
  • 1回复

QTreeVidgetItem 有谁熟不?遇到难题了 [复制链接]

上一主题 下一主题
离线gpwang84329
 
只看楼主 正序阅读 楼主  发表于: 2009-11-09
各位大侠,请问个问题:
我的树状结构显示如下:
+devicetree
     +device 0
                  ------channel0
                   ------channel1
                   ------channel2
                   ------channel3
                  ------channelN
          +device 1
                  ------channel0
                   ------channel1
                   ------channel2
                   ------channel3
                  ------channelN

现在我想鼠标右击对设备或通道的属性进行编辑,我是用的信号是clicked和customContextMenuRequested,但是我只能得到index 和鼠标位置的pos,
我无法记录下pos或index对应的通道和设备,treemodel使用的是QT助手里的demo程序,见文后,不知各位大侠有没有更好的办法,我看qtreewidgetitem
类挺好的,但不知如何使用,还请各位大侠释惑,不甚感谢!

class TreeItem
{
public:
     TreeItem(const QList<QVariant> &data, TreeItem *parent = 0);
     ~TreeItem();

     void appendChild(TreeItem *child);

     TreeItem *child(int row);
     int childCount() const;
     int columnCount() const;
     QVariant data(int column) const;
     int row() const;
     TreeItem *parent();

private:
     QList<TreeItem*> childItems;
     QList<QVariant> itemData;
     TreeItem *parentItem;
};


class TreeModel : public QAbstractItemModel
{
     Q_OBJECT

public:
     TreeModel(const QString &data, QObject *parent = 0);
     ~TreeModel();

     QVariant data(const QModelIndex &index, int role) const;
     Qt::ItemFlags flags(const QModelIndex &index) const;
     QVariant headerData(int section, Qt::Orientation orientation,
                         int role = Qt::DisplayRole) const;
     QModelIndex index(int row, int column,
                       const QModelIndex &parent = QModelIndex()) const;
     QModelIndex parent(const QModelIndex &index) const;
     int rowCount(const QModelIndex &parent = QModelIndex()) const;
     int columnCount(const QModelIndex &parent = QModelIndex()) const;

private:
     void setupModelData(const QStringList &lines, TreeItem *parent);

     TreeItem *rootItem;
};
   QTreeView view;
     view.setModel(&model);
     view.setWindowTitle(QObject::tr("Simple Tree Model"));
     view.show();
离线gpwang84329
只看该作者 1楼 发表于: 2009-11-09
呵呵,看来我得自己搞定啦~ 冬天到了,大家注意保暖,冬天快乐!
快速回复
限100 字节
 
上一个 下一个