I am intending to develop a user interface using rqt in ros. I have developed a user interface containing a QTPushButton and QTLabel in QT Designer. I have imported this ui file into my Python Plugin. So, when I run the plugin, the user interface does pop up. But now I want to add a callback to the PushButton. So, that if I press the Button then the label should show the text
"Button Clicked". I have tried many code snippets, but somehow nothing happens when I click the Button.
Can somebody please help. It is very urgent. Can somebody write the actual code for the callback.
My Plugin.py file is given below:
import os
import rospy
import rospkg
from qt_gui.plugin import Plugin
from python_qt_binding import loadUi
from python_qt_binding.QtGui import QWidget, QPushButton, QLabel
class MyPlugin(Plugin):
def __init__(self, context):
super(MyPlugin, self).__init__(context)
self.setObjectName('MyPlugin')
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-q", "--quiet", action="store_true", dest="quiet",
help="Put plugin in silent mode")
args, unknowns = parser.parse_known_args(context.argv())
if not args.quiet:
print 'arguments: ', args
print 'unknowns: ', unknowns
# Create QWidget
self._widget = QWidget()
# Get path to UI file which should be in the "resource" folder of this package
ui_file = os.path.join(rospkg.RosPack().get_path('rqt_mypkg'), 'resource', 'mainwindow.ui')
# Extend the widget with all attributes and children from UI file
loadUi(ui_file, self._widget)
# Give QObjects reasonable names
self._widget.setObjectName('MyPluginUi')
# Show _widget.windowTitle on left-top of each plugin (when
# it's set in _widget). This is useful when you open multiple
# plugins at once. Also if you open multiple instances of your
# plugin at once, these lines add number to make it easy to
# tell from pane to pane.
if context.serial_number() > 1:
self._widget.setWindowTitle(self._widget.windowTitle() + (' (%d)' % context.serial_number()))
# Add widget to the user interface
context.add_widget(self._widget)
self.text = QLabel("Data")
pushButton = QPushButton("Click Here")
pushButton.clicked.connect(self.buttonClicked)
def buttonClicked(self):
print("Button Clicked")
self.text.setText("Button Has been Clicked")
Also, my ui file looks like this "mainwindow.ui":
Form 0 0 400 300 Form 80 180 98 27 PushButton 100 80 66 17 TextLabel
↧