本文首發於我的博客kezunlin.me/post/91842b…,歡迎閱讀!linux
Part-1: Install and Configure Qt5 on Ubuntu 16.04git
sudo apt-get purge qt5-default qtcreator
sudo apt-get purge qt4-designer qt4-dev-tools複製代碼
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:ubuntu
sudo apt-get install build-essential libgl1-mesa-dev複製代碼
Download qt and install.vim
sudo apt-get install build-essential libgl1-mesa-dev
wget 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
windows
cd /opt
sudo ln -s /opt/Qt5.7.0 qt複製代碼
vim .bashrc複製代碼
# added by kzl
export PATH=/opt/cuda-8.0/bin:$PATH
export PATH=/opt/MATLAB/R2016b/bin:$PATH
export PATH=/opt/qt/5.7/gcc_64/bin:$PATH複製代碼
cd /etc/ld.so.conf.d
sudo vim qt.conf複製代碼
/opt/qt/5.7/gcc_64/lib複製代碼
sudo ldconfig複製代碼
or only install qtcreator (find_package error for `qt5x11extra-config.cmake`)bash
sudo apt-get install qt5-default qtcreator
sudo apt-get install qt4-designer qt4-dev-tools
複製代碼
cat /usr/share/qtchooser/qt57.conf 複製代碼
/opt/qt/5.7/gcc_64/bin
/opt/qt/5.7/gcc_64/lib複製代碼
# link to qt57
cd /usr/lib/x86_64-linux-gnu/qtchooser
sudo ln -s /usr/share/qtchooser/qt57.conf qt57.conf複製代碼
cd /usr/lib/x86_64-linux-gnu/qt-default/qtchooser複製代碼
sudo rm default.conf
sudo ln -s /usr/share/qtchooser/qt57.conf default.conf
ls -l default.conf
default.conf -> /usr/share/qtchooser/qt57.conf複製代碼
/usr/share/qtchooser/
app
/usr/lib/x86_64-linux-gnu/qtchooser/
less
/usr/lib/x86_64-linux-gnu/qt-default/qtchooser/
ide
which qtchooser
/usr/bin/qtchooser複製代碼
qtchooser -l複製代碼
4
5
default
qt4-x86_64-linux-gnu
qt4
qt5-x86_64-linux-gnu
qt5複製代碼
qtchooser -print-env
QT_SELECT="default"
QTTOOLDIR="/opt/qt/5.7/gcc_64/bin"
QTLIBDIR="/opt/qt/5.7/gcc_64/lib"
複製代碼
vim .bashrc
export QT_QPA_PLATFORM_PLUGIN_PATH=/opt/qt/5.7/gcc_64/plugins/platforms複製代碼
otherwise,errors may occur.svg
Failed to load platform plugin "xcb".
sudo apt-get -y install qtcreator複製代碼
qtcreator -version複製代碼
Qt Creator 3.5.1 based on Qt 5.5.1複製代碼
Start qtcreator
and create a Qt Widget Application
named hello
.
add resource fileAdd 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
複製代碼
steps:
mainwindow.ui ---> choose button ---> right click ---> Go to slot... ---> choose clicked() ---> OK複製代碼
will add slots in mainwindow.h
automatically
private slots:
void on_pushButtonOK_clicked();複製代碼
and in mainwindow.cpp
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.
add slots in mainwindow.h
by hand
private slots:
void pushButtonCancel_clicked();複製代碼
and in mainwindow.cpp
void MainWindow::pushButtonCancel_clicked()
{複製代碼
}複製代碼
connect button with slot in mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);複製代碼
// connect
connect(ui->pushButtonCancel, SIGNAL(clicked()), this, SLOT(pushButtonCancel_clicked()));
}
複製代碼
folder structure like this:
$ 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複製代碼
#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(); // method1
void pushButtonCancel_clicked(); // method2
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H複製代碼
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// connect
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)") );
}複製代碼
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}複製代碼
cmake_minimum_required(VERSION 2.8.8)
project(helloworld)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# ${QT_INCLUDE_DIRS} ${QT_LIBRARIES} and so on are all Qt4 Macro Definitions!!!!!
# Find the QtWidgets library
find_package(Qt5Core)
find_package(Qt5Widgets)
find_package(Qt5Gui)
find_package(Qt5OpenGL)
find_package(Qt5Xml)
#message( [qt] ${Qt5Core_INCLUDE_DIRS} )
#message( [qt] ${Qt5Core_LIBRARIES} )
#message( [qt] ${Qt5Widgets_INCLUDE_DIRS} )
#message( [qt] ${Qt5Widgets_LIBRARIES} )
# cpp files
aux_source_directory(. SRC_LIST)
# ui files
qt5_wrap_ui(ui_FILES mainwindow.ui)
# resource files
qt5_add_resources(qrc_FILES resource.qrc)
message( [Main] ${SRC_LIST} ) # ./main.cpp./mainwindow.cpp
message( [Main] ${ui_FILES} ) # build/ui_mainwindow.h
message( [Main] ${qrc_FILES} )# build/qrc_resource.cpp
# Tell CMake to create the helloworld executable
add_executable(${PROJECT_NAME} ${SRC_LIST} ${ui_FILES} ${qrc_FILES})
qt5_use_modules(${PROJECT_NAME} Core Widgets OpenGL Xml Gui)
# Use the Widgets module from Qt 5.
#qt5_use_modules(helloworld Widgets)
# link other libraries
#target_link_libraries (${PROJECT_NAME} ${SPEC_OPENCV_LIBS})複製代碼
cmake_minimum_required(VERSION 3.0)
set(PROJECT_NAME demo)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin) # bin/
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_PREFIX_PATH ${QTDIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
# QT5_DIR /opt/Qt5.7.0/5.7/gcc_64/lib/cmake/Qt5
find_package(Qt5 REQUIRED Widgets Core Gui Network OpenGL)
find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc features2d calib3d )
find_package(Protobuf REQUIRED) # 3.6.1
find_package(VTK REQUIRED) # 8.1.2
include(${VTK_USE_FILE})
MESSAGE( [Main] " VTK_INCLUDE_DIRS = ${VTK_INCLUDE_DIRS}")
MESSAGE( [Main] " VTK_LIBRARIES = ${VTK_LIBRARIES}")
find_package(PCL REQUIRED) # 1.9.1
#find_package(PCL REQUIRED COMPONENTS common io filters visualization)
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(
./ # current folder
# ${GFLAGS_INCLUDE_DIRS}
# ${GLOG_INCLUDE_DIRS}
# ${GTEST_INCLUDE_DIRS}
${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
)複製代碼
cd hello
mkdir build
cd build
cmake ..
make複製代碼
./helloworld複製代碼
screen snapshot like this: