5

I'm creating QGIS plugin and I'm informing user about the progress of computation via QProgressBar, that is shown in messageBar. I have added Cancel button, that gives user a possibility to end the computation (it shows QMessageBox with question whether the user wants to end computation or not). I'd like to implement the same functionality as Cancel button to x button, that is created automatically as part of messageBar or completely erase it, so there will be only Cancel button.

Does anybody have an idea how to get rid of x button or what signal is emitted, when it is clicked? I found one signal in QObject class - destroyed, but if I understand it correctly from the documentation, the process of destroying QObject cannot be stopped this way.

For illustration, my code looks like this:

def progressBar(self):
    """Initializing progress bar.
        """
    self.progressMessageBar = iface.messageBar().createMessage(u"Ground Radiation Monitoring:",u" Computing...")
    self.progress = QProgressBar()
    self.progress.setMaximum(100)
    self.progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)

    self.cancelButton = QtGui.QPushButton()
    self.cancelButton.setText('Cancel')
    self.progressMessageBar.layout().addWidget(self.cancelButton)
    self.progressMessageBar.layout().addWidget(self.progress)
    self.iface.messageBar().pushWidget(self.progressMessageBar, iface.messageBar().INFO)

    self.cancelButton.clicked.connect(self.onCancelButton)

And messageBar:

messageBar

1

That x is a QToolButton which we can identify from the message bar as it's the only QToolButton present. And instead of destroying it, we could just hide it:

def progressBar(self):
    """Initializing progress bar.
        """
    self.progressMessageBar = iface.messageBar().createMessage(u"Ground Radiation Monitoring:",u" Computing...")
    self.progress = QProgressBar()
    self.progress.setMaximum(100)
    self.progress.setAlignment(Qt.AlignLeft|Qt.AlignVCenter)

    self.cancelButton = QtGui.QPushButton()
    self.cancelButton.setText('Cancel')
    self.progressMessageBar.layout().addWidget(self.cancelButton)
    self.progressMessageBar.layout().addWidget(self.progress)

    # Let's give the messageBar() a name
    msgBar = self.iface.messageBar()
    msgBar.pushWidget(progressMessageBar, iface.messageBar().INFO)
    # Now we can find the x button which is the only QToolButton in the messageBar()
    # and hide it
    msgBar.findChildren(QToolButton)[0].setHidden(True)

    self.cancelButton.clicked.connect(self.onCancelButton)

Before:

Before

After:

After

  • 1
    @pandaActive - Most welcome! Glad it worked :) – Joseph Apr 28 '17 at 14:38

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.