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