Results 1 to 4 of 4

Thread: Using OpenCV with QT creator

  1. #1
    Join Date
    Mar 2006
    Location
    Chicago
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using OpenCV with QT creator

    I am trying to create a project using OpenCV with QT for the user interface but I am having some problems.

    I am using QT creator 2.0.0 on Ubuntu 10.4 64 bit with OpenCV 2.1.0.

    I have each component installed and working independantly, but I can't seem to get them to play nice together.

    For instance: using CMake and the following CMakeLists.txt file:

    Qt Code:
    1. PROJECT( facedetect_proj )
    2. FIND_PACKAGE( OpenCV REQUIRED )
    3. ADD_EXECUTABLE( facedetect facedetect.cpp )
    4. TARGET_LINK_LIBRARIES( facedetect ${OpenCV_LIBS} )
    To copy to clipboard, switch view to plain text mode 

    Creator creates a project that compiles and runs just fine, but does not create a .pro file.

    When I create just a QT project, it compiles just fine and seems to find the OpenCV libraries just fine in the IDE but then does not build properly. I get "undefined reference" errors for every function call using OpenCV. I am pretty sure that I need to include more in the .pro file for this to work but I am not sure how to do it or what to include. Please help me! Thanks!

  2. #2
    Join Date
    Mar 2006
    Location
    Chicago
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using OpenCV with QT creator

    OK, so I've done some more research on CMake. Apparently CMakeLists.txt replaces the .pro file altogether and when you use a cmake file, you run cmake to run the MOC instead of QMake. I am still going through the CMake documentation to figure out how to do this. I will post when I figure it out. Also, I can't find the button to edit my posts. Oh well.

  3. #3
    Join Date
    Mar 2006
    Location
    Chicago
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using OpenCV with QT creator

    I have gotten things to build, but now I am having linker errors. Here is my CMakeLists.txt file:

    Qt Code:
    1. PROJECT( detect_proj )
    2. CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)
    3.  
    4.  
    5. FIND_PACKAGE( Qt4 REQUIRED )
    6. INCLUDE_DIRECTORIES( ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
    7.  
    8. FIND_PACKAGE( OpenCV REQUIRED )
    9.  
    10. #set up optional include packages here:
    11. SET( QT_USE_OPENGL TRUE )
    12.  
    13. # .cxx sources
    14. SET( DETECT_SRCS_CXX main.cpp myclass.cpp mainwindow.cpp)
    15.  
    16. # files which need to be moc'd by Qt
    17. SET( DETECT_MOC_SRCS myclass.h mainwindow.h )
    18.  
    19. SET( DETECT_UI mainwindow.ui )
    20.  
    21. # build ui_XXX files from the XML-style .ui files
    22. QT4_WRAP_UI( DETECT_SRCS_CXX ${DETECT_UI})
    23.  
    24. # this moc's the above variable and appends to the cxx sources
    25. QT4_WRAP_CPP( DETECT_SRCS_CXX ${DETECT_MOC_SRCS})
    26.  
    27. ADD_EXECUTABLE( detect ${DETECT_SRCS_CXX} ${DETECT_MOC_SRCS} ${DETECT_UI} )
    28.  
    29. TARGET_LINK_LIBRARIES( detect ${OpenCV_LIBS} ${QT_LIBRARIES} )
    To copy to clipboard, switch view to plain text mode 

    When I compile, I get undefined reference to QApplication, QObject, and pretty much all the other QT classes. What am I doing wrong?

    -Greg

  4. #4
    Join Date
    Mar 2006
    Location
    Chicago
    Posts
    8
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using OpenCV with QT creator [SOLVED]

    I have solved my issue. Apparently when you install Qt through the .bin file they have on the site it does not change your path, which cmake relies on but QtCreator does not. To do this, edit your bash.bashrc file with superuser permission and add the following lines to the end of the file:

    Qt Code:
    1. export QTDIR=/opt/qtsdk-2010.04/qt
    2. export PATH=$QTDIR/bin:$PATH
    3. export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
    To copy to clipboard, switch view to plain text mode 

    If you use gnome, use: sudo gedit /etc/bash.bashrc, for KDE I think you would use: sudo kate /etc/bash.bashrc
    Also you may have to modify line 1 to match your own QT installation directory.

    Modify the following file for your CMakeLists.txt project:

    Qt Code:
    1. # You must set the minimum version for CMake
    2. CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
    3.  
    4. # This is the name of your project:
    5. PROJECT(ObjDetect_proj)
    6.  
    7. SET(CMAKE_BUILD_TYPE Release)
    8. SET(CMAKE_CXX_FLAGS "-Wall")
    9.  
    10. # Use this for additional packages you need support for. In this case, I am using OpenCV.
    11. # Consult CMake documentation to see which packages are supported and what the command is.
    12. FIND_PACKAGE( OpenCV REQUIRED )
    13.  
    14. # QT4 Handling
    15. FIND_PACKAGE(Qt4 REQUIRED)
    16. INCLUDE_DIRECTORIES( ${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR})
    17.  
    18. INCLUDE(${QT_USE_FILE})
    19.  
    20. # Change these to all your header files
    21. SET( HWQ_Qt4_SRC
    22. src/MainWindow.h
    23. src/ObjDetect.h
    24. )
    25. # Change this to all your .ui designer files
    26. SET( HWQ_Qt4_UI
    27. src/MainWindow.ui
    28. )
    29. # Add resource files if you have them.
    30. SET( HWQ_Qt4_RES
    31. )
    32.  
    33. # This runs the QT MOC
    34. QT4_WRAP_CPP(HWQ_MOC_CPP ${HWQ_Qt4_SRC})
    35. # This creates your ui_whatever.h files
    36. QT4_WRAP_UI(HWQ_UI_CPP ${HWQ_Qt4_UI})
    37. QT4_ADD_RESOURCES(HWQ_RES_H ${HWQ_Qt4_RES})
    38.  
    39. INCLUDE_DIRECTORIES( . )
    40.  
    41. # Put your cpp files here
    42. SET( HWQ_SRC
    43. src/main.cpp
    44. src/MainWindow.cpp
    45. src/ObjDetect.cpp
    46. ${HWQ_MOC_CPP}
    47. ${HWQ_UI_CPP}
    48. ${HWQ_RES_H}
    49. )
    50.  
    51. SET( HWQ_LIB
    52. ${QT_LIBRARIES}
    53. )
    54.  
    55. ADD_EXECUTABLE(ObjDetect ${HWQ_SRC} )
    56. TARGET_LINK_LIBRARIES(ObjDetect ${HWQ_LIB} ${OpenCV_LIBS} )
    57.  
    58. # I think this line only gets called when you run 'make install'
    59. INSTALL_TARGETS( /bin Objdetect)
    To copy to clipboard, switch view to plain text mode 

    Now I don't know why exactly, but when you run CMake from Qt Creator, it screws up. You have to go to the command line first and run
    Qt Code:
    1. cmake .
    To copy to clipboard, switch view to plain text mode 
    After you run this initially, you can open the CMakeLists.txt file with QtCreator and it will open your whole project and build and link correctly.

    I hope this helps some other poor sucker trying to figure this stuff out.

    -Greg

  5. The following user says thank you to gmiller39 for this useful post:

    fezvez (8th July 2010)

Similar Threads

  1. OpenCV + QT4
    By thereisnoknife in forum Qt Programming
    Replies: 6
    Last Post: 8th March 2013, 05:19
  2. opencv-qt-mac
    By ireneluis in forum Qt Programming
    Replies: 0
    Last Post: 16th March 2010, 15:24
  3. Qt and OpenCV
    By malorie in forum Newbie
    Replies: 2
    Last Post: 7th March 2010, 14:57
  4. OpenCv & Qt4
    By switch in forum Qt Programming
    Replies: 0
    Last Post: 4th August 2009, 15:12
  5. OpenCv + Qt
    By Janderson Borges in forum Qt Programming
    Replies: 3
    Last Post: 2nd December 2008, 13:01

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.