ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A
Ask Your Question

How to Add Callbacks for a QTWidget in ROS?

asked 2014-10-16 10:51:01 -0500

ish45 gravatar image

updated 2014-10-16 11:18:48 -0500

gvdhoorn gravatar image

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 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)


    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
          # 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

        self.text = QLabel("Data")
         pushButton = QPushButton("Click Here")

    def buttonClicked(self):
        print("Button Clicked")
        self.text.setText("Button Has been Clicked")

Also, my ui file looks like this "mainwindow.ui":

<ui version="4.0">
  <widget class="QWidget" name="Form">
    <property name="geometry">
    <property name="windowTitle">
    <widget class="QPushButton" name="pushButton">
      <property name="geometry">
      <property name="text">
    <widget class="QLabel" name="label">
      <property name="geometry">
      <property name="text">
edit retag flag offensive close merge delete


It looks like you're creating a new button rather than retrieving the existing button that is created by the UI file.

ahendrix gravatar imageahendrix ( 2014-10-16 11:52:20 -0500 )edit

Can you tell me how to reference the button from the UI File?

ish45 gravatar imageish45 ( 2014-10-16 11:53:37 -0500 )edit

Can somebody please help?

ish45 gravatar imageish45 ( 2014-10-16 12:50:59 -0500 )edit

I don't know how to retrieve a button that is created by the UI file offhand, but it shouldn't be hard to find a QT tutorial online.

ahendrix gravatar imageahendrix ( 2014-10-16 13:18:56 -0500 )edit

I have searched the web for the last 2 days. But could not find a way of referencing the buttons from the ui file. I hope somebody else would be able to answer. It should be easy I suppose.

ish45 gravatar imageish45 ( 2014-10-16 13:23:04 -0500 )edit

I did this search: and the first link (for me) looks like a fairly good reference: .

ahendrix gravatar imageahendrix ( 2014-10-16 13:30:15 -0500 )edit

Thanks. I would have a look at these.

ish45 gravatar imageish45 ( 2014-10-16 14:16:48 -0500 )edit

There is a helper function in python_qt_binding called loadUi that will make things easier and more portable than those links. I have detailed its use below.

Aaron Blasdel gravatar imageAaron Blasdel ( 2014-10-17 16:23:49 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2014-10-17 16:21:53 -0500

Aaron Blasdel gravatar image

updated 2014-10-17 16:26:57 -0500

The rqt system uses the "LoadUi" helper function to load ui files from python_qt_binding

It is imported like so:

from python_qt_binding import loadUi

And called like this:

ui_file = *PATH TO YOUR UI FILE*
loadUi(ui_file, my_widget)

Each widget is loaded as a child widget to "my_widget" and it can be accessed using the name you gave it in the ui_file as so:


Then simply connect the relevant callbacks as so:


If you want to see a working example please look at rqt_msg here:
edit flag offensive delete link more


Thanks. Yes, I have got this working now. I did not know that each widget would be a child widget of my_widget as in this case.

Thanks to you anyway. It was quite easy in the end.

ish45 gravatar imageish45 ( 2014-10-17 17:01:00 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-10-16 10:51:01 -0500

Seen: 494 times

Last updated: Oct 17 '14