QT5 Qtreewidget Implementation Click on the item event and the right-click menu to delete item and rename item

Source: Internet
Author: User

1. New a Qtreewidget object, and set header tag, and root node (required by personal program)

   qtreewidget* treewidget = Ui.treewidget; // I've dragged a qtreewidget in the UI designer.     QString headers;    Headers  "Name"  ;    Treewidget, Setheaderlabel (headers);    Qstringlist Roottext;    Roottext  "Wells";     New Qtreewidgetitem (Treewidget, Roottext);        

2, add Qtreewidgetitem (here is the name of the file in the folder to read the name of the item)

     for (int2; i < filelist.size (); i++)    {                new Qtreewidgetitem (Root, Qstringlist () <<filelist.at (i). FileName ());        Leaf->setflags (leaf->flags () |  qt::itemiseditable);          Root-addChild (leaf);    }

3. Click on the Item event

 

Connect (treewidget,signal (itemclicked (qtreewidgetitem*,intThis, SLOT (Checkself ( qtreewidgetitem*,int))); // detect click events, signal slot mechanism

4, Checkself (qtreewidgetitem*,int); The function is the response function after the click. In the private slot: under the Declaration (the specific implementation here is not posted)

5. Right-click popup menu

Connect (treewidget,signal (customcontextmenurequested (constThis, SLOT (Popmenu (const (qpoint&))); // Detect right mouse button

6, pop-up menu response function Popmenu (const qpoint&)

voidLWD::p Opmenu (Constqpoint&) {Qtreewidgetitem* Curitem=treewidget->currentitem ();//Gets the node that is currently clicked    if(Curitem==null)return;//In this case, the right-hand position is not in the range of TreeItem, that is, right-clicking in a blank positionQString wellname = Curitem->text (0); if(Wellname! ="Wells") {qaction Deletewell (qstring::fromlocal8bit ("& Delete the Well"), This);//Delete a wellQaction Renamewell (QString:: Fromlocal8bit ("& Renaming Wells"), This);//Rename well//Delete the item on the interfaceConnect (&deletewell, SIGNAL (triggered ()), This, SLOT (DeleteItem ())); Connect (&renamewell,signal (triggered ()), This, SLOT (Renamewell ()));        Qpoint POS;        Qmenu menu (Ui.treewidget); Menu.addaction (&Deletewell); Menu.addaction (&Renamewell);  Menu.exec (qcursor::p OS ()); //display at current mouse position            }}

7, DeleteItem ()

voidLWD::d Eleteitem () {root->removechild (treewidget->CurrentItem ()); if(MyW! =NULL) {MyW-setParent (NULL); Ui.verticallayout_4-Removewidget (MyW); }    //Delete well data filesQString Dirpath =".. /data1/"; Dirpath.append (Treewidget->currentitem ()->text (0)); Dirpath.append ("/"); DeleteDirectory (Dirpath);//implemented in the following}BOOLLWD::D eletedirectory (ConstQString &path) {    if(Path.isempty ())return false;    Qdir dir (path); if(!dir.exists ())return true; Dir.setfilter (Qdir::allentries|Qdir::nodotanddotdot); Qfileinfolist fileList=dir.entryinfolist (); foreach(Qfileinfo fi, fileList) {if(Fi.isfile ()) Fi.dir (). Remove (Fi.filename ()); Elsedeletedirectory (Fi.absolutefilepath ()); }        returnDir.rmpath (Dir.absolutepath ());}

8, Renamewell ()

voidLwd::renamewell () {prename= Treewidget->currentitem ()->text (0); Prepath=".. /data1/";    Prepath.append (Prename); Ui.treewidget->edititem (ui.treewidget->CurrentItem ()); //T to determine the modified name by monitoring the ItemChanged event!!!! Connect (treewidget,signal (itemchanged (Qtreewidgetitem *,int)), This, SLOT (namechanged (qtreewidgetitem*  ))); }voidLwd::namechanged (qtreewidgetitem*Item) {    //Rename a folder firstQString newName = Treewidget->currentitem ()->text (0); QString NewPath=".. /data1/";    Newpath.append (NewName);    Qfile::rename (Prepath,newpath); Prepath= Newpath.append ("/");    Prepath.append (Prename); Prepath.append (". txt"); //Renaming a file after the wellbore trajectory has been processedNewpath.append ("/");    Newpath.append (NewName); Newpath.append (". txt"); Qfile::rename (Prepath,newpath);}

QT5 Qtreewidget Implementation Click on the item event and the right-click menu to delete item and rename item

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.