【问题标题】:Qt/PyQt: How do I create a drop down widget, such as a QLabel, QTextBrowser, etc.?Qt/PyQt:如何创建下拉小部件,例如 QLabel、QTextBrowser 等?
【发布时间】:2012-02-22 23:35:01
【问题描述】:
如何创建下拉小部件,例如下拉QLabel、下拉QTextBrowser等?
例如,我在 QTextBrowser 中记录信息,但我不希望它占用屏幕空间。所以我希望能够单击 QToolbutton 并有一个可滚动的 QTextBrowser 下拉菜单。 (QComboBox 也可以,但我不能只将每个事件添加为单独的项目 - 我需要文本来换行,而不是被省略。因此是下拉 QTextBrowser。)
或者,例如,我想要一个包含图片等的下拉 QLabel...
【问题讨论】:
标签:
python
qt
drop-down-menu
widget
pyqt
【解决方案1】:
为下拉小部件创建一个QWidgetAction,并将其添加到工具按钮的menu:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
layout = QtGui.QHBoxLayout(self)
self.button = QtGui.QToolButton(self)
self.button.setPopupMode(QtGui.QToolButton.MenuButtonPopup)
self.button.setMenu(QtGui.QMenu(self.button))
self.textBox = QtGui.QTextBrowser(self)
action = QtGui.QWidgetAction(self.button)
action.setDefaultWidget(self.textBox)
self.button.menu().addAction(action)
layout.addWidget(self.button)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.resize(100, 60)
window.show()
sys.exit(app.exec_())