我想通过按一个按钮来显示/隐藏QTextBrowser小部件。切换它有什么意思吗?现在我有两个按钮;一个用于显示文本浏览器,另一个用于隐藏它。按钮也会隐藏,具体取决于文本浏览器的可见性。这个实现按预期工作,但我认为应该/必须采用更复杂的方式来实现它。有什么建议?
sbt
[info] Set current project to QuickStartSpec (in build file:/tmp/stack/)
> test:compile
[info] Updating {file:/tmp/stack/}stack...
[info] Resolving jline#jline;2.12 ...
[info] Done updating.
[info] Compiling 1 Scala source to /tmp/stack/target/scala-2.11/test-classes...
[info] 'compiler-interface' not yet compiled for Scala 2.11.4. Compiling...
[info] Compilation completed in 6.372 s
[success] Total time: 9 s, completed 27 nov. 2015 06:38:26
> test
[info] QuickStartSpec
[info] + This is my first specification
[info] it is working
[info] + really working!
[info]
[info] Total for specification QuickStartSpec
[info] Finished in 17 ms
[info] 2 examples, 0 failure, 0 error
[info]
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 1 s, completed 27 nov. 2015 06:38:31
>
只要显示或隐藏QTextBrowser小部件,就会调整对话框的大小。
答案 0 :(得分:0)
只需保存当前状态并使用它来切换可见性:
def toggleVisibleTextBrowser(self):
isVisible= !isVisible # declared somewhere else
self.textBrowser.setVisible(isVisible)
if isVisible:
self.resize(461, 444)
else
self.resize(461, 200)
...
答案 1 :(得分:0)
您只需要一个按钮和一个处理程序。单击按钮时更改按钮的文本,并使用浏览器的当前可见性在两种状态之间切换。
这是一个有效的演示:
from PySide import QtCore, QtGui
class Program(QtGui.QWidget):
def __init__(self):
super(Program, self).__init__()
self.textBrowser = QtGui.QTextBrowser(self)
self.button = QtGui.QPushButton('Hide', self)
self.button.clicked.connect(self.toggleTextBrowser)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.textBrowser)
layout.addWidget(self.button)
def toggleTextBrowser(self):
if self.textBrowser.isHidden():
self.button.setText('Hide')
self.textBrowser.show()
self.resize(461, 444)
else:
self.button.setText('Show')
self.textBrowser.hide()
self.resize(461, 200)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Program()
window.setGeometry(500, 300, 461, 444)
window.show()
sys.exit(app.exec_())