class DictationOverlay(QWidget): """Frameless window showing the partial dictation results """ app = property(appref.app) DEFAULT_FLAGS = ( Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.WindowTransparentForInput | Qt.Tool | Qt.WindowDoesNotAcceptFocus ) REPOSITION_FLAGS = Qt.WindowStaysOnTopHint | Qt.Tool def __init__(self, *args, **named): super(DictationOverlay, self).__init__(*args, **named) self.setWindowTitle('%s Text Preview' % (defaults.APP_NAME_SHORT)) self.setWindowFlags(self.DEFAULT_FLAGS) # self.setAttribute(Qt.WA_TranslucentBackground, True) # self.setAttribute(Qt.WA_NoSystemBackground, True) self.setAttribute(Qt.WA_ShowWithoutActivating, True) # self.setMaximumHeight(40) # self.setMinimumWidth(400) self.setWindowOpacity(0.8) self.label = QPushButton(defaults.APP_NAME_HUMAN, self) self.label.setFlat(True) self.label.connect(self.label, SIGNAL('clicked()'), self.save_new_position) # self.setCentralWidget(self.label) self.timer = QTimer(self) self.timer.connect(self.timer, SIGNAL('timeout()'), self.on_timer_finished) def show_for_reposition(self): """Allow the user to reposition""" self.setWindowFlags(Qt.WindowStaysOnTopHint) self.set_text('Drag, then click here', 0) GEOMETRY_SAVE_KEY = 'overlay.geometry' def save_new_position(self, *args): """On close during reposition, save geometry and hide""" log.info("Saving new position: %s", self.geometry()) self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint) self.app.settings.setValue(self.GEOMETRY_SAVE_KEY, self.saveGeometry()) self.hide() self.disconnect(SIGNAL('click()'), self.save_new_position) def set_text(self, text, timeout=500): log.info("Setting text: %s", text) self.label.setText(text) self.label.adjustSize() self.adjustSize() self.show() if timeout: self.timer.start(500) def on_timer_finished(self, evt=None): """When the timer finishes without any interruption/reset, hide the window""" self.hide() self.timer.stop()
def testButtonClickClose(self): button = QPushButton() button.connect(button, SIGNAL('clicked()'), SLOT('close()')) button.show() self.assertTrue(button.isVisible()) button.click() self.assertTrue(not button.isVisible())
def testButtonClickClose(self): button = QPushButton() button.connect(button, SIGNAL('clicked()'), SLOT('close()')) button.show() self.assert_(button.isVisible()) button.click() self.assert_(not button.isVisible())
import sys from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel from PySide2 import QtCore app = QApplication(sys.argv) hello_button = QPushButton("Hello") world_label = QLabel("Sun") layout = QVBoxLayout() layout.addWidget(hello_button) layout.addWidget(world_label) def set_text_in_world_label(): world_label.setText("World") hello_button.connect(QtCore.SIGNAL('clicked()'), set_text_in_world_label) window = QWidget() window.setLayout(layout) window.resize(200, 200) window.show() sys.exit(app.exec_())