QTreeWidget 和QTreeWidgetItem用法總結

pamxy發表於2013-07-23

轉自:http://blog.csdn.net/sunboyhch/article/details/8857305

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<QTreeWidgetItem *> 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<QTreeWidgetItem*>&);

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) ) );