Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the functions of dragging the list of self-selected stocks and right-clicking common menus in Qt

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how Qt to achieve self-selection list drag, right-click common menu functions, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

I. Preface

The stock selections in this article, like most stock trading software, support drag and drop, and the mouse will follow a drag image when you drag, and there will be a drag prompt when the mouse is moved. tell us where the drag item will be inserted when the mouse is released. In addition to dragging, the stock selection list also supports right-click menus, all of which are the same common operations.

Right-click menus include set top, set low, delete, move down one item, move up item, etc.

II. Function

It has the following functions

1. Search edit box, support stock symbol and stock name search 2. The search preview box supports mouse hover, and you can use the keyboard up and down keys to switch the current item, and you can switch optional stocks 3. 5% on a single machine. List of optional stocks, supporting drag and drop, there will be an image of drag items when dragging, and indicate where to drag to 4. Right-click menu is supported, and you can move or delete an item.

If you think demo is ugly, you can take a look at the effect picture in this article of the Financial Associated Press-Product Show.

III. List of self-selected stocks

Next is the highlight of our article, which is also a more complicated content.

I chose to use QListWidget to implement the stock selection list, and then put a Widget on each item. Widget is the content of our customized form. Here we mainly talk about several important core contents.

1. List initialization

Initialize StockList, in fact, the list of self-selected stocks should be pulled from the server, we here as a demo test, so we simulated 5 pieces of data to insert.

/ / A list of selected stocks droomptra-> m_pStockList = new StockList;connect (dumbptra-> m_pStockList, & StockList::RowClicked, this, [this] (const QString & symbol) {emit RowClicked (symbol);}); / / normally, the test data should pull OptionalMarketItem item;for (int I = 1; I mstocklist-> AddItem (item);}

2. Add Item

When adding item items to StockList, we first need to construct a standard QListWidgetItem structure, and then put our own custom ListItem on this standard item structure.

QListWidgetItem * StockList::AddItem (const OptionalMarketItem & data) {ListItem * itemWidget = new ListItem; itemWidget- > SetData (data); QListWidgetItem * item = new QListWidgetItem; addItem (item); item- > setSizeHint (QSize (0,50)); setItemWidget (item, itemWidget); return item;}

ListItem is a normal QWidget with some QLabel arranged on it to display our stock data.

The construction of the ListItem interface is not explained too much. The only thing that needs to be explained is that when our stock data sends changes, there will be an animation prompt of a red and green box on the interface. Here, we need to call two lines of code to retrieve the control qss code and scrub the interface.

This- > style ()-> unpolish (this); this- > style ()-> polish (this)

3. Right-click menu

This article and the last article right-click menu implementation is the same as I wrote a long time ago, Qt custom QLineEdit right-click menu this article, the implementation of the default contextMenuEvent function.

Right-click menu has been said a lot, here at a glance, the need for students can take a quick look, it should be easier to understand.

