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 customize the search edit box with C++ qt

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you how to use C++ qt custom search edit box related knowledge points, the content is detailed, the logic is clear, I believe most people are too aware of this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

The results are as follows:

Description of implementation method:

(1) Custom QLineEdit, add the layout in the edit box, and set the button on the right

(2) add a signal to the custom QLineEdit, and send a signal to the main interface to do the corresponding operation when the button search button is clicked.

Custom LineEdit

Csearchlineedit.h

# ifndef CSEARCHLINEEDIT_H#define CSEARCHLINEEDIT_H#include # include # include class CSearchLineEdit: public QLineEdit {Q_OBJECTpublic: CSearchLineEdit (QWidget* parent = nullptr); signals: void sig_search (const QString& context); private slots: void on_ClickedSearch (); private: QPushButton * m_pBtn = nullptr;}; # endif / / CSEARCHLINEEDIT_H

Csearchlineedit.cpp

# include "csearchlineedit.h" # include CSearchLineEdit::CSearchLineEdit (QWidget* parent): QLineEdit (parent) {m_pBtn = new QPushButton (this); mroompBtn-> setCursor (Qt::PointingHandCursor); massipBtn-> setFixedSize (22,22); mroompBtn-> setToolTip (QStringLiteral ("search")); mroompBtn-> setStyleSheet ("QPushButton {border-image:url (: / images/icon_search_normal); background:transparent" }\ QPushButton:hover {border-image:url (: / images/icon_search_hover)}\ QPushButton:pressed {border-image:url (: / images/icon_search_press)} "); / / prevent text box input from being placed under the button QMargins margins = this- > textMargins () / / set the gap between the upper and lower right of the text content this- > setTextMargins (0,0, mroompBtn-> width () + 1,0); this- > setPlaceholderText ("enter search content"); / / set the layout of the edit box QHBoxLayout * pSearchLayout = new QHBoxLayout (this); pSearchLayout- > addStretch (); pSearchLayout- > addWidget (m_pBtn); pSearchLayout- > setSpacing (0); pSearchLayout- > setContentsMargins (0,0,0) This- > setLayout (pSearchLayout); connect (m_pBtn, & QPushButton::clicked, this, & CSearchLineEdit::on_ClickedSearch);} void CSearchLineEdit::on_ClickedSearch () / / other logical emit sig_search (this- > text ())

Note that the click event of the search button is received in the main interface, and the parameter of the slot function is the search content. Of course, const QString&, can also deal with it in CSearchLineEdit, but it is generally customary to separate the control code from the logic code and try not to deal with business logic in the control class.

The main interface code is # include "widget.h" # include # include "csearchlineedit.h" Widget::Widget (QWidget * parent): QWidget (parent) {resize (400,300); setWindowTitle ("custom search box"); CSearchLineEdit * pLineEdit = new CSearchLineEdit (this); QVBoxLayout * pLayout = new QVBoxLayout (); pLayout- > addWidget (pLineEdit); pLayout- > setContentsMargins (10,10,10,10); setLayout (pLayout) Connect (pLineEdit, & CSearchLineEdit::sig_search, this, & Widget::onSearch);} Widget::~Widget () void Widget::onSearch (const QString& context) if (context.isEmpty ()) {QMessageBox::information (this, QStringLiteral ("prompt"), QStringLiteral ("search content is empty"); return } QMessageBox::information (this, QStringLiteral ("prompt"), QStringLiteral ("search content is 1") .arg (context); other instructions

In addition to adding buttons on the right, you can also add buttons on the left, as long as you reasonably set the gap between the text of the LineEdit and the controls on the left and right. For example, the following custom edit box:

Youku search box

Nail search box

All these can be realized. Youku needs to use qss to set the fillet around the LineEdit to half the height, and the fillet on the right button to half the height, so it can be realized; the search box for nails is the search button on the left; both search functions will pop up dialog boxes, such as nails.

This can also be done by popping up the dialog box when LineEdit gets focus.

The above is all the content of this article "how to use C++ qt to customize the search edit box". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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