[PyQt] Widget border and focus
Russell Valentine
russ at coldstonelabs.org
Wed Feb 24 03:47:48 GMT 2010
I'm wondering if anyone has tried to make a custom widget and wanted a
border around your widget (sunken or otherwise) like all the other
widgets have. It then also gets highlighted when it has focus. I thought
that it was QFrame that is doing that, but by putting my custom widget
in a QFrame, nothing happens, even if I manually set the frame style.
Below is some simple code. I have a splitter, on one side is my custom
widget and on the other side is a QListView. You can see the border
around the QListView and when it has focus the border changes to
indicate it. You can not tell the customwidget has focus however.
I've been trying to figure this out for a couple of days now, and
haven't found a solution yet. I think I'm missing some simple concept.
If anyone can point me in the right direction I would appreciate it.
Thank you.
Russell Valentine
import sys
from PyQt4 import QtGui, QtCore
class CustomWidget(QtGui.QWidget):
def __init__(self, parent):
QtGui.QWidget.__init__(self, parent)
self.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
self.setMinimumHeight(200)
self.setMinimumWidth(200)
self.setFocusPolicy(QtCore.Qt.StrongFocus)
def paintEvent(self, event):
painter=QtGui.QPainter(self)
b =
QtGui.QBrush(QtGui.QApplication.palette().color(QtGui.QPalette.Base))
painter.fillRect(0, 0, self.width(), self.height(), b)
class MainWindow(QtGui.QMainWindow):
def __init__(self, app):
QtGui.QMainWindow.__init__(self)
self.app=app
self.setWindowTitle("Frame test")
self.splitter=QtGui.QSplitter()
self.frame=QtGui.QFrame()
vbox=QtGui.QVBoxLayout(self.frame)
self.customWidget = CustomWidget(self)
vbox.addWidget(self.customWidget)
self.frame.setFocusProxy(self.customWidget)
self.list = QtGui.QListView()
self.splitter.addWidget(self.frame)
self.splitter.addWidget(self.list)
self.setCentralWidget(self.splitter)
if __name__ == "__main__":
app=QtGui.QApplication(sys.argv)
window = MainWindow(app)
window.show()
sys.exit(app.exec_())
More information about the PyQt
mailing list