ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ros_qt project setup

asked 2021-09-08 07:06:39 -0500

SurajJadhav gravatar image

I m using Qt5 in Ubuntu 20.04 and ROS noetic.
Generally the directory structure for ROS Qt project is:

image description

This can also be seen here in a standard ros_qt project.

But when I create a ROS package in Qt, following directory structure is created: image description

Do we have to create other folders and files manually?
What about .ui file, I think it should have been created by the Qt Creator, What am I missing?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2021-09-09 12:42:14 -0500

SurajJadhav gravatar image

After conducting experiments on the answer provided by @Ranjit Kathiriya , I was able to find that the directory structure for ROS-Qt project and files is not compulsory but it is mandatory to have 4 kind of files:
1. main.cpp
2. mainwindow.cpp
3. mainwindow.h
4. mainwindow.ui
The names of above files can vary. It is mandatory to declare all files in CMakeLists.txt (they can be located in different folders or same).

In order to run the project the ui_mainwindow.h file is mandatory which is auto generated in build directory of catkin workspace if CMAKE_AUTOUIC macro is set in CMakeLists.txt.

Also there is no need to manually create 3 of the above mentioned 4 files. Once an empty project is created in Qt Creator go to:
FIle -> New File Or Project
Following window will appear, choose Qt under Files and Classes and then on right side panel choose Qt Designer Form Class, follow steps by clicking on choose and you can create files 2, 3 and 4.

image description

edit flag offensive delete link more

Comments

Tick on your answer, that can help someone.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-09-10 02:01:54 -0500 )edit

:D, currently not credible enough to do that.

SurajJadhav gravatar image SurajJadhav  ( 2021-09-10 03:30:12 -0500 )edit

You can try now.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-09-10 03:31:10 -0500 )edit

Thank you.

SurajJadhav gravatar image SurajJadhav  ( 2021-09-10 03:37:21 -0500 )edit
0

answered 2021-09-08 07:47:48 -0500

Ranjit Kathiriya gravatar image

updated 2021-09-08 08:20:57 -0500

Hello @surajj4837,

notation$ tree .
.
├── CMakeLists.txt
├── include
│   └── ai_robotic_auto_annotation
│       ├── clickable.h
│       ├── clickablelabel.h
│       ├── mainwindow.h
│       └── my_qlabel.h
├── launch
│   └── ai_robotic_auto_annotation.launch
├── package.xml
├── resources
│   ├── about.png
│   ├── ai_icon.png
│   ├── annotation.png
│   ├── changedir.png
│   ├── close.png
│   ├── create.png
│   ├── edit.png
│   ├── exit.png
│   ├── icon.png
│   ├── model.png
│   ├── next.png
│   ├── open_directory.png
│   ├── open_single.png
│   ├── previous.png
│   ├── redo.png
│   ├── remove.png
│   ├── reset.png
│   ├── save.png
│   ├── train.png
│   ├── tutorials.png
│   └── undu.png
├── resources.qrc
├── resources.qrc.autosave
└── src
    ├── ai_robotic_auto_annotation.pro
    ├── clickable.cpp
    ├── clickablelabel.cpp
    ├── main.cpp
    ├── mainwindow.cpp
    ├── mainwindow.ui
    └── my_qlabel.cpp

5 directories, 40 files

This was the structure of my one of the qt with ros project.

Do we have to create other folders and files manually?

Yes, You have to create it manually which is required for ros support. For example, Package.xml and Cmake.txt

What about .ui file?

You can add it anywhere you like, Just make sure to add a path to Cmake.txt

set(QT_SOURCES
#Main Window
    src/main.cpp
    src/mainwindow.cpp
    src/my_qlabel.cpp
    src/clickable.cpp
    src/clickablelabel.cpp
    # Header file paths
    include/ai_robotic_auto_annotation/mainwindow.h
    include/ai_robotic_auto_annotation/my_qlabel.h
    include/ai_robotic_auto_annotation/clickablelabel.h
    include/ai_robotic_auto_annotation/clickable.h
    # UI path
    src/mainwindow.ui     
    )

