Series
Tutorial
qt: 5.7.0
qmake: 3.0 based on Qt 5.7.0
qtcreator: 3.5.1 based on Qt 5.5.1
purge existing qt 1 2 sudo apt-get purge qt5-default qtcreator sudo apt-get purge qt4-designer qt4-dev-tools
install qt57 In addition, building graphical Qt applications requires OpenGL libraries and headers installed. On Ubuntu and other Debian-based Linux systems you can get OpenGL and the minimal set of development tools by installing the packages libgl1-mesa-dev and build-essential , i.e. by running this command:
1 sudo apt-get install build-essential libgl1-mesa-dev
Download qt and install.
1 2 3 4 sudo apt-get install build-essential libgl1-mesa-devwget http://download.qt.io/official_releases/qt/5.7/5.7.0/qt-opensource-linux-x64-5.7.0.run chmod +x qt-opensource-linux-x64-5.7.0.run./qt-opensource-linux-x64-5.7.0.run
install to /opt/Qt5.7.0
create links to /opt/qt/
1 2 cd /optsudo ln -s /opt/Qt5.7.0 qt
qtchooser add qt57.conf 1 2 3 4 5 6 7 8 cat /usr/share/qtchooser/qt57.conf /opt/qt/5.7/gcc_64/bin /opt/qt/5.7/gcc_64/lib cd /usr/lib/x86_64-linux-gnu/qtchoosersudo ln -s /usr/share/qtchooser/qt57.conf qt57.conf
link to default 1 2 3 4 5 6 cd /usr/lib/x86_64-linux-gnu/qt-default/qtchoosersudo rm default.confsudo ln -s /usr/share/qtchooser/qt57.conf default.confls -l default.confdefault.conf -> /usr/share/qtchooser/qt57.conf
qt conf /usr/share/qtchooser/
qt4-x86_64-linux-gnu.conf (FILE)
qt5-x86_64-linux-gnu.conf (FILE)
qt57.conf (FILE)
/usr/lib/x86_64-linux-gnu/qtchooser/
4.conf (link)
5.conf (link)
qt4.conf (link)
qt5.conf (link)
qt57.conf (link)
/usr/lib/x86_64-linux-gnu/qt-default/qtchooser/
default.conf (link) qt57.conf
qtchooser 1 2 3 4 5 6 7 8 9 10 11 12 which qtchooser /usr/bin/qtchooser qtchooser -l 4 5 default qt4-x86_64-linux-gnu qt4 qt5-x86_64-linux-gnu qt5
qtchooser env 1 2 3 4 qtchooser -print-env QT_SELECT="default" QTTOOLDIR="/opt/qt/5.7/gcc_64/bin" QTLIBDIR="/opt/qt/5.7/gcc_64/lib"
1 2 vim .bashrc export QT_QPA_PLATFORM_PLUGIN_PATH=/opt/qt/5.7/gcc_64/plugins/platforms
otherwise,errors may occur. Failed to load platform plugin "xcb".
QtCreator Tutorial install qtcreator 1 2 3 4 sudo apt-get -y install qtcreatorqtcreator -version Qt Creator 3.5.1 based on Qt 5.5.1
create application
Start qtcreator
and create a Qt Widget Application
named hello
.
Add a resource file named resource.qrc
.
hello-->right click--> Add New... --->qt---> qt resource file---> name resource---> generate resource.qrc
and then import images to resource.qrc
Resources | resource.qrt--->right click---> Add Prefix...---> name /prefix
Resources | resource.qrt | /prefix--->right click ---> Add Existing Files... ---> choose images ---> OK
use resource in mainwindow.ui
button ---> property page ---> icon ---> Choose Resource... ---> select image ---> OK
qt slots method 1 steps:
mainwindow.ui ---> choose button ---> right click ---> Go to slot... ---> choose clicked() ---> OK
will add slots in mainwindow.h
automatically
1 2 private slots: void on_pushButtonOK_clicked () ;
and in mainwindow.cpp
1 2 3 4 void MainWindow::on_pushButtonOK_clicked () { }
Tips: if we use on_pushButtonOK_clicked
style, there is no need to connect with slots in MainWindow
constructor by hand.
method 2 (OK) add slots in mainwindow.h
by hand
1 2 private slots: void pushButtonCancel_clicked () ;
and in mainwindow.cpp
1 2 3 4 void MainWindow::pushButtonCancel_clicked () { }
connect button with slot in mainwindow.cpp
1 2 3 4 5 6 7 8 9 MainWindow::MainWindow (QWidget *parent) : QMainWindow (parent), ui (new Ui::MainWindow) { ui->setupUi (this ); connect (ui->pushButtonCancel, SIGNAL (clicked ()), this , SLOT (pushButtonCancel_clicked ())); }
Code Example folder folder structure like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 $ tree hello/ hello/ ├── CMakeLists.txt ├── hello.pro ├── hello.pro.user ├── images │ ├── kezunlin_logo.png │ ├── logo.svg │ └── searchicon.png ├── main.cpp ├── mainwindow.cpp ├── mainwindow.h ├── mainwindow.ui └── resource.qrc 1 directory, 11 files
code mainwinow.h 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui {class MainWindow ;} class MainWindow : public QMainWindow{ Q_OBJECT public : explicit MainWindow (QWidget *parent = 0 ) ; ~MainWindow (); private slots: void on_pushButtonOK_clicked () ; void pushButtonCancel_clicked () ; private : Ui::MainWindow *ui; }; #endif
mainwindow.cpp 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow (QWidget *parent) : QMainWindow (parent), ui (new Ui::MainWindow) { ui->setupUi (this ); connect (ui->pushButtonCancel, SIGNAL (clicked ()), this , SLOT (pushButtonCancel_clicked ())); } MainWindow::~MainWindow () { delete ui; } void MainWindow::on_pushButtonOK_clicked () { QString text = ui->lineEditName->text (); QMessageBox::information (this , "OK" , text); ui->pushButtonOK->setText ( tr ("(OK. click me)" ) ); } void MainWindow::pushButtonCancel_clicked () { QString text = ui->lineEditName->text (); QMessageBox::information (this , "Cancel" , text); ui->pushButtonCancel->setText ( tr ("(Cancel.click me)" ) ); }
main.cpp 1 2 3 4 5 6 7 8 9 10 11 #include "mainwindow.h" #include <QApplication> int main (int argc, char *argv[]) { QApplication a (argc, argv) ; MainWindow w; w.show (); return a.exec (); }
CMakeLists.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 cmake_minimum_required (VERSION 2.8 .8 )project (helloworld)set (CMAKE_INCLUDE_CURRENT_DIR ON )set (CMAKE_AUTOMOC ON )find_package (Qt5Core)find_package (Qt5Widgets)find_package (Qt5Gui)find_package (Qt5OpenGL)find_package (Qt5Xml)aux_source_directory (. SRC_LIST)qt5_wrap_ui(ui_FILES mainwindow.ui) qt5_add_resources(qrc_FILES resource.qrc) message ( [Main] ${SRC_LIST} ) message ( [Main] ${ui_FILES} ) message ( [Main] ${qrc_FILES} )add_executable (${PROJECT_NAME} ${SRC_LIST} ${ui_FILES} ${qrc_FILES} )qt5_use_modules (${PROJECT_NAME} Core Widgets OpenGL Xml Gui)
CMakeLists.txt 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 cmake_minimum_required (VERSION 3.0 )set (PROJECT_NAME demo)set (CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR} /bin) set (EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR} )set (LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR} )set (CMAKE_CXX_STANDARD 11 )set (CMAKE_AUTOMOC ON )set (CMAKE_AUTOUIC ON )set (CMAKE_AUTORCC ON )find_package (Qt5 REQUIRED Widgets Core Gui Network OpenGL)find_package (OpenCV REQUIRED COMPONENTS core highgui imgproc features2d calib3d )find_package (Protobuf REQUIRED) find_package (VTK REQUIRED) include (${VTK_USE_FILE} )MESSAGE ( [Main] " VTK_INCLUDE_DIRS = ${VTK_INCLUDE_DIRS}" )MESSAGE ( [Main] " VTK_LIBRARIES = ${VTK_LIBRARIES}" )find_package (PCL REQUIRED) link_directories (${PCL_LIBRARY_DIRS} )add_definitions (${PCL_DEFINITIONS} )message ([main] "PCL_DIR = ${PCL_DIR}" )message ([main] "PCL_FOUND = ${PCL_FOUND}" )message ([main] "PCL_INCLUDE_DIRS = ${PCL_INCLUDE_DIRS}" )message ([main] "PCL_LIBRARIES = ${PCL_LIBRARIES}" )message ([main] "PCL_LIBRARY_DIRS = ${PCL_LIBRARY_DIRS}" )message ([main] "PCL_COMMON_LIBRARIES = ${PCL_COMMON_LIBRARIES}" )message ([main] "PCL_IO_LIBRARIES = ${PCL_IO_LIBRARIES}" )message ([main] "PCL_FILTERS_LIBRARIES = ${PCL_FILTERS_LIBRARIES}" )message ([main] "PCL_VISUALIZATION_LIBRARIES = ${PCL_VISUALIZATION_LIBRARIES}" )include_directories ( ./ ${PROTOBUF_INCLUDE_DIRS} ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${VTK_INCLUDE_DIRS} ${PCL_INCLUDE_DIRS} ) set (SOURCE_FILES main.cpp MainWindow.cpp ./proto/camera_image.pb.cc ./proto/point_cloud.pb.cc ) set (RESOURCE_FILE resource.qrc)add_executable (${PROJECT_NAME} ${SOURCE_FILES} ${RESOURCE_FILE} )target_link_libraries (${PROJECT_NAME} Qt5::Widgets Qt5::Gui Qt5::Core Qt5::OpenGL Qt5::Network ${Boost_LIBRARIES} ${PROTOBUF_LIBRARIES} ${OpenCV_LIBRARIES} ${VTK_LIBRARIES} ${PCL_LIBRARIES} pthread rt )
cmake 1 2 3 4 5 cd hellomkdir buildcd buildcmake .. make
run
screen snapshot like this:
Reference
History