Reputation: 408
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:
Upvotes: 0
Views: 898
Reputation: 243897
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:
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()
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()
Upvotes: 1