If I've created a QMainWindow instance, and assign the central widget and dock widgets for it:
class MyWindow(QtGui.QMainWindow):
def __init__(self, MyCentralWidget, MyDockWidget):
super(MyWindow,self).__init__()
self.setCentralWidget(MyCentralWidget)
self.addDockWidget(Qt.LeftDockWidgetArea,MyDockWidget)
self.show()
Instance=MyWindow(A,B)
Then I think I can get access to the center widget A by Instance.centralWidget(), but how can I get access to the dockwidget in the LeftDockWidgetArea similarly? (for example, I want to modify the properties of the Widget in that dockwidget after instance is created)
try:
self.dockWidgetContents.your_widget...
Related
I'm little confusing meaning of widgets instance creation in PyQt.
Seems QWidget(self) can create a widget.
But what's the meaning of QWidget(instance name)?
For exmaple,
grid_layout = QGridLayout(self)
test_label = QWidget(grid_layout)
I know test_label created inherited from gridlayout.
But what's the meaning during program meaning?
Is that meaning test_label widget is under grid_layout?
For many of the widgets when the signature is just a single parameter like in both of your examples, the argument given sets the created widget/layouts parent.
So in your first example, using self as the argument means that the parent of the QGridLayout and the widget that the layout will be applied to is whatever widget self is.
In the second example you are setting the parent of QWidget to be the widget represented by it's argument. A QWidget with no parent becomes a new window, when set with a parent then the widget becomes a child window within the parent widget.
Neither one are inheriting anything. Inheritance only occurs during Subclassing.
First, a heads up - I am not too familiar with OOP concepts, so this may just be some form of python functionality that I am not aware of.
In Kivy we can modify the behaviour and appearance of widgets by creating classes child to the widgets whose functions we want to alter, for instance:
class MyWidget(Button):
def __init__(self, **kwargs):
super(MyWidget, self).__init__(**kwargs)
self.size_hint = None
self.size = 200, 100
Then use MyWidget: or <MyWidget> to instantiate the widget in kv. This is good if I wish for my widgets to be parent to the root widget, but there are times when this is not wanted for instance when requiring a temporary Popup.
In which case I would just create a typical class which is instantiated when an event is triggered. Like so:
class Interface():
def __init__(self):
btn = Button()
btn.bind(on_press=self.some_callback)
self.popup = Popup(content=btn)
self.popup.open()
def some_callback(self, instance):
print('woo')
And this, to all appearances, looks fine, but the widget events (on_press here) don't trigger callback functions??? The bind() function will call the callback on the event, since I will be notified if the callback function definition has incorrect syntax, but for some reason the contents of the callback are not executed - only when Interface() inherits from the Widget() class (Interface(Widget): ...).
From the docs:
Widget interaction is built on top of events that occur. If a property changes, the widget can respond to the change in the ‘on_’ callback. If nothing changes, nothing will be done.
btn is the widget, it is instance of Button() which itself is a child of Widget, its method has no connection at all to what may be the parent class of Interface so why then is the callback only fully executed when Widget is the parent class of Interface? What am I missing?
Im making a QTableWidget in Pyqt and ran into a bit of an annoying hiccup.
I need to use widgets in my table for its functionality, so im using setCellWidget to add them to the table. However, widgets dont have the same methods available as QTableWidgetItem's do (especially regarding selection in the table).
Im wondering if its possible to do something subclassing both items, so i can have the methods of both, and how i woulda dd that to the table.
Something like:
class TableItem(QtGui.QTableWidgetItem, QtGui.QWidget):
def __init__(self, parent=None):
super(TableItem, self).__init__(parent)
self.check = QtGui.QCheckBox()
self.label = QtGui.QLabel('Some Text')
self.h_box = QtGui.QHBoxLayout()
self.h_box.addWidget(self.check)
self.h_box.addWidget(self.label)
and then somehow add that to my table as a TableWidgetItem so it displays widgets and also has selection methods available.
Any ideas here?
For reference:
setCellWidget: http://pyqt.sourceforge.net/Docs/PyQt4/qtablewidget.html#setCellWidget
QWidget: (easy to find, i cant post more than 2 links)
-Which doesnt have the nice methods for a table
QTableWidgetItem: http://pyqt.sourceforge.net/Docs/PyQt4/qtablewidgetitem.html#type
with isSelected and setSelected (Methods not avialble from a widget used in setCellWidget.
To return the widget in a cell you can use table.cellWidget(row, column) and then use your widgets methods on that. But beacuse setSelected and isSelected arent methods of a widget, you cant check for selection. I was hoping to subclass the two together to allow for both
--Basically I need to know how to get my class to 'return' the proper type when i call it to add to the table with setItem
I am not sure what you want to do but you could "inject" a method like:
class TableWidgetItem(QtGui.QTableWidgetItem):
def __init__(self, parent=None):
QtGui.QTableWidgetItem.__init__(self)
def doSomething(self):
print "doing something in TableWidgetItem"
class Widget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self)
tableWidgetItem = TableWidgetItem()
widget = Widget()
def widgetFunction(self):
tableWidgetItem.doSomething()
# as an instance method
settatr(widget, "widgetFunction", MethodType(widgetFunction, widget, type(widget)))
# or as a class method
settatr(widget, "widgetFunction", widgetFunction)
Then you can:
>>>widget.widgetFunction()
doing something in TableWidgetItem
(not tested)
The code below creates a simple QComboBox. But instead of using a "traditional" .addItem('myItemName') method it creates QStandardItem first and then adds it via QComboBox's .model().appendRow(). Since now I can access each QStandardItem individually I wonder if there is a way to assign CSS to each of them (to each QStandardItem) individually. The goal is to customize each item displayed in ComboBox pulldown menu. So far I am only able to assign a single CSS style to entire ComboBox globally.
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Combo(QtGui.QComboBox):
def __init__(self, *args, **kwargs):
super(Combo, self).__init__()
for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
item=QtGui.QStandardItem(each)
self.model().appendRow(item)
tree=Combo()
sys.exit(app.exec_())
It looks like this class has no setStyleSheet method, but you can use setBackground, setForeground and setTextAlignment methods. With QBrush you be able to customize elements. Of course it isn't so powerful as styleSheets but better than nothing.
http://pyqt.sourceforge.net/Docs/PyQt4/qstandarditem.html
Being new to pyside I am still having trouble understanding some GUI concepts, even aware of decent documentation.
I have a widget, dervied from QWidget, which I want to draw inside a function paintEvent, which is the function called to paint the widget (as far as I understood the documentation). Also, the method update should be used to update the widget, which calls the method paintEvent (as far as I understood the documentation).
In the following code skeleton I give a short overview of my code which should do two things:
- when initialized the widget should be drawn
- if required, the widget should be redrawn by a call to update inside the derived class
class MyWindow(QWidget):
def __init__(self, *args):
QWidget.__init__(self, *args)
self.update() # update the widget for the first time
def paintEvent(self, x):
self.setGeometry(300, 200, 970, 450)
self.setWindowTitle("My Window")
...
self.table_model = MyTableModel(...)
self.view = QTableView()
...
self.setLayout(layout)
def do_something(self):
....
self.update()
When running the code the widget is drawn as expected. But once the call to update is made inside do_something nothing happens, the widget is NOT redrawn! I also tried to use the method repaint instead, but the widget is still not redrawn.
How to fix the code to ensure the widget is redrawn from scratch by calling paintEvent?