PySide PyQt QDataWidgetMapper - model-view-controller

I try to connect a QtGui.QPlainTextEdit to a Model with QDataWidgetMapper.
I dont get any errors, just nothing in the TextEdit.
I dont get it and i cant find good example Code.
Here is some ExampleCode.
I really hope that someone could help me.
from PySide import QtCore, QtGui
import sys
class ComponentsListModel(QtCore.QAbstractListModel):
def __init__(self, components=[], parent = None):
super(ComponentsListModel, self).__init__(parent=None)
self.components = components
self.list = parent
def rowCount(self, parent):
return len(self.components)
def data(self, index, role):
row = index.row()
if role == QtCore.Qt.DisplayRole:#index.isValid() and
value = self.components[row]
return value
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self._build_ui()
def _build_ui(self):
self.layout = QtGui.QVBoxLayout()
self.listView = QtGui.QListView()
self.model = ComponentsListModel(components = ['1', '2', '3'])
self.listView.setModel(self.model)
self.text = QtGui.QPlainTextEdit()
self.layout.addWidget(self.listView)
self.layout.addWidget(self.text)
self.setLayout(self.layout)
self._mapper = QtGui.QDataWidgetMapper(self)
self._mapper.setModel(self.model)
self._mapper.setSubmitPolicy(QtGui.QDataWidgetMapper.AutoSubmit)
self._mapper.addMapping(self.text, 0)
self._mapper.toFirst()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle('Plastique')
mySW = MainWindow()
mySW.show()
sys.exit(app.exec_())

You will need to add a condition for Qt.EditRole in your data function inside ComponentsListModel class
if role == Qt.EditRole:
value = self.components[row]
return value

Related

Window closeEvent() not triggering

I'm having a problem with the closeEvent() function override in a class which is not triggering.
This is the code I have in the main.py file:
from mainwindow import *
if __name__ == '__main__':
app = QApplication()
mainwindow = QMainWindow()
window = Ui_MainWindow()
window.setupUi(mainwindow)
mainwindow.show()
app.exec_()
And then in another file named mainwindow.py I have:
class Ui_MainWindow(QMainWindow):
def setupUi(self, MainWindow):
.......
def retranslateUi(self, MainWindow):
........
def closeEvent(self,event):
print("Testing")
But when I close the window the closeEvent() function never runs.
I'm missing something or doing it wrong but I can't figure out what it is.
Please help.

wxPython - PlateButton stopping wx.EVT_KEY_DOWN event

In my app, when I press that PlateButton, the wx.EVT_KEY_DOWN stop working. I don't know what's going on. If I erase the bmpImage code and set the parent of the PlateButton to self, the event doesn't even fire in the first place.
Thanks!
import wx
import wx.lib.platebtn as pb
class MainFrame(wx.Frame):
def __init__(self, parent):
super().__init__(parent)
self.initUI()
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def initUI(self):
bmpImage = wx.StaticBitmap(self, wx.ID_ANY)
bmpImage.SetBitmap(wx.Bitmap('image.JPG', wx.BITMAP_TYPE_ANY))
btn = pb.PlateButton(bmpImage, -1, 'Click Me!', style=pb.PB_STYLE_NOBG)
def OnKey(self, event):
print('Key pressed!')
event.Skip()
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()
If you run the following code, you'll see that the platebutton is grabbing the event and obviously not skipping the event, thus it is not available to the frame, which you are binding to.
import wx
import wx.lib.platebtn as pb
class MainFrame(wx.Frame):
def __init__(self, parent):
super().__init__(parent)
self.initUI()
self.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def initUI(self):
text = wx.StaticText(self, -1, "Key Test", pos=(10, 10))
btn = pb.PlateButton(self, -1, 'Click Me!', pos=(10,40), size=(100,25))
btn.Bind(wx.EVT_KEY_DOWN, self.OnKey)
def OnKey(self, event):
print(event.GetEventObject())
print('Key pressed!')
event.Skip()
app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

PyQt5 - How can I add image in 'pushbutton' widget? | (not icon)

