无锡网站建,中国纪检监察报电子版,制作网站学什么,最常用的网站开发工具简述 关于搜索框#xff0c;大家都经常接触。例如#xff1a;浏览器搜索、Windows资源管理器搜索等。 当然#xff0c;这些对于Qt实现来说毫无压力#xff0c;只要思路清晰#xff0c;分分钟搞定。 方案一#xff1a;调用QLineEdit现有接口 void addAction(QAction * act… 简述 关于搜索框大家都经常接触。例如浏览器搜索、Windows资源管理器搜索等。 当然这些对于Qt实现来说毫无压力只要思路清晰分分钟搞定。 方案一调用QLineEdit现有接口 void addAction(QAction * action, ActionPosition position) 在QLineEdit的前/后添加部件ActionPosition表示部件所在方位。QAction * addAction(const QIcon icon, ActionPosition position) 重载函数。枚举QLineEdit::ActionPosition 常量值描述QLineEdit::LeadingPosition0当使用布局方向Qt::LeftToRight时部件显示在文本左侧使用Qt::RightToLeft则显示在右侧。QLineEdit::TrailingPosition1当使用布局方向Qt::LeftToRight时部件显示在文本右侧使用Qt::RightToLeft则显示在左侧。方案二自定义可以实现任何组合下面我们来针对自定义进行讲解。 简述效果细节分析Coding源码下载 效果 细节分析 实现细节需要如下步骤 组合实现输入框按钮事件关联获取输入文本进行文本搜索为了更人性、易用这里有一些细节需要注意 输入框的文本不能处于按钮之下输入框无文本时必须给与友好性提示按钮无文本描述一般需要给予ToolTip提示按钮样式-正常、滑过、按下以及鼠标滑过鼠标样式手型这些都想清楚了我们就能快速实现一个搜索框了。 Coding 搜索框实现 m_pSearchLineEdit new QLineEdit();
QPushButton *pSearchButton new QPushButton(this);pSearchButton-setCursor(Qt::PointingHandCursor);
pSearchButton-setFixedSize(22, 22);
pSearchButton-setToolTip(QStringLiteral(搜索));
pSearchButton-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)});//防止文本框输入内容位于按钮之下
QMargins margins m_pSearchLineEdit-textMargins();
m_pSearchLineEdit-setTextMargins(margins.left(), margins.top(), pSearchButton-width(), margins.bottom());
m_pSearchLineEdit-setPlaceholderText(QStringLiteral(请输入搜索内容));QHBoxLayout *pSearchLayout new QHBoxLayout();
pSearchLayout-addStretch();
pSearchLayout-addWidget(pSearchButton);
pSearchLayout-setSpacing(0);
pSearchLayout-setContentsMargins(0, 0, 0, 0);
m_pSearchLineEdit-setLayout(pSearchLayout);connect(pSearchButton, SIGNAL(clicked(bool)), this, SLOT(search())); 槽函数实现 void Widget::search()
{QString strText m_pSearchLineEdit-text();if (!strText.isEmpty()){QMessageBox::information(this, QStringLiteral(搜索), QStringLiteral(搜索内容为%1).arg(strText));}
} 源码下载 Qt之自定义搜索框Qt之QLineEdit