I am creating a plugin for QGIS but am having trouble with the Qt components. I have used Plugin Builder and Qt designer to create all necessary files for the plugin. I want to use a QStackedWidget inside my QDialog and I can open the dialog just fine when I don't try to access the "pages" (or widgets) of the stacked widget. But I am getting an error when trying to reach the pages/objects located inside the pages of a stacked widget from my code.

Simple code example:

def __init__(self, iface):

    self.dlg = Dialog() 
    self.stackedWidget = self.dlg.stackedWidget

def func(self):
    :code that does some stuff:

Returns the error: AttributeError: 'QStackedWidget' object has no attribute 'page'

Similarly if I try to work with just a stacked widget not inside a QDialog (again opens just fine without the code that causes errors) I get the same kind of error.

Code Example:

def __init__(self, iface):

    self.stackedWidget = StackedWidget() 
    self.page = self.stackedWidget.page

def func(self):
    :code that does some stuff:

Returns the error: AttributeError: 'QWidget' object has no attribute 'pushButton'

I am not sure what I am doing wrong or what is causing the error but will of course provide whatever code/information that might be needed to help me solve this problem.

I am thankful for whatever help you can provide!

  • have you compiled your UI file with the below command? pyuic4 -o ui_myplugin.py ui_myplugin.ui – vinayan Apr 27 '15 at 12:04
  • Hmm, you know I am not sure I have since the latest build of plugin builder doesn't require it. Will try this and report back! Thanks! – Platnik Apr 27 '15 at 12:21
  • Hi! I tried compiling it as suggested doesn't seem to make a difference. I don't think it is a necessary step anymore and the .py file ends up not being used. Maybe compiling and using this .py-file might make things work correctly? I am not sure how to open that file instead of my .ui file though. – Platnik Apr 27 '15 at 12:39

There is no page attribute on the QStackWidget object. Here are all the methods that it has: http://doc.qt.io/qt-4.8/qstackedwidget.html

What you need to do get the widget is call the widget function:

widget = self.stackedWidget.widget(index)

where index is the page index of the widget.

  • Thank you for your answer! I have tried messing around with calling through the index methods and probably should have mentioned it in my original post (sorry). Using your method returns the exact same error as I had previously, however if I go outside the range of my index I get the error "AttributeError: 'NoneType' object has no attribute 'pushButton'" instead. Am I still messing up somehow? (I have also tried just calling the widget through currentWidget() and I get the same error as in my original post) – Platnik Apr 27 '15 at 12:45
  • Going to need to see some more code if that is the case – Nathan W Apr 27 '15 at 12:47
  • Understandable, I have a test plugin that I basically created just to try and get a button inside a stacked widget change the page of the stacked widget. It is pretty light on code and I could try cleaning that up and posting it. In essence though there isn't much stuff that I have added to the plugin created in plugin designer besides the qt-components (created in qt-designer) and the code I have already posted. Give me a moment and I will try to make it presentable. Anything in particular you would be interested in seeing? – Platnik Apr 27 '15 at 12:59
  • having a hard time pasting the code because of character limit, I can make the whole folder available to you through google drive if you would like. – Platnik Apr 27 '15 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.