2
A
回答
5
見提議的解決方案 http://blog.qt.digia.com/blog/2007/06/06/lineedit-with-a-clear-button/
由於原鏈接這篇博客已不存在,我提供了一個新的代碼片段。
主要想法是將QToolButton
添加到QLineEdit
並正確定位。
LineEdit::LineEdit(QWidget *parent)
: QLineEdit(parent)
{
int height = sizeHint().height();
int btnSize = sizeHint().height() - 5;
clearButton = new QToolButton(this);
QPixmap pixmap(":clear.png");
clearButton->setIcon(QIcon(pixmap));
clearButton->setCursor(Qt::ArrowCursor);
clearButton->setStyleSheet("QToolButton { border: none; padding: 2px}");
clearButton->setFixedSize(btnSize, btnSize);
clearButton->hide();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
setStyleSheet(QString("QLineEdit { padding-right: %1px }")
.arg(btnSize - frameWidth));
setMinimumHeight(height);
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
connect(this, SIGNAL(textChanged(const QString&)),
this, SLOT(updateCloseButton(const QString&)));
}
void LineEdit::resizeEvent(QResizeEvent *)
{
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
clearButton->move(width() - clearButton->width() - frameWidth, 0);
}
void LineEdit::updateCloseButton(const QString& text)
{
clearButton->setVisible(!text.isEmpty());
}
另外,由於Qt的5.2,可以使用QLineEdit
內置方法setClearButtonEnabled
。 http://doc.qt.io/qt-5/qlineedit.html#clearButtonEnabled-prop
相關問題
- 1. 如何在Stata的條形圖中製作額外的標籤
- 2. 如何在css和html中製作這樣的標題
- 3. 如何在可點擊的樣式中製作圖標?
- 4. 如何在swift中製作像下圖一樣的標籤
- 5. NavigationToolbar2QTAgg上的額外圖標
- 6. 沒有這樣的插槽QLineEdit :: setText
- 7. 如何在標題中顯示額外的圖像 - Magento
- 8. 如何在Android中的操作欄圖標之間刪除此額外空間?
- 9. 如何製作這樣的東西?
- 10. 如何從Tweetbot製作這樣的NSWindow?
- 11. 如何製作這樣的桌子?
- 12. 如何製作這樣的分隔線?
- 13. 如何製作這樣的迴歸樹?
- 14. 如何製作這樣的CSS邊框?
- 15. 如何製作這樣的SQL查詢?
- 16. 如何製作這樣的JSON輸出?
- 17. 如何製作這樣的PHP代碼?
- 18. 如何製作這樣的菜單?
- 19. 在Matlab圖上繪製額外的圓
- 20. 如何限制QLineEdit中的表達式
- 21. 像mathematica這樣的程序如何繪製圖形,以及如何製作這樣的程序?
- 22. Symfony額外的控制器的標題
- 23. 如何製作像徽標測驗這樣的應用程序
- 24. 如何製作這樣的自定義標記? osmdroid
- 25. 如何將圖標放到QLineEdit上?
- 26. 如何繪製這樣的「帶標記線」圖?
- 27. 如何在PCL視覺工作室添加額外的目標
- 28. Jade - 如何在href標記中添加額外的href標記
- 29. 如何在Android中製作這樣的按鈕?
- 30. 如何在WP中製作這樣的水平下拉菜單
請不要發表僅由鏈接組成的答案,而應在答案中包含解決方案。這項政策是針對這種情況進行辯護的,其中鏈接到的網頁已更改,並且不再有解決此問題的方法。 – nandhp 2015-05-30 00:07:58
@nandhp我已經更新了答案。 – hank 2015-06-01 07:22:50