Is there a function or option that adds an image in PushButton widget? In this situation, The added image means an image as a background applied to the entire widget, not an image as an 'icon'. I tried to find this option and I use the 'setStyleSheet' function, but it doesn't work. What is a problem, and how can I add an image in pushbutton?
btn6.setStyleSheet(
"color: black;"
"border-style: solid;"
"border-width: 2px;"
"border-color: #FFB400;"
"border-radius: 3px;"
"background-color: #FFD732;"
"**background-image: url('D:\PyQt5_Tutorial\Test.png')")**
You can just override QPushButton.paintEvent() for this. And dont forget to add interactivity to reflect different states of button: sunken, disabled, mouse hover, input focus.
from PyQt5 import QtWidgets, QtGui
class Button(QtWidgets.QPushButton):
def __init__(self, parent = None) -> None:
super().__init__(parent)
self._image = None
def setImage(self, image):
self._image = image
self.update()
class Button(QtWidgets.QPushButton):
def __init__(self, parent = None) -> None:
super().__init__(parent)
self._image = None
def setImage(self, image):
self._image = image
self.update()
def paintEvent(self, event):
if self._image is None:
return
opt = QtWidgets.QStyleOptionButton()
self.initStyleOption(opt)
rect = self.rect()
painter = QtGui.QPainter(self)
self.style().drawControl(QtWidgets.QStyle.CE_PushButtonBevel, opt, painter, self)
if opt.state & QtWidgets.QStyle.State_Sunken:
rect.adjust(2,2,2,2)
painter.drawImage(rect, self._image)
if opt.state & QtWidgets.QStyle.State_MouseOver:
color = self.palette().color(QtGui.QPalette.Highlight)
color.setAlpha(50)
painter.fillRect(self.rect(), color)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
button = Button()
button.setImage(QtGui.QImage("D:\\PyQt5_Tutorial\\Test.png"))
button.show()
app.exec()

PySide2, How to create a Widget above Docking

The docking is created as required. What I'm not able to do is creating 'Shelf' widget above docking.
It shouldn't be part of docking and it should remain at the top. When hidden, docks should cover up the
space and when shown again docks should move below.
import sys
from PySide2 import QtGui, QtCore, QtWidgets
class Shelf(QtWidgets.QTabWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.tab1 = QtWidgets.QWidget()
self.tab2 = QtWidgets.QWidget()
self.tab3 = QtWidgets.QWidget()
self.addTab(self.tab1, "Tab 1")
self.addTab(self.tab2, "Tab 2")
self.addTab(self.tab3, "Tab 3")
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.centre = QtWidgets.QMainWindow(self)
self.centre.setWindowFlags(QtCore.Qt.Widget)
self.centre.setDockOptions(
QtWidgets.QMainWindow.AnimatedDocks |
QtWidgets.QMainWindow.AllowNestedDocks)
self.setCentralWidget(self.centre)
self.dockCentre1 = QtWidgets.QDockWidget(self.centre)
self.dockCentre1.setWindowTitle('Centre 1')
self.centre.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dockCentre1)
self.dockCentre2 = QtWidgets.QDockWidget(self.centre)
self.dockCentre2.setWindowTitle('Centre 2')
self.centre.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dockCentre2)
self.dockLeft = QtWidgets.QDockWidget(self)
self.dockLeft.setWindowTitle('Left')
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dockLeft)
self.dockLeft1 = QtWidgets.QDockWidget(self)
self.dockLeft1.setWindowTitle('Left')
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.dockLeft1)
self.dockRight = QtWidgets.QDockWidget(self)
self.dockRight.setWindowTitle('Right')
self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockRight)
self.dockRight1 = QtWidgets.QDockWidget(self)
self.dockRight1.setWindowTitle('Right')
self.addDockWidget(QtCore.Qt.RightDockWidgetArea, self.dockRight1)
self.menuBar().addMenu('File').addAction('Quit', self.close)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.setGeometry(500, 50, 600, 400)
window.show()
sys.exit(app.exec_())

Cannot Get Pygtk to Display Label in Hbox

