Qt之自定义搜索框

    关于搜索框,大家都经常接触!Qt如何实现一个搜索框?It's so easy!

 

    Google浏览器

    Windows计算机

 

直接上代码:

 

QLabel *forward_path = new QLabel();
QLineEdit *forward_line_edit = new QLineEdit();
QLineEdit *search_line_edit = new QLineEdit();
QToolButton *forward_button = new QToolButton(this);
QToolButton *search_button = new QToolButton(this);

 

forward_button->setToolTip(tr("forward"));
search_button->setToolTip(tr("search"));
forward_line_edit->setPlaceholderText(tr("computer"));
search_line_edit->setPlaceholderText(tr("search"));

forward_path->setPixmap(QPixmap(":/icon/path"));
forward_path->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

 

QIcon forward_icon = QIcon(":/icon/gobtn");
forward_button->setFixedSize(25, 24);
forward_button->setIcon(forward_icon );
forward_button->setIconSize(QSize(16, 16));
forward_button->setAutoRaise(true);
forward_button->setCursor(Qt::ArrowCursor);
forward_button->setStyleSheet("background:transparent;");

 

QIcon search_icon = QIcon(":/icon/search");
search_button ->setFixedSize(25, 24);
search_button ->setIcon(search_icon );
search_button ->setIconSize(QSize(16, 16));
search_button ->setAutoRaise(true);
search_button ->setCursor(Qt::ArrowCursor);
search_button ->setStyleSheet("background:transparent;");

 

forward_path->setFixedSize(20, 24);
forward_line_edit->setMinimumSize(100, 25);
search_line_edit->setMinimumSize(50, 25);

 

QSplitter *search_splitter = new QSplitter(this);
search_splitter->setFrameShape(QFrame::NoFrame);
search_splitter->setHandleWidth(1);

 

QHBoxLayout *forward_layout = new QHBoxLayout();
forward_layout->addWidget(forward_path);
forward_layout->addStretch();
forward_layout->addWidget(forward_button);
forward_layout->setSpacing(0);
forward_layout->setContentsMargins(0, 0, 0, 0);
forward_line_edit->setLayout(forward_layout);

//防止文本框输入内容位于按钮之下
forward_line_edit->setTextMargins(forward_path->width(), 0, forward_button->width(), 0);

 

QHBoxLayout *search_layout = new QHBoxLayout();
search_layout->addStretch();
search_layout->addWidget(search_button);
search_layout->setSpacing(0);
search_layout->setContentsMargins(0, 0, 0, 0);
search_line_edit->setLayout(search_layout);
search_line_edit->setTextMargins(0, 0, search_button->width(), 0);

 

search_splitter->addWidget(forward_line_edit);
search_splitter->addWidget(search_line_edit);
search_splitter->setStretchFactor(0, 8);
search_splitter->setStretchFactor(1, 2);
search_splitter->setChildrenCollapsible(false);


forward_line_edit->setStyleSheet("QLineEdit{border-style:outset; border-top-width:1px; border-left-width:1px; border-bottom-width:1px;border-top-color:gray; border-left-color:gray; border-bottom-color:gray; background:white;}");
search_line_edit->setStyleSheet("QLineEdit{border-style:outset; border-top-width:1px; border-right-width:1px; border-bottom-width:1px;border-top-color:gray; border-right-color:gray; border-bottom-color:gray; background:white;}");
search_splitter->setStyleSheet("QSplitter::handle{background:lightgray;}");

 

效果如下:


OK,关于输入框与按钮的事件就不再多说,实现槽函数,连接对应的信号和槽就行!

注:
    技术在于交流、沟通,转载请注明出处并保持作品的完整性。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值