2013年12月1日 星期日

Qt Widgets C++ building

refer to: 
https://www.cntofu.com/book/46/qt/ubuntubian_yi_an_zhuang_qt5__0__1.md
https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/617555
https://blog.51cto.com/u_15295315/2997065
https://www.cnblogs.com/fengyaoyao/p/10549320.html

Preinstall:
sudo add-apt-repository ppa:levi-armstrong/qt-libraries-xenial
sudo apt update
sudo apt install bison build-essential flex gperf libgstreamer1.0-dev \
libgstreamer-plugins-base1.0-dev libasound2-dev libatkmm-1.6-dev \
libbz2-dev libcap-dev libcups2-dev libdrm-dev \
libgl1-mesa-dev libglu1-mesa-dev libgl1-mesa-glx libfontconfig1-dev libfreetype6-dev \
libicu-dev libnss3-dev libpci-dev libpulse-dev libssl-dev libudev-dev \
libx11-dev libx11-xcb-dev libxcb-composite0 libxcb-composite0-dev libxcb-cursor-dev \
libxcb-cursor0 libxcb-damage0 libxcb-damage0-dev libxcb-dpms0 libxcb-dpms0-dev \
libxcb-dri2-0 libxcb-dri2-0-dev libxcb-dri3-0 libxcb-dri3-dev libxcb-ewmh-dev libxcb-ewmh2 \
libxcb-glx0 libxcb-glx0-dev libxcb-icccm4 libxcb-icccm4-dev libxcb-image0 libxcb-image0-dev \
libxcb-keysyms1 libxcb-keysyms1-dev libxcb-present-dev libxcb-present0 libxcb-randr0 \
libxcb-randr0-dev libxcb-record0 libxcb-record0-dev libxcb-render-util0 \
libxcb-render-util0-dev libxcb-render0 libxcb-render0-dev libxcb-res0 libxcb-res0-dev \
libxcb-screensaver0 libxcb-screensaver0-dev libxcb-shape0 libxcb-shape0-dev libxcb-shm0 \
libxcb-shm0-dev libxcb-sync-dev libxcb-sync-dev libxcb-sync1 libxcb-util-dev \
libxcb-util0-dev libxcb-util1 libxcb-xf86dri0 \
libxcb-xf86dri0-dev libxcb-xfixes0 libxcb-xfixes0-dev libxcb-xinerama0 \
libxcb-xinerama0-dev libxcb-xkb-dev libxcb-xkb1 \
libxcb-xtest0 libxcb-xtest0-dev libxcb-xv0 libxcb-xv0-dev libxcb-xvmc0 libxcb-xvmc0-dev \
libxcb1 libxcb1-dev libxcomposite-dev libxcursor-dev libxdamage-dev libxext-dev \
libxfixes-dev libxi-dev libxrandr-dev libxrender-dev libxslt1-dev libxss-dev \
libxtst-dev libgcrypt11-dev libsigc++-2.0-dev libgtk2.0-dev perl ruby
apt install qt5-default qt59creator
~~~run /opt/qt59/bin/qtcreator

1.
pkg-config --cflags egl
error message: package 'xfixes' requires 'fixesproto = 6.0' but version of fixesproto is 5.0
fix way:
wget https://xorg.freedesktop.org/archive/individual/proto/xorgproto-2022.2.tar.xz
tar -xvf ./xorgproto-2022.2.tar.xz
cd xorgproto-2022.2
./configure
make && make install

2.
CPU_NUM=$(grep -c processor /proc/cpuinfo)
echo "CPU number = "$CPU_NUM

wget https://download.qt.io/archive/qt/5.12/5.12.12/single/qt-everywhere-src-5.12.12.tar.xz
tar -xvf ./qt-everywhere-src-5.12.12.tar.xz
cd qt-everywhere-src-5.12.12

###cat /proc/cpuinfo, if you don't have avx avx2 avx512 remove -no-avx -no-avx2 -no-avx512
###make sure /usr/lib/x86_64-linux-gnu/libGL.so correct or Desktop OpenGL not found
./configure -prefix /opt/qt -release -force-debug-info -opensource -nomake tests -skip qtwebengine -no-avx -no-avx2 -no-avx512
make V=1 -j$CPU_NUM
make install

3.
mkdir -p helloworld
cd helloworld
gedit helloworld.cpp
~~~as follows~~~

#include "QtWidgets/qlabel.h"
#include "QtWidgets/qapplication.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);

    QLabel* label = new QLabel("<h1><font color=blue>Hello!World!</font><font color=red>Orz...</font></h1>");
    label->setWindowTitle("First Qt!");
    label->resize(200, 50);
    label->show();

    return app.exec();
}

4.
gedit Makefile
~~~~~~~~~~~
CXXFLAGS=-I/opt/qt/include -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -O3 -g -fPIC -pthread -fmessage-length=0 -std=c++14
LDFLAGS=-L/opt/qt/lib -Wl,-rpath,/opt/qt/lib -lQt5Widgets -lQt5Quick -lQt5Gui -lQt5Qml -lQt5Core -lGL -lpng -ljpeg -ldl -lrt -lpthread

OBJS=helloworld.o
TARGET=helloworld

$(TARGET): $(OBJS)
$(CXX) $(OBJS) $(LDFLAGS) -o $(TARGET)

all: $(TARGET)

clean:
rm -f $(OBJS) $(TARGET)

5.
make
./helloworld

6.
~~~advanced project
If header using Q_OBJECT must generate moc file
/opt/qt/bin/moc ./mainwindow.h -o ./moc_mainwindow.cpp
/usr/bin/uic ./mainwindow.ui -o ui_mainwindow.h

edit Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o

7. new creator version (option)
wget https://download.qt.io/archive/qtcreator/4.9/4.9.2/qt-creator-opensource-linux-x86_64-4.9.2.run
chmod 777 ./qt-creator-opensource-linux-x86_64-4.9.2.run
./qt-creator-opensource-linux-x86_64-4.9.2.run

select menu bar tool:
Tool -> Option -> Kits -> Qt Versions -> Manual: /opt/qt/bin/qmake
Tool -> Option -> Kits -> Kits -> Manual -> Compiler: 
C: GCC (C, x86 64bit in /usr/bin)
C++: GCC (C++, x86 64bit in /usr/bin)
select left side projects:
Projects -> Build ->shadow build (unchecked)

Demo source:
(a) check box + name + image using QListWidget QListWidgetItem QWidget
https://www.mediafire.com/file/tx8r9nypux0d8vz/qt_client_ui.tar.gz
(b) connect signals emit slot update QImage
https://www.mediafire.com/file/r5y160q5nwvo3gx/qt_creator_ui.tar.gz
(c) simple box
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz

沒有留言:

張貼留言