I'm extremely new to Pygtk and Stackoverflow in general. I'm trying to build a small dictionary application: I have one master VBox and an Hbox containing two Vboxes divided by a Vseparator. I am trying to display text through a label in the right-hand Vbox, but it will not appear. Here's my pitiful code:
import gtk
import pygtk
from Wordlist import *
pygtk.require('2.0')
def click_handler(button):
for i in nouns:
print i
k = gtk.Label(str=i)
k.show()
meaningvbox.pack_start(k,True,True,0)
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(300,400)
window.set_title("English-Japanese Reference")
window.show()
window.connect("delete-event", gtk.main_quit)
vbox = gtk.VBox(False,0)
window.add(vbox)
vbox.show()
hbox = gtk.HBox(True,0)
vbox.pack_end(hbox,False)
hbox.show()
hbox2 = gtk.HBox(True,0)
vbox.pack_end(hbox2,False)
hbox2.show()
vsep = gtk.VSeparator()
vbox.pack_start(vsep)
vsep.show()
dichbox = gtk.HBox() #### These are boxes created to store the words
vbox.pack_start(dichbox)
wordvbox = gtk.VBox()
dichbox.pack_start(wordvbox)
wordvbox.show()
meaningvbox = gtk.VBox()
dichbox.pack_start(meaningvbox)
meaningvbox.show()
label = gtk.Label(str="hi")
meaningvbox.pack_start(label)
label.show()
verbButton = gtk.Button(label="Verbs")
hbox.pack_end(verbButton,True,False)
verbButton.set_size_request(100,30)
verbButton.show()
nounButton = gtk.Button(label="Nouns")
nounButton.set_size_request(100,30)
hbox.pack_end(nounButton,True,False)
nounButton.show()
nounButton.connect("clicked", click_handler)
familyButton = gtk.Button(label="Family")
familyButton.set_size_request(100,30)
hbox.pack_end(familyButton,True,False)
familyButton.show()
particleButton = gtk.Button(label="Particles")
hbox2.pack_end(particleButton,True,False)
particleButton.set_size_request(100,30)
particleButton.show()
adjectiveButton = gtk.Button(label="Adjectives")
adjectiveButton.set_size_request(100,30)
hbox2.pack_end(adjectiveButton,True,False)
adjectiveButton.show()
pronounButton = gtk.Button(label="Pronouns")
pronounButton.set_size_request(100,30)
hbox2.pack_end(pronounButton,True,False)
pronounButton.show()
def main():
gtk.mainloop()
main()
You forgot to call dichbox.show().
I would also recommend to restructure your code and group similar function calls, use show_all() instead of many show()s and use a class for the whole window.
That's what it would look like:
import pygtk
pygtk.require('2.0')
import gtk
from Wordlist import *
class Window(gtk.Window):
def __init__(self):
gtk.Window.__init__(self,gtk.WINDOW_TOPLEVEL)
self.set_size_request(300,400)
self.set_title("English-Japanese Reference")
self.connect("delete-event", gtk.main_quit)
verbButton = gtk.Button(label="Verbs")
nounButton = gtk.Button(label="Nouns")
nounButton.connect("clicked", self.click_handler)
familyButton = gtk.Button(label="Family")
particleButton = gtk.Button(label="Particles")
adjectiveButton = gtk.Button(label="Adjectives")
pronounButton = gtk.Button(label="Pronouns")
verbButton.set_size_request(100,30)
nounButton.set_size_request(100,30)
familyButton.set_size_request(100,30)
particleButton.set_size_request(100,30)
adjectiveButton.set_size_request(100,30)
pronounButton.set_size_request(100,30)
hbox = gtk.HBox(True,0)
hbox.pack_end(verbButton, True, False)
hbox.pack_end(nounButton, True, False)
hbox.pack_end(familyButton, True, False)
hbox2 = gtk.HBox(True,0)
hbox2.pack_end(particleButton, True, False)
hbox2.pack_end(adjectiveButton, True, False)
hbox2.pack_end(pronounButton, True, False)
label = gtk.Label("hi")
self.meaningvbox = gtk.VBox()
self.meaningvbox.pack_start(label)
wordvbox = gtk.VBox()
vsep = gtk.VSeparator()
dichbox = gtk.HBox()
dichbox.pack_start(wordvbox)
dichbox.pack_start(vsep)
dichbox.pack_start(self.meaningvbox)
vbox = gtk.VBox(False, 0)
vbox.pack_end(hbox, False)
vbox.pack_end(hbox2, False)
vbox.pack_start(dichbox)
self.add(vbox)
self.show_all()
def click_handler(self,button):
for i in nouns:
k = gtk.Label(i)
k.show()
self.meaningvbox.pack_start(k, True, True, 0)
def main():
win = Window()
gtk.main()
main()

Resources