Domanda

I have a shell script with an old text-based user interface: I want to replace my shell code with python and create a graphical user interface using qt designer tool and pyside.

I was able to add a menu bar, a tab and some radio buttons.

When a user selects a radio button I would like to show in a text box a brief description of what that selection means; when it selects another radio button a different description on the same text box should appear and so on.

My questions are:

  1. What widget should I use as text box ? I was thinking at Text Browser as Display Widget, but I'm not sure. A Label ? A Text Edit ?
  2. When I connect a radio button to the Text Browser I can see some interesting slots under the Text Browser such as insertHtml or insertPlainText, but as soon as I select the signal clicked() on the radio button they disappear and I'm not even able to find them again.
  3. I tried also to connect a button with a Label widget, but I'm not able to find any kind of setText slot. Reading the documentation I know setText exists, but I cannot use it inside my code.

Thanks for any kind of your support.

È stato utile?

Soluzione

Qt has several different built-in APIs for showing help messages to the user, so I would suggest you give consideration to these before trying to roll your own solution:

  • For simple a message that describes what the GUI element is, you can set a tooltip.
  • For slightly longer messages that describe what a GUI element does, you can set a status tip. These are most often used for menu items and toolbar buttons.
  • For longer, more informative messages, you can use what's this mode. These messages can make use of html formatting, and can also include links to the main help.

The demo script below uses all these methods, and also shows how to handle links in "what's this" messages:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.statusBar()
        self.toolbar = self.addToolBar('toolbar')
        action = QtGui.QWhatsThis.createAction(self)
        action.setStatusTip('Enter what\'s this mode')
        self.toolbar.addAction(action)
        widget = QtGui.QGroupBox(self)
        layout = QtGui.QVBoxLayout(widget)
        self.setCentralWidget(widget)
        for name in 'one two three four'.split():
            checkbox = QtGui.QCheckBox('Checkbox %s' % name, self)
            checkbox.setToolTip('Tooltip for checkbox %s' % name)
            checkbox.setWhatsThis("""
                <b>What\'s this text</b>:<br><br>
                Checkbox %s<br><br>
                <a href="url-%s">link to main help</a>
                """ % (name, name))
            checkbox.installEventFilter(self)
            layout.addWidget(checkbox)

    def eventFilter(self, source, event):
        if event.type() == QtCore.QEvent.WhatsThisClicked:
            print(event.href())
        return QtGui.QMainWindow.eventFilter(self, source, event)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 250, 200)
    window.show()
    sys.exit(app.exec_())

Altri suggerimenti

To use setText after a button click :

void myclass::on_mybutton_click()
{
    ui->mylabel->setText("My awesome text");
}

EDIT : This answer is in C++, but you'll be able to convert it to python easily. Just for you to have the structure.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top