In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain how to apply the single-layer tree component of CAccord + Qt TreeWidget. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to apply the single-layer tree components of CAccord Craft + Qt TreeWidget.
TreeWidget directory tree component, which is suitable for creating and managing directory tree structure. In development, we often use it as an upgraded version of ListView component, because ListView can only display one column of data set at a time, but using TableWidget component to display multiple columns is not beautiful enough. At this time, using Tree component to display single-layer structure is the best way. This chapter will achieve multi-field display through TreeWidget and add a custom menu. You can pop up the menu and operate on the specified record by right-clicking the specified record.
1. Through the TreeView component to achieve a read-only attribute tree directory, the directory specifies three fields, which are used to represent the ID,IP address, user name field.
Initialize the Tree component
1. Initialize and set the treeView property
two。 Set column header length
3. Set column header data
4. Set the elements in the table
/ / set the width of the last column adaptively ui- > treeView- > setIndentation (1); / / set the header indentation to 1 ui- > treeView- > setEditTriggers (QAbstractItemView::NoEditTriggers); / / Node is not editable / / set the column header length ui- > treeView- > setColumnWidth (0mem50) QStandardItem * item_addr = new QStandardItem ("192.168.1.1"); ptr.push_back (item_addr); QStandardItem * item_username = new QStandardItem ("lyshark"); ptr.push_back (item_username); tree- > appendRow (ptr);}
After the code runs, it looks like this:
two。 Using the TreeWidget component, define a menu and bind the menu to the Tree component. The specific implementation code is as follows.
# include "mainwindow.h" # include "ui_mainwindow.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); / / add this item ui- > treeWidget- > setContextMenuPolicy (Qt::CustomContextMenu) to use the right-click menu in MainWindow; / / create the base top menu QMenuBar * bar = menuBar (); this- > setMenuBar (bar); QMenu * fileMenu = bar- > addMenu ("menu 1") / / implement only hides menu 1 other unaffected fileMenu- > menuAction ()-> setVisible (false); / / add submenu GetColumnAction = fileMenu- > addAction ("get column number"); GetRowDataAction = fileMenu- > addAction ("get row data"); GetLineAction = fileMenu- > addAction ("get line number"); / / set icons GetColumnAction- > setIcon ("QIcon (": / image/1.ico ") respectively) GetRowDataAction- > setIcon (QIcon (": / image/2.ico"); GetLineAction- > setIcon (QIcon (": / image/3.ico")); / / bind hotkeys GetColumnAction- > setShortcut (Qt::CTRL | Qt::Key_A) to submenus; GetRowDataAction- > setShortcut (Qt::SHIFT | Qt::Key_S); GetLineAction- > setShortcut (Qt::CTRL | Qt::SHIFT | Qt::Key_B) / / set treeWidget properties ui- > treeWidget- > setColumnCount (4); / / set total number of columns ui- > treeWidget- > setColumnWidth (0300); / / set last column width adaptive ui- > treeWidget- > setIndentation (1); / / set header indentation to 1 / / set header data QStringList headers; headers.append ("file name"); headers.append ("update time") Headers.append ("Files of Type"); headers.append ("File size"); ui- > treeWidget- > setHeaderLabels (headers); / / simulate inserting data into the table for (int xint0 / setIcon (": / image/1.ico"); item- > setText (1, "2020-12-11"); item- > setText (2, "* .pdf") Item- > setText (3, "102MB"); item- > setIcon (3addTopLevelItem QIcon (": / image/2.ico")); ui- > treeWidget- > addTopLevelItem (item);}} MainWindow::~MainWindow () {delete ui;} / / trigger / / By: LyShark// https://www.cnblogs.com/lysharkvoid MainWindow::on_treeWidget_customContextMenuRequested (const QPoint & pos) {Q_UNUSED (pos) when the right button in treeWidget is clicked / / create a new Menu menu QMenu * ptr = new QMenu (this); / / add Actions to create menu items ptr- > addAction (GetColumnAction); ptr- > addAction (GetLineAction); / / add a split line ptr- > addSeparator (); ptr- > addAction (GetRowDataAction); / / display right-click shortcut menu ptr- > exec (QCursor::pos ()) at the mouse cursor position / / manually created pointers must manually delete delete ptr;}
In the end, the effect we achieved is as follows.
At this point, I believe you have a deeper understanding of "how to apply the single-layer tree components of CAccord + Qt TreeWidget". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.