qt 之自定義 搜尋框

2021-07-31 13:49:06 字數 1755 閱讀 9539

當然,這些對於qt實現來說毫無壓力,只要思路清晰,分分鐘搞定。

列舉:qlineedit::actionposition常量值

描述qlineedit::leadingposition

0當使用布局方向qt::lefttoright時,部件顯示在文字左側,使用qt::righttoleft則顯示在右側。

qlineedit::trailingposition

1當使用布局方向qt::lefttoright時,部件顯示在文字右側,使用qt::righttoleft則顯示在左側。

下面,我們來針對自定義進行講解。

實現細節需要如下步驟:

組合實現,輸入框+按鈕

事件關聯

獲取輸入文字,進行文字搜尋

為了更人性、易用,這裡有一些細節需要注意:

輸入框的文字不能處於按鈕之下

輸入框無文字時必須給與友好性提示

按鈕無文字描述,一般需要給予tooltip提示

按鈕樣式-正常、滑過、按下,以及滑鼠滑過滑鼠樣式手型,

這些都想清楚了,我們就能快速實現乙個搜尋框了。

搜尋框實現

m_psearchlineedit =

new qlineedit();

qpushbutton *psearchbutton =

new qpushbutton(this);

psearchbutton->setcursor(qt::pointinghandcursor);

psearchbutton->setfixedsize(22, 22);

psearchbutton->settooltip(qstringliteral("搜尋"));

psearchbutton->setstylesheet("qpushbutton \

qpushbutton:hover \

qpushbutton:pressed");

//防止文字框輸入內容位於按鈕之下

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()

}

Qt之自定義搜尋框

當然,這些對於qt實現來說毫無壓力,只要思路清晰,分分鐘搞定。列舉 qlineedit actionposition常量值 描述qlineedit leadingposition 0當使用布局方向qt lefttoright時,部件顯示在文字左側,使用qt righttoleft則顯示在右側。qli...

Qt之自定義搜尋框

當然,這些對於qt實現來說毫無壓力,只要思路清晰,分分鐘搞定。實現細節需要如下步驟 組合實現,輸入框 按鈕 事件關聯 獲取輸入文字,進行文字搜尋 為了更人性 易用,這裡有一些細節需要注意 輸入框的文字不能處於按鈕之下 輸入框無文字時必須給與友好性提示 按鈕無文字描述,一般需要給予tooltip提示 ...

Qt 自定義搜尋框

1.建立輸入框 qlineedit text new qlineedit this text setgeometry 50,80,200,25 text setobjectname qstring text 2.建立按鈕 qsize size qsize 40,text height qpushbu...