Void StockList::contextMenuEvent (QContextMenuEvent * event) {if (dumbptra-> m_AllowMenu = = false) {return;} if (dumbptra-> m_ContextMenu = = nullptr) {dumbptra-> m_ContextMenu = new QMenu (this); dumbptre-> masked ContextMenu-> setObjectName (QStringLiteral ("StockListMenu")); dumbptr- > masked ContextMenu-> setFixedWidth (100); QAction * delAct = new QAction (QStringLiteral ("Delete optional stocks"), dumbptra-> m_ContextMenu) QAction * topAct = new QAction (QStringLiteral ("top"), dumbptra-> m_ContextMenu); QAction * bottomAct = new QAction (QStringLiteral ("bottom"), dumbptra-> m_ContextMenu); QAction * upAct = new QAction (QStringLiteral ("move one bit up"), dumbptra-> m_ContextMenu); QAction * downAct = new QAction (QStringLiteral ("move one bit down"), dumbptra-> m_ContextMenu); connect (delAct, & QAction::triggered, this, & StockList::DeleteSotck) Connect (topAct, & QAction::triggered, this, & StockList::TopSotck); connect (bottomAct, & QAction::triggered, this, & StockList::BottomSotck); connect (upAct, & QAction::triggered, this, & StockList::UpSotck); connect (downAct, & QAction::triggered, this, & StockList::DownSotck) Droomptra-> masked ContextMenu-> addAction (upAct); dumbptre-> masked ContextMenu-> addAction (downAct);} dhumptre-> masked ContextMenu-> exec (mapToGlobal (event- > pos (); QListWidget::contextMenuEvent (event);}

Although the above five menus seem to have different functions, the processing logic is basically the same, first sorting the content structure, and then refreshing the data to the interface.

In order to save space, I will only introduce the operation of buying a stock here.

The logic at the top looks like this.

1. Remove the current item 2. And insert the current item item into the new location 3. Construct a new Widget and set it to item4. Set the item of the new location to the currently selected item 5. 0. Upload the latest list to the data center or server

Void StockList::TopSotck () {QListWidgetItem * item = currentItem (); if (item = = nullptr) {return;} if (row (item) = 0) {return;} ListItem * itemWidget = ItemWidget (item); QListWidgetItem * newItem = takeItem (row (item)); insertItem (0, newItem); ListItem * topWidget = new ListItem; topWidget- > SetData (itemWidget- > GetData ()); setItemWidget (newItem, topWidget); if (itemWidget) {itemWidget- > close (); close = itemWidget} itemWidget (itemWidget); itemWidget ();}

4. Drag and drop Item

Dragging Item should be a bit more difficult. Fortunately, Qt has implemented a set of callback methods for QDrag events for us, and it is also easier to use. As shown in the following figure, rewrite the following four methods, and the basic drag events can be completed.

But here we do not choose the default callback function to implement this function, the biggest reason is that their customization is too limited.

What I take here is to simulate the mouse drag function myself, and rewrite the following functions to achieve my goal.

Virtual void mousePressEvent (QMouseEvent * event) override;virtual void mouseMoveEvent (QMouseEvent * event) override;virtual void mouseReleaseEvent (QMouseEvent * event) override;virtual void enterEvent (QEvent * event) override;virtual void leaveEvent (QEvent * event) override

1. When the mouse is pressed, it mainly records some content status, which is convenient to judge when the mouse moves, and decides whether to enable the mouse drag function 2. Mouse movement is more complex, made a variety of comparisons, but also need to move the image position of the dragged item, move the position of the horizontal line 3. When the mouse is released, adjust the contents of the entire list 4. When the mouse enters the form, the horizontal identification line 5. Hide the horizontal identification line when the mouse leaves the form

The above is only a rough description of the functions of these functions, because the function implementation is relatively long, so here I also choose a few key points to explain.

A, move function

When dragging and moving the mouse, we need to deal with a lot of events, such as

1. Initialize horizontal representation lines and drag-and-drop item images

If (dumbptra-> m_ShotLine = = nullptr) {InitShotLine ();} if (dumbptra-> m_ShotPicture = = nullptr) {InitShotLabel ();}

2. Modify the mouse status while dragging

Set the status of the mouse according to whether the mouse is still over the current drag item after the drag is started.

If (ListItem * newWidget = ItemWidget (dumbptra-> dragItem)) {dumbptra-> masked ShotPicture-> move (QCursor::pos ()-dumbptre-> dragItemPos); dumbptre-> m_DragRect = visualItemRect (dumbptra-> dragItem); if (dumbptra-> m_DragRect.contains (event- > pos ()) | event- > pos (). IsNull () {if ((event- > pos ()-dumbptre-> startPos). ManhattanLength () > 5) {setCursor (Qt::ForbiddenCursor) }} else {setCursor (Qt::ArrowCursor);} if (dumbptra-> masked ShotPicture-> isHidden ()) {dumbptra-> masked ShotPicture-> show ();}}

B, release function

When the mouse is released, move the drag item to a new location

If (ListItem * oldWidget = ItemWidget (dumbptra-> dragItem)) {QListWidgetItem * newItem = new QListWidgetItem; ListItem * itemWidget = new ListItem; itemWidget- > SetData (oldWidget- > GetData ()); insertItem (insertPos, newItem); newItem- > setSizeHint (QSize (0,50)); setItemWidget (newItem, itemWidget); setCurrentItem (newItem); oldWidget- > deleteLater ();}

5. Refresh data

Full refresh of data. Refresh data on the original list

When the number of rows in the original list is insufficient, construct a new row

Remove rows with multiple ends when there are many original list functions

Void StockList::Update_p (OptionalMarketItemVector data) {dumbptra-> m_bOnceLoad = true; disconnect (this, & QListWidget::currentItemChanged, this, & StockList::CurrentItemChanged); int I = 0; for (auto iter = data.begin (); iter! = data.end (); + + iter, + + I) {bool success = false; if (QListWidgetItem * item = this- > item (I)) {if (ListItem * itemWidget = ItemWidget (item)) {itemWidget- > SetData (* iter); success = true }} if (! success) {AddItem (* iter);}} if (I

< this->

Count () {QListWidgetItem * item = nullptr; while (item = this- > item (I)) {if (ListItem * itemWidget = ItemWidget (item)) {itemWidget- > close (); itemWidget = nullptr;} item = takeItem (I); delete item;} if (dumbptra-> m_LeftPress = = false) {RecoveryCurrentItem ();} connect (this, & QListWidget::currentItemChanged, this, & StockList::CurrentItemChanged);}

The above is all the contents of the article "how to drag and drop the list of self-selected stocks in Qt, right-click common menu functions". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report