QTreeWidget 和QTreeWidgetItem用法总结

QTreeWidgetItem::addChild(QTreeWidgetItem*)   //用于根节点添加子节点

QTreeWidget::setColumnCount ;                           //用于设置表中的列的列数目,在表头中会有对应的显示,

QTreeWidget::setHeaderHidden();                         //隐藏表头

QTreeWidgetItem::setSortingEnabled(bool)          //可以用来设置是否可以排序,当为true时候,点击表头,会自动排序

openPersistentEditor和closePersistentEditor        //用来控制某一item是否可以编辑

 

QTreeWidgetItem * QTreeWidget::currentItem ()  //const返回当前的item指针,

int QTreeWidget::currentColumn ()                        // const放回当前item的列编号

QTreeWidgetItem * QTreeWidget::itemAt ( int x, int y ) const返回给定的位置的item指针

QTreeWidgetItem * QTreeWidget::itemBelow ( const QTreeWidgetItem * item ) const       //返回指定的item下面item

QTreeWidgetItem * QTreeWidget::itemAbove ( const QTreeWidgetItem * item ) const       //返回指定的item上面item

查找item时候,通QListWidget和QTableWidget一样,同样有多个查找匹配模式

QList QTreeWidget::findItems ( const QString & text, Qt::MatchFlags flags, int column = 0 ) const

 

//显示隐藏列:

setSectionHidden(int, bool);isSectionHidden(int);

增加删除顶层用:

addTopLevelItem(QTreeWidgetItem*);

takeTopLeveltem(int);

topLevelItem(int); //返回

topLevelItemCount();

增加删除子层:

addChild(QTreeWidgetItem*);

addChildren(const QList&);

takeChild(int);

takeChildren();

child(int)  //返回

childCount();

 

//排序

treeWidget->setSortingEnabled(true);

treeWidget->header()->setSortIndicatorShown(true);

treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);

//要自定义就用信号

connect( treeWidget->header(), SIGNAL( sectionClicked(int) ), this, SLOT( sectionClickedSlot(int) ) );

 

添加表头的两种方法

方法一:

QStringList header;

header<<"ECJTU"<<"CERT";

treewidget->setHeaderLabels(header); //设置表头

方法二:

QStringList header;

header<<"ECJTU"<<"CERT";

QTreeWidgetItem *head=new QTreeWidgetItem((QTreeWidget*)0,header);

treewidget->setHeaderItem(head);

 

初始化用基本用到的:

//this->setMouseTracking(true);

this->setRootIsDecorated(false);

this->setSelectionMode(QAbstractItemView::ExtendedSelection);

this->setStyleSheet("QTreeWidget::item{height:25px}");  //设置行宽,     我也找不到提供的接口  setStyleSheet很强大建议看

this->setColumnWidth(0, 100);  //设置列宽

this->setColumnCount(2);

QStringList lists;

lists << "NO" << "name";

this->setHeaderLabels(lists);

 

for (int i=0;i<10;i++)

{

QStringList contentList;

contentList << QString("00%1").arg(i) << "boy";

QTreeWidgetItem *pNewItem = new QTreeWidgetItem(this, contentList);  //增加

pNewItem->setBackgroundColor(0, QColor(150,0,0));

pNewItem->setFont(0, QFont());

}

//pNewItemRoot->setExpanded(true);  //展开子项

 

进阶:

          //加checkbox, icon

          pNewItem->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |Qt::ItemIsEnabled);

          pNewItem->setCheckState(0, Qt::Unchecked);

         //pNewItem->setIcon(int , QIcon);

        //向ITEM里放一些QT的控件,如QSpinBox,QComboBox,QPushButton等

         this->setItemWidget(pNewItem, 0, new QSpinBox());

 

         //获取某项的QRect, 有时候会很有用,如显缩略图:

         QRect rect = this->visualItemRect(QTreeWidgetItem*);

          //右键菜单重写

         contextMenuEvent( QContextMenuEvent * event );或者使用信号itemPressed(QTreeWidgetItem*, int);

        //对头的操作,可以用QLabel或者QTreeWidgetItem对头进行初始化,

         //隐藏头

         header()->hide();

         m_pHeaderView->setClickable(true);  //能够发射sectionClicked(int)信号,可做菜单,

          m_pHeaderView->setMovable(true);

          m_pHeaderView->setResizeMode(0, QHeaderView::Fixed);  //固定列宽

          m_pHeaderView->setDefaultSectionSize(100);  //默认

         //排序

         treeWidget->setSortingEnabled(true);

        treeWidget->header()->setSortIndicatorShown(true);

        treeWidget->header()->setSortIndicator(0, Qt::AscendingOrder);

         treeWidget->header()->setSectionHidden(1, true);//隐藏列

       //要自定义就用信号

        connect( treeWidget->header(), SIGNAL( sectionClicked(int) ), this, SLOT( sectionClickedSlot(int) ) );

你可能感兴趣的:(QT)