zoukankan      html  css  js  c++  java
  • 【Qt】Qt之自定义搜索框【转】

    简述

    关于搜索框,大家都经常接触。例如:浏览器搜索、Windows资源管理器搜索等。

    这里写图片描述

    这里写图片描述

    当然,这些对于Qt实现来说毫无压力,只要思路清晰,分分钟搞定。

    效果

    这里写图片描述

    细节分析

    实现细节需要如下步骤:

    1. 组合实现,输入框+按钮
    2. 事件关联
    3. 获取输入文本,进行文本搜索

    为了更人性、易用,这里有一些细节需要注意:

    1. 输入框的文本不能处于按钮之下
    2. 输入框无文本时必须给与友好性提示
    3. 按钮无文本描述,一般需要给予ToolTip提示
    4. 按钮样式-正常、滑过、按下,以及鼠标滑过鼠标样式手型,

    这些都想清楚了,我们就能快速实现一个搜索框了。

    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));
        }
    }

    源码下载


    原文作者:一去丶二三里
    作者博客:去作者博客空间
    作者:芝麻科技
    出处:芝麻麻雀-Asp.Net学习之路
    技术:C++,C#
    向我打赏
    加我微信,聊一聊技术
  • 相关阅读:
    定理,定律,公理
    逻辑的体系:论据-》论证-〉论点
    深度解读:数学的本质与宇宙万物的关联--数学的本质是一门语言
    第一性原理的钥匙—逻辑奇点
    系统论的两个方向:系统分析与系统构建
    系统
    科学思考
    系统思考-使用系统论构建系统
    系统论是大尺度的还原论的时空思考-系统论是宏观上的还原论
    思考的几种形式
  • 原文地址:https://www.cnblogs.com/mzy-google/p/5162110.html
Copyright © 2011-2022 走看看