1

I'm using PyQt5 and QT Designer to create the user interface for my QGIS plugin. Now I'm learning to use QTabWidget and QScrollArea. What I don't understand is how to use the objects inside tabs or scroll areas. For example in my dialog there are two tabs, inside these there's a scroll area and finally the other objects like labels, lineEdits and buttons. You can see this in the image below. The problem is that if I try to use, as usual in my run method, simple functions like: self.dlg.label_2.setText() or self.dlg.lineEdit.text(), I receive this kind of error log: AttributeError: 'Dialog' object has no attribute 'label_2'. How to solve it?

enter image description here

1

You can solve it putting lines above mentioned in add_action method instead run method. To exemplify this, in my test plugin, I added a QTabWidget object and, inside first and second tabs, a QLabel object; as it can be observed in following image (only for first Tab).

enter image description here

Afterward, I modified plugin code as follows for changing default text in Qt5 Designer ('TextLabel') for 'test' in first tab and for 'another test' in second tab.

.
.
.
    def add_action(
.
.
.
        self.actions.append(action)

        self.dlg.label.setText("test")
        self.dlg.label_2.setText("another test")
.
.
.
        return action
.
.
.

After running 'Reload Plugin', I launched it and text inside first and second Tabs were changed as expected. For first tab:

enter image description here

For second tab:

enter image description here

| improve this answer | |
  • Hi thanks for your answer. Can I see the rest of your plugin? I don't understand what actions is and where I can call the method add_action(). More over I need to trigger it when I click on a button. For example in my run method in this moment I have this: self.dlg.pushButton.clicked.connect(lambda:self.dlg.label.setText("test") – Lorenzo Jan 18 at 18:20
  • I only added two lines referred to labels. Other lines are acting as references in add,_actions for you find easily its position in this method. – xunilk Jan 18 at 18:41
  • By the way, I supposed that you are using plugin builder. – xunilk Jan 18 at 18:45
  • No I'm not, but If this is a "standard" method I could understand how to use it. – Lorenzo Jan 18 at 18:56
  • It is easier to work with plugin builder. I don't like the behavior of new versions of this plugin so, I use an older version from this link dropbox.com/s/ehjvylihfmz28o2/pluginbuilder3.zip?dl=0. It is recommendable to also use experimental Reload plugin. Therefore, any changes you have made to your plugin are updated immediately without leaving QGIS. – xunilk Jan 18 at 19:15

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