PyQt5- Add Custom QComboBox to Pop up Window - python-3.x

I have created a custom class inherited from QCombobox() as part of my main application which works fine. I now want to add the same widget to another window upon clicking the ChangeAttributesButton() (QPushButton()). Relevant snippets of code below.
import sys, os, glob
from PyQt5.QtWidgets import (QApplication, QLabel, QMdiSubWindow,
QMainWindow, QAction, QPushButton, QFileDialog, QComboBox)
from PyQt5.QtCore import Qt, pyqtSignal, QObject
from qgis.core import *
from qgis.gui import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Save reference to the QGIS interface
self.iface = iface
self.canvas = self.iface.mapCanvas()
self.project = QgsProject.instance()
self.setWindowTitle('Marker')
#add toolbar
self.toolbar = QToolBar("Grade Marker Toolbar")
self.toolbar.setIconSize(QSize(20,20))
self.addToolBar(self.toolbar)
self.SelectCategoryComboBox = SelectCategoryComboBox()
self.toolbar.addWidget(self.SelectCategoryComboBox)
self.toolbar.addSeparator()
self.ChangeAttributesButton = ChangeAttributesButton(self.canvas)
self.ChangeAttributesButton.setIcon(QIcon(r'abc.png'))
self.ChangeAttributesButton.setIconText('Change Grade')
self.toolbar.addWidget(self.ChangeAttributesButton)
self.toolbar.addSeparator()
self.ChangeAttributesButton.clicked.connect(self.ChangeAttributesButton.on_click)
class SelectCategoryComboBox(QComboBox):
def __init__(self):
super(SelectCategoryComboBox, self).__init__()
self.setEnabled(True)
categories = ['Select Category',"A+","A", "B", "C", "F" ]
for cat in categories:
self.addItem(cat)
The bit that is causing a problem is here:
class ChangeAttributesButton(QPushButton):
def __init__(self,canvas):
super(ChangeAttributesButton, self).__init__()
self.setEnabled(True)
self.canvas = canvas
def on_click(self,state):
window = ChangeAttributesWindow()
class ChangeAttributesWindow(QWidget):
def __init__(self):
super(ChangeAttributesWindow, self).__init__()
self.title = 'Change Feature Attributes'
category_box = SelectCategoryComboBox()
self.addWidget(category_box)
self.initUI()
def initUI(self):
self.show()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
I am getting
AttributeError: 'ChangeAttributesWindow' object has no attribute 'addWidget'`. I have tried changing the parent class of `ChangeAttributesWindow()
to
QMdiSubWindow()
and QMainWindow() but I still get an error in the same vein.
How do I add an instance of SelectCategoryComboBox() to a child or pop up window?
This is the complete error:

A QWidget does not have any method called addWidget() so this error is thrown.
If you want to add a widget to a window, you can add it:
Setting the parent of the widget to the window.
class ChangeAttributesWindow(QWidget):
def __init__(self):
super(ChangeAttributesWindow, self).__init__()
self.title = 'Change Feature Attributes'
category_box = SelectCategoryComboBox()
category_box.setParent(self)
category_box.move(30, 30)
self.initUI()
Use a layout established in the window.
class ChangeAttributesWindow(QWidget):
def __init__(self):
super(ChangeAttributesWindow, self).__init__()
self.title = 'Change Feature Attributes'
category_box = SelectCategoryComboBox()
lay = QVBoxLayout(self)
lay.addWidget(category_box)
self.initUI()

Related

PyQt5 button "clicked.connect()" not working when importing from class

I am running a test for clicked.connect failing issue.
I want to wrap all the widgets in the CentralWidget to a class called React. When I call React in CentralWidget, widgets like Button, self.writeLine and self.valLabel will show up, but when I pressed the react Button, it's not functioning, which is to update self.valLabel according to self.writeLine.
A snap shot: Buttom not working
I suspect that
Button.clicked.connect(self.BottomPressedTest) # no function when pressed
is not doing what is supposed to do. But have no clue why this is happening.
The entire test code is shown below,
from PyQt5 import QtCore, QtWidgets, QtGui
import sys
class CentralWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CentralWidget, self).__init__(parent)
self.mainLayout = QtWidgets.QGridLayout()
self.setLayout(self.mainLayout)
panel = React(1).groupbox
self.mainLayout.addWidget(panel, 0, 0)
class React(QtWidgets.QGroupBox):
def __init__(self, ch):
self.value = 0
self.groupbox = QtWidgets.QGroupBox()
self.vbox = QtWidgets.QVBoxLayout()
self.groupbox.setLayout(self.vbox)
Button = QtWidgets.QPushButton('React')
Button.clicked.connect(self.BottomPressedTest) # no function when pressed
self.writeLine = QtWidgets.QSpinBox()#QLineEdit()
self.writeLine.setFont(QtGui.QFont('Arial', 16))
self.valLabel = QtWidgets.QLabel()
self.valLabel.setFont(QtGui.QFont('Arial', 20))
self.valLabel.setText(f'React: {self.value}')
self.vbox.addWidget(Button)
self.vbox.addWidget(self.valLabel)
self.vbox.addWidget(self.writeLine)
def BottomPressedTest(self):
print('React bottom pressed.')
self.valLabel.setText(f'React: {self.writeLine.text()}')
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle(QtCore.QCoreApplication.applicationName())
self.centralWidget = CentralWidget()
self.setCentralWidget(self.centralWidget)
class MainApplication(QtWidgets.QApplication):
def __init__(self, argv: list):
super(MainApplication, self).__init__(argv)
self.mainWindow = MainWindow()
self.mainWindow.resize(200,200)
self.mainWindow.raise_()
self.mainWindow.show()
def main():
application = MainApplication([])
sys.exit(application.exec_())
if __name__ == "__main__":
main()
Edit
I changed the first few line in React to
class React(QtWidgets.QGroupBox):
def __init__(self, ch):
super().__init__()
self.value = 0
self.setStyleSheet('QGroupBox { color: white; border: 3px solid grey; }') #
self.vbox = QtWidgets.QVBoxLayout()
self.setLayout(self.vbox)
, and then change CentralWidget to
class CentralWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(CentralWidget, self).__init__(parent)
self.mainLayout = QtWidgets.QGridLayout()
self.setLayout(self.mainLayout)
panel = React(1)
self.mainLayout.addWidget(panel)
Now the problem is solved. Many thanks to #musicamante and #Passerby
Your React object is thrown away at the end of CentralWidget.__init__, with only the QGroupBox persisting. Save it in an attribute and your problem will be solved.
Note that your React object inherits from QGroupBox, but does not call its __init__ method, instead creating a new QGroupBox as a member.

Get a selected font in a subclassed QFontDialog

I'm trying to subclass QFontDialog and would like to retrieve the characteristics of the selected font. If I use getFont() a QFontDialog window appears first, ... I'm certainly doing something wrong.
Here's my example code :
from PyQt5.QtWidgets import (QFontDialog, QPushButton,
QMainWindow, QApplication,
QTabWidget, QWidget, QVBoxLayout)
import sys
class FontSelection(QFontDialog) :
def __init__(self, parent=None):
super(FontSelection, self).__init__(parent)
self.setOption(self.DontUseNativeDialog, True)
self.bouton = self.findChildren(QPushButton)
self.intitule_bouton = self.bouton[0].text().lower()
self.ouvertureBouton = [x for x in self.bouton if self.intitule_bouton in str(x.text()).lower()][0]
self.ouvertureBouton.clicked.disconnect()
self.ouvertureBouton.clicked.connect(self.font_recup)
def font_recup(self) :
self.font_capture()
def font_capture(self) :
if self.intitule_bouton in ['ok', '&ok'] :
font, self.intitule_bouton = self.getFont()
print(font)
class MainQFontDialogTry(QMainWindow):
def __init__(self):
super(MainQFontDialogTry, self).__init__()
self.setWindowTitle('QFontDialog subclassed try')
self.setGeometry(0, 0, 1000, 760)
self.setMinimumSize(1000, 760)
self.tab_widget = QTabWidget()
self.win_widget_1 = FontSelection(self)
widget = QWidget()
layout = QVBoxLayout(widget)
self.tab_widget.addTab(self.win_widget_1, "QFontDialog Tab")
layout.addWidget(self.tab_widget)
self.setCentralWidget(widget)
self.qfont = FontSelection()
self.qfont.font_recup()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainQFontDialogTry()
ex.show()
sys.exit(app.exec_())

pyqt signal from a window not changing the main window widgets

I can pass values between two windows using signal,
and I want the plain text change, while I press a button from another window.
but the insertPlainText and even text.show() are not working
I've tried sending the signal to the init part of the Mainwindow,
tried update, repaint, but none of them works.
appreciate for any of your help, thanks!
the search_send method in class SearchWindow to MainWindow method test_
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
self.ui.tag_box.hide()
def test_(self, i): # -------problem here------------------
print(i) // <- value here were right
self.ui.tag_box.insertPlainText(i) # -------Plain Text does not change-------
self.ui.tag_box.show()# -------Plain Text does not show either--------------
class SearchWindow(QtWidgets.QMainWindow):
signal=pyqtSignal(str)
def __init__(self, endpoint=None, user=None, password=None, points_link=None):
super(SearchWindow, self).__init__()
self.ui = Ui_Search()
self.ui.setupUi(self)
self.ui.pushButton_2.clicked.connect(self.search_send)
def search_send(self): # -------problem here------------------
tag_list = [tag1,tag2, tag3]
otherClass = MainWindow()
self.signal.connect(otherClass.test_)
for k in tag_list:
self.signal.emit(k)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
sys.exit(app.exec_())
As we're unclear what Ui_Dialog contains, I can't tell what's wrong exactly, but can show you how Signal should be used.
This is example of Signal that's transferring text to Slot. Name may differ in PyQt5 - change accordingly.
from PySide2.QtWidgets import QWidget, QApplication, QPlainTextEdit, QTextEdit, QVBoxLayout
from PySide2.QtCore import Signal, Slot
import sys
class MainWindow(QWidget):
sig = Signal(str)
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Main")
self.layout = QVBoxLayout()
self.text = QTextEdit()
self.layout.addWidget(self.text)
self.setLayout(self.layout)
self.text.textChanged.connect(self.onChange)
def onChange(self):
self.sig.emit(self.text.toPlainText())
class SubWindow(QWidget):
def __init__(self, connect_target: MainWindow):
super(SubWindow, self).__init__()
self.setWindowTitle("Sub")
self.layout = QVBoxLayout()
self.text = QPlainTextEdit()
self.layout.addWidget(self.text)
self.setLayout(self.layout)
connect_target.sig.connect(self.onSignal)
#Slot(str)
def onSignal(self, text):
self.text.insertPlainText(text + '\r\n')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
window_sub = SubWindow(window)
window_sub.show()
sys.exit(app.exec_())
Anything inside Main will be inserted to QPlainTextEdit as you wanted.

Qt pass focus between windows

In the following example I have a Qt button which opens a pop-out window. However, the pop-out window takes the focus entirely. Is it possible to modify this such that I can still interact with the main window by moving the mouse over it, e.g. to change the value of QDoubleSpinBox even when the pop-out window is open? I think I may need to use the QtHoverEvent class put I can't find a good example of how to do this.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QGridLayout, QDoubleSpinBox,\
QLabel
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = QPushButton('Button', self)
btn.move(50, 50)
btn2 = QDoubleSpinBox(self)
btn2.move(50,100)
self.sideWindowTest = sideWindowTest(self)
btn.clicked.connect(lambda: self.sideWindowTest.setupWindow())
self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('Main')
self.show()
class sideWindowTest(object):
myWdg = None
def __init__(self, parent):
super().__init__()
self.viewer = parent
def initiateMenuBar(self):
self.myWdg.setWindowTitle('Phasing')
self.myWdg.setWindowModality(Qt.ApplicationModal)
MenuBar = QVBoxLayout()
self.labels = {
'phase 0': QLabel('Phase 0', self.myWdg),
}
self.inputs = {
'phase 0': QDoubleSpinBox(self.myWdg),
}
for i in self.inputs.values():
i.installEventFilter(self.myWdg)
self.inputs['phase 0'].setValue(0)
MenuBar.addWidget(self.labels['phase 0'])
MenuBar.addWidget(self.inputs['phase 0'])
MenuBar.addStretch(1)
return MenuBar
def setupWindow(self):
if not self.myWdg:
self.myWdg = QWidget()
MenuBar = self.initiateMenuBar()
grid = QGridLayout()
grid.setSpacing(10)
grid.addLayout(MenuBar, 0, 0, 1, 2)
self.myWdg.setLayout(grid)
self.myWdg.setGeometry(0, 0, 400, 100)
self.myWdg.show()
self.myWdg.activateWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Yes, but you have to explicitly wrap your QWidget's in a QDialog or a QMainWindow. By default, if you create a orphan QWidget (as you are doing), Qt will wrap it in a QDialog.
Just change your class type to QDialog. Also, you should probably pass in the initial main window as the parent.
btn.clicked.connect(lambda: self.sideWindowTest.setupWindow(self))
...
def setupWindow(self, parent=None):
if not self.myWdg:
self.myWdg = QDialog(parent)

PyQt keyPressEvent not triggered from QDialog

I have a simple example of of a dialog window that has the keyPressEvent method. However, no matter what is typed when the sub window has focus, the event is not triggered.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import PyQt4.Qt
class KpeWindow(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
main = QVBoxLayout(self)
label = QLabel(self)
label.setText('Test the keyPressEvent')
self.adjustSize()
self.setLayout(main)
def keyPressEvent(self, event):
QMessageBox.warning(self, 'MDI', 'keyPressEvent')
super().keyPressEvent(event)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('KeyPressEvent Test')
child = KpeWindow()
self.setCentralWidget(child)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
The following code works:
class KpeWindow(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self,parent)
main = QVBoxLayout(self)
label = QLabel(self)
label.setText('Test the keyPressEvent')
main.addWidget(label)
self.adjustSize()
self.setLayout(main)
def keyPressEvent(self, event):
QMessageBox.warning(self, 'MDI', 'keyPressEvent')
self.parent().keyPressEvent(event)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle('KeyPressEvent Test')
main = QVBoxLayout(self)
child = KpeWindow(self)
child.setFocusPolicy(Qt.StrongFocus)
self.setFocusProxy(child)
main.addWidget(child)
child.setFocus(True)
self.adjustSize()
self.setLayout(main)
I am not sure which of my changes work, I suspect setFocusProxy. In general I would recommend using QWidget as the child, and putting things into layouts even when there are no siblings.
The keyPressEvent is sensitive to the focus policy. In your example, the event is going to the QMainWindow (if you move the keyPressEvent to there, it does receive key events).
Is there any reason to have a dialog within a window? If you launch the dialog in the usual way, using child.show(), child.exec_() instead of setCentralWidget, it shows in a separate window and captures the key event.

Resources