QTreeView

引用

#include"QDebug"

#include"QTreeView"

#include"QStandardItemModel"

#include"QPushButton"

#include"QItemSelectionModel"

#include"QModelIndex"

#include"QStandardItem"

代碼

????this->resize(1000,1000);

? ? //創(chuàng)建樹

? ? QTreeView *treeView=new QTreeView(this);

? ? treeView->resize(800,800);

? ? treeView->move(100,100);

? ? QStandardItemModel *model=new QStandardItemModel(treeView);

? ? treeView->setModel(model);

? ? //model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("項目名")<<QStringLiteral("相關(guān)信息1")<<QStringLiteral("相關(guān)信息2"));

? ? //model->setVerticalHeaderLabels(QStringList()<<QStringLiteral("項目名")<<QStringLiteral("相關(guān)信息1")<<QStringLiteral("相關(guān)信息2"));

? ? QStandardItem * item = new QStandardItem(tr("item one"));//創(chuàng)建一個條目對象

? ? model->appendRow(item);

? ? //去掉表頭

? ? treeView->setHeaderHidden(true);

//利用QList<QStandardItem*>添加節(jié)點

? ? QList itemList1;

? ? QList itemList2;

? ? QStandardItem * item1 = new QStandardItem(tr("item one"));//創(chuàng)建一個條目對象

? ? itemList1.append(item1);

? ? QStandardItem * item2 = new QStandardItem(tr("item two"));//創(chuàng)建一個條目對象

? ? itemList1.append(item2);

? ? QStandardItem * item3 = new QStandardItem(tr("item three"));//創(chuàng)建一個條目對象

? //? item.push_back(item3);

? ? QStandardItem * item4 = new QStandardItem(tr("item four"));//創(chuàng)建一個條目對象

? // item.push_back(item4);

? ? QStandardItem * item5 = new QStandardItem(tr("item five"));//創(chuàng)建一個條目對象

? ? QStandardItem * item6 = new QStandardItem(tr("item five"));//創(chuàng)建一個條目對象

? ? itemList2<

? ? model->appendRow(item3);

? ? model->appendRow(item4);

? ? //model->setItem(1,new QStandardItem("123"));

? ? item3->appendRows(itemList1);

? ? item2->appendRows(itemList2);

? ? //item->setChild(0,new QStandardItem("456"));

? ? //添加子節(jié)點

? ? QPushButton *buttonAdd=new QPushButton(treeView);

? ? buttonAdd->move(300,700);

? ? buttonAdd->setText("添加");

? ? QItemSelectionModel *currentModel=new QItemSelectionModel();

? ? connect(buttonAdd,&QPushButton::clicked,this,[=](){?

? ? ? ? QStandardItem *currentModel=model->itemFromIndex(treeView->currentIndex());

? ? ? ? if(model->rowCount()>0)

? ? ? {

? ? ? ? ? ? currentModel->appendRow(new QStandardItem("333"));

? ? ? }

? ? });

? ? //刪除子節(jié)點

? ? QPushButton *buttonDel=new QPushButton(treeView);

? ? buttonDel->move(500,700);

? ? buttonDel->setText("刪除");

? ? connect(buttonDel,&QPushButton::clicked,this,[=](){

? ? ? ? QStandardItem *currentModel=model->itemFromIndex(treeView->currentIndex());

? ? ? ? qDebug()<<"treeView->currentIndex().row()"<currentIndex().row();

? ? ? ? if(model->rowCount()>0)

? ? ? {

? ? ? ? ? ? if(currentModel->parent())

? ? ? ? ? ? {

? ? ? ? ? ? ? ? currentModel->parent()->removeRow(treeView->currentIndex().row());

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? model->removeRow(treeView->currentIndex().row());

? ? ? ? ? ? ? ? qDebug()<rowCount();

? ? ? ? ? ? }

? ? ? }

? ? });

//打開根節(jié)點

? ? treeView->expand(model->indexFromItem(model->item(0)));

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末惕医,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子扰付,更是在濱河造成了極大的恐慌,老刑警劉巖商膊,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胀溺,死亡現(xiàn)場離奇詭異,居然都是意外死亡擂啥,警方通過查閱死者的電腦和手機扳还,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門才避,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人氨距,你說我怎么就攤上這事桑逝。” “怎么了俏让?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵楞遏,是天一觀的道長茬暇。 經(jīng)常有香客問我,道長寡喝,這世上最難降的妖魔是什么糙俗? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮预鬓,結(jié)果婚禮上巧骚,老公的妹妹穿的比我還像新娘。我一直安慰自己格二,他們只是感情好劈彪,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著顶猜,像睡著了一般沧奴。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上长窄,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天滔吠,我揣著相機與錄音,去河邊找鬼抄淑。 笑死屠凶,一個胖子當(dāng)著我的面吹牛驰后,可吹牛的內(nèi)容都是我干的肆资。 我是一名探鬼主播,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼灶芝,長吁一口氣:“原來是場噩夢啊……” “哼郑原!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起夜涕,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤犯犁,失蹤者是張志新(化名)和其女友劉穎须尚,沒想到半個月后遣鼓,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡猫胁,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年驾胆,在試婚紗的時候發(fā)現(xiàn)自己被綠了涣澡。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡丧诺,死狀恐怖入桂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情驳阎,我是刑警寧澤抗愁,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布馁蒂,位于F島的核電站,受9級特大地震影響蜘腌,放射性物質(zhì)發(fā)生泄漏沫屡。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一撮珠、第九天 我趴在偏房一處隱蔽的房頂上張望谁鳍。 院中可真熱鬧,春花似錦劫瞳、人聲如沸倘潜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽涮因。三九已至,卻和暖如春伺绽,著一層夾襖步出監(jiān)牢的瞬間养泡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工奈应, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留澜掩,地道東北人。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓杖挣,卻偏偏與公主長得像肩榕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子惩妇,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355