经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Windows » 查看文章
Qt 搜索框
来源:cnblogs  作者:凉天满月  时间:2021/5/17 9:15:51  对本文有异议

一、前言

  用户需要输入文本时,可使用QLineEdit控件进行编辑输入,缺点是样式相对单一。

  在使用百度搜索输入框时,发觉比较人性化,故采用QLineEdt+QPushButton通过css样式实现自定义搜索框控件,包含如下功能:

  1、可设置占位符文本

  2、可设置搜索按钮显示字符内容、文本颜色

  3、可设置搜索按钮为图标形式或文本形式

  4、可设置背景色、边框颜色、边框圆角角度

  5、支持回车发送当前文本信号

  6、支持获取当前编辑文本内容

  7、支持设置当前文本内容

  8、鼠标移入/移出到搜索按钮上,切换鼠标状态

二、搜索框实现

  1、运行环境Qt5.5 VS2013

  2、自定义搜索框

  1)继承QWidget,定义搜索框类SearchLineEdit

  2)采用水平无间距布局

  1. 1 //用于输入搜索文本,左对齐
  2. 2 m_LineEdit = new QLineEdit;
  3. 3 m_LineEdit->setObjectName("SearchText");
  4. 4 m_LineEdit->setPlaceholderText(" 请输入搜索字符");
  5. 5 m_LineEdit->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  6. 6 m_LineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  7. 7 connect(m_LineEdit, SIGNAL(editingFinished()), this, SLOT(searchSlot()));
  8. 8
  9. 9 m_Button = new QPushButton;
  10. 10 m_Button->setObjectName("SearchButton");
  11. 11 m_Button->setText(buttonText);
  12. 12 m_Button->setIconSize(QSize(28, 28));
  13. 13 m_Button->setFixedWidth(55);
  14. 14 m_Button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  15. 15 connect(m_Button, SIGNAL(clicked(bool)), this, SLOT(searchSlot()));
  16. 16 m_Button->installEventFilter(this);
  17. 17
  18. 18 m_BgFrame = new QFrame;
  19. 19 m_BgFrame->setObjectName("frameSearch");
  20. 20
  21. 21 //设置样式
  22. 22 setStyle();
  23. 23
  24. 24 QVBoxLayout *verticalLayout = new QVBoxLayout(this);
  25. 25 verticalLayout->setMargin(0);
  26. 26 verticalLayout->setSpacing(0);
  27. 27 verticalLayout->addWidget(m_BgFrame);
  28. 28
  29. 29 //将控件按照横向布局排列
  30. 30 QHBoxLayout *layout = new QHBoxLayout(m_BgFrame);
  31. 31 layout->setMargin(0);
  32. 32 layout->setSpacing(0);
  33. 33 layout->addWidget(m_LineEdit);
  34. 34 layout->addWidget(m_Button);
控件布局

  3)设置QLineEdit与QPushButton的显示样式

  1. void SearchLineEdit::setStyle()
  2. {
  3. QStringList qss;
  4. qss.append(QString("QFrame#%1{border:none;border-radius:%2px;}")
  5. .arg(m_BgFrame->objectName()).arg(borderRadius));
  6. qss.append(QString("QLineEdit{background-color:%1;border:none;}").arg(bgColor));
  7. qss.append(QString("QLineEdit{border-top-left-radius:%1px;border-bottom-left-radius:%1px;border:2px solid %2;}")
  8. .arg(borderRadius).arg(borderColor));
  9. qss.append(QString("QPushButton{background-color:%1;border:none;color:%2;}")
  10. .arg(borderColor).arg(buttonTextColor));
  11. qss.append(QString("QPushButton{border-top-right-radius:%1px;border-bottom-right-radius:%1px;}")
  12. .arg(borderRadius));
  13. m_BgFrame->setStyleSheet(qss.join(""));
  14. }
样式设置

  3、控件效果如下

  

原文链接:http://www.cnblogs.com/liangtianmanyue/p/14775002.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号