Qt Custom Search box
Brief Introduction
The search box is frequently used. Such as browser search and Windows Resource Manager search.
Of course, there is no pressure on Qt implementation, as long as the idea is clear, it can be done in minutes.
Brief Analysis of results details download Coding source code
Effect
Detail Analysis
The following steps are required:
Combined implementation, input box + button event Association to get input text for text search
To be more humane and easy to use, we need to pay attention to the following details:
The text in the input box cannot be under the button. If there is no text in the input box, a friendly prompt button must be provided without text description. Generally, A ToolTip prompt button style must be provided-normal, slide, and pressed, and move the mouse over the mouse style hand,
All of these are clear, so we can quickly implement a search box.
Coding
Search box implementation
M_shortarchlineedit = new QLineEdit (); QPushButton * extends archbutton = new QPushButton (this); your archbutton-> setCursor (Qt: PointingHandCursor); your archbutton-> setFixedSize (22, 22 ); export archbutton-> setToolTip (QStringLiteral ("Search"); export archbutton-> 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 content from being placed under the button QMargins margins = m_shortarchlineedit-> textMargins (); m_shortarchlineedit-> setTextMargins (margins. left (), margins. top (), repeated archbutton-> width (), margins. bottom (); m_shortarchlineedit-> setPlaceholderText (QStringLiteral ("Enter search content"); QHBoxLayout * specify archlayout = new QHBoxLayout (); then archlayout-> addStretch (); revoke archlayout-> addWidget (shortarchbutton); Revoke archlayout-> setSpacing (0); Revoke archlayout-> setContentsMargins (0, 0, 0, 0); m_shortarchlineedit-> setLayout (shortarchlayout ); connect (shortarchbutton, SIGNAL (clicked (bool), this, SLOT (search ()));
Slot function implementation
Void Widget: search () {QString strText = m_shortarchlineedit-> text (); if (! StrText. isEmpty () {QMessageBox: information (this, QStringLiteral ("Search"), QStringLiteral ("search content: % 1"). arg (strText ));}}