For the Resource file, if you have any:

find_package(Qt5 COMPONENTS Core Gui Widgets)
file(GLOB QT_RESOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} resources.qrc)
QT5_ADD_RESOURCES(QT_RESOURCES_CPP ${QT_RESOURCES})

I think it should have been created by the Qt Creator, What am I missing?

You need to make sure that you are including each file's path in your Cmake.txt, I think that configuration is important. And You are also adding dependices to the package.xml file.

  <build_depend>qt_build</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>libqt4-dev</build_depend>
  <exec_depend>qt_build</exec_depend>
  <exec_depend>roscpp</exec_depend>
  <exec_depend>libqt4-dev</exec_depend>

I hope this helps, if you have any issues feel free to drop a comment. If you want QT -> ROS using noetic then Tell me I will upload it for you.

edit flag offensive delete link more

Comments

I have generated required files and folders and added them in CMakeLists.txt. Apart from these files Qt creates a ui_<name>.h file, in your case it might be ui_mainwindow.h. In Qt this file is auto generated in build folder on every build, how to generate it in case of ROS environment?

SurajJadhav gravatar image SurajJadhav  ( 2021-09-09 09:41:49 -0500 )edit

It is just a QT header file. You can create this file like you are creating a text file. The catch is that you have to give an extension as .h that's it.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-09-09 09:45:02 -0500 )edit

I have added the file. But I was not aware of the content it should have so I created an empty project in Qt and opened it's ui_mainwindow.h. It has following comments:
/*************************
*
Form generated from reading UI file 'mainwindow.ui'
*
*
Created by: Qt User Interface Compiler version 5.13.2
*
*
WARNING! All changes made in this file will be lost when recompiling UI file!
**************************/
After seeing this I m not able t understand how to generate this file during each compilation?

SurajJadhav gravatar image SurajJadhav  ( 2021-09-09 10:25:31 -0500 )edit

Yes, in this the basic click events, your class variables and functions are defined. Take a look at a small example provided by QT official website. about the importance of Headers files and link between UI, Header file and cpp file.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-09-09 10:46:05 -0500 )edit

I went through the link which informs about precompiled headers and I don't understand how it is related to the current problem of generating ui_mainwindow.h in every build.
As quoted in the page:
The precompiled header must contain code which is stable and static throughout your project.
Whereas the file I m talking about should be updated after every time I change something in UI design.

SurajJadhav gravatar image SurajJadhav  ( 2021-09-09 11:32:01 -0500 )edit

Finally I found how to auto generate the ui_mainwindow.h, I have to set CMAKE_AUTOUIC flag to ON in CMakeLists.txt. This automatically generates the file in build folder.
More information is given in this official page.

Thank you @Ranjit Kathiriya for your instant replies.

SurajJadhav gravatar image SurajJadhav  ( 2021-09-09 12:01:30 -0500 )edit

Another question remains:
If I create all files and leave them blank, the Qt Creator doesn't accept them. For example I created mainwindow.ui manually and it was blank, when I opened it in Qt Creator, the IDE displayed an error:
An error has occurred while reading the UI file at line 1, column 0: Premature end of document.

To resolve this I created an empty project in Qt creator and copied the contents of that ui file to mainwindow.ui. After that the error was not seen and I was able to add widgets and other items in UI.
@Ranjit Kathiriya I think mainwindow.ui should also be auto generated.

SurajJadhav gravatar image SurajJadhav  ( 2021-09-09 12:11:50 -0500 )edit

If I create all files and leave them blank, the Qt Creator doesn't accept them. For example I created mainwindow.ui

If you are creating Ui then you will get a blank UI.

An error has occurred while reading the UI file at line 1, column 0: Premature end of document.

I think this is an issue of your layout, what I think you may have left horizontal, vertical, or any sub layout that may cause the error.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-09-10 02:04:34 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-09-08 07:06:39 -0500

Seen: 439 times

Last updated: Sep 09 '21