code-prettify

顯示具有 Qt 標籤的文章。 顯示所有文章
顯示具有 Qt 標籤的文章。 顯示所有文章

2016年10月15日 星期六

gitignore 範本及多個 gitignore 檔案實踐方法

Git 用了一段時間,常常遇到要調整 .gitignore 忽略自動產生的檔案,
當專案多了之後就會覺得不知道有沒有什麼範本可以使用,
馬上發現 GitHub 有個 gitignore 專案專門收集各種 .gitignore 範本。
https://github.com/github/gitignore

接下來想到的問題是,雖然一開始可以用範本,但是之後修修改改又不一致了?
有沒有什麼好的方法呢?直覺是能不能分開成多個 gitignore 檔案呢,
可惜,Git 不支援也不建議。
還好在 StackOverFlow 有網友提出合併檔案的方法,這樣就可以實踐了!

在 git 根目錄下建立一個 gitignore 目錄,然後將範本放進去,像是 VisualStudio.gitignore、C++.gitignore、Qt.gitignore
接著將專案的設定放在 Project.gitignore,最後用一個批次檔來產生 .gitignore。

cat Project.gitignore VisualStudio.gitignore C++.gitignore Qt.gitignore > ../.gitignore

這麼一來 gitignore 既方便使用也好維護 !



資料來源:
gitignore - A collection of useful .gitignore templates
https://github.com/github/gitignore

Best practice for using multiple .gitignore files
http://stackoverflow.com/questions/10274424/best-practice-for-using-multiple-gitignore-files

2016年8月29日 星期一

Install Qt5.7 binary on Ubuntu 16.04

Use Ubuntu Server 16.04 for Example

For C++ need build-essential, OpenGL need libgl1-mesa-dev
and install Qt 5.7 binary from PPA

sudo add-apt-repository -y ppa:beineri/opt-qt57-xenial
sudo apt update
sudo apt install -y build-essential libgl1-mesa-dev qt-latest
. "/opt/qt57/bin/qt57-env.sh"

For env setting, add bashrc command
echo . "/opt/qt57/bin/qt57-env.sh" >> .bashrc

=== Ref
Install Qt 5 on Ubuntu
https://wiki.qt.io/Install_Qt_5_on_Ubuntu

Stephan Binner
https://launchpad.net/~beineri

2016年5月19日 星期四

Linux auto build project when file modify

Arch auto build qt project when file modify use inotify-tools

#install inotify tools
pacman -S inotify-tools

# write a monitor script
autobuild.sh

=== Refference ===
Github - inotify-tools
https://github.com/rvoicilas/inotify-tools/wiki

inotifywait(1) - Linux man page
http://linux.die.net/man/1/inotifywait






2015年10月4日 星期日

Qt 如何將 UTF-8 檔案轉存為 UTF-16

之前的文章介紹如何讀取 UTF-8 檔案。
Qt 如何從檔案讀取 UTF-8 的中文
這次介紹如何將 UTF-8 檔案轉存為 UTF-16。

使用 QFile 開啟檔案,
使用 QTextStream 處理編碼,

=== 完整範例
QFile fileIn("input.txt");
if (!fileIn.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "fileIn opne fail.";
return;
}

QFile fileOut("output.txt");
if (!fileOut.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "fileOut opne fail.";
return;
}

QTextStream in(&fileIn);
in.setCodec("UTF-8");
QTextStream out(&fileOut);
out.setCodec("UTF-16");

while (!in.atEnd()) {
QString line = in.readLine();
out << changeText(line);
}

fileIn.close();
fileOut.close();

qDebug() << "done";

=== 資料來源
Qt Documentation - QTextStream Class
http://doc.qt.io/qt-5/qtextstream.html

2015年10月1日 星期四

Qt PostgreSQL 範例

Qt PostgreSQL 範例

=== 測試環境
OS: Windows 7
Qt Verstion: 5.5.0
Compiler: Visual Studio 2013

OS: Arch Linux 4.1.6
Qt Version: 5.5.0
Compiler: GCC 5.2.0

=== 程式範例
=== 錯誤訊息
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7

在 Windows,確認下列檔案
MyAppDir\sqldrivers\qsqlpsql.dll
MyAppDir\libpq.dll
MyAppDir\libeay32.dll
MyAppDir\ssleay32.dll

在 linux,確認 qt project file 加上
INCLUDEPATH+=/usr/include/pgsql
LIBS+=-L/usr/lib -lpq

=== 資料來源
Qt Documentation - SQL Database Drivers
http://doc.qt.io/qt-5/sql-driver.html

Qt: SELECT-Query to PostgreSQL-Server always responses with NULL
http://stackoverflow.com/questions/13633055/qt-select-query-to-postgresql-server-always-responses-with-null

QT5: Failed to load psql driver in windows
http://stackoverflow.com/questions/20884010/qt5-failed-to-load-psql-driver-in-windows

2015年9月24日 星期四

Qt 如何從檔案讀取 UTF-8 的中文

首先開啟檔案,
然後使用 QTextStream,
記得使用 setCodec 設定編碼。

程式範例:
QFile file("utf8");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;

QTextStream in(&file);
in.setCodec("UTF-8");
while (!in.atEnd()) {
QString line = in.readLine();
QTextStream qtout(stdout);
qtout << line << "\n";
}

資料來源:
http://stackoverflow.com/questions/5630114/how-to-read-utf-8-text-from-file-using-qt

2015年9月23日 星期三

Qt - 使用 qrand() 產生亂數 rand

Qt - 使用 qrand() 產生亂數 rand

max 最大值
min 最小值

qrand() % ((max + 1) - min) + min;

產生出來的亂數包含 max 及 min 兩者。

如果連續執行多次程式,會發現每次產生的亂數都是一樣的,
因為電腦產生的亂數,其實是偽亂數,所以想要讓每次的亂數不同,
還需要執行 qsrand 來設定亂數種子。

備註:使用 QTime::currentTime().msec() 現在時間當作種子來設定
備註:使用 QElapsedTimer 取得小於 ms 的時間刻度

資料來源:
Qt Documentation - <QtGlobal> - Global Qt Declarations
http://doc.qt.io/qt-5/qtglobal.html#qrand

How to generate random number between two numbers? Qt
http://qt-project.org/forums/viewthread/24262

Use of qsrand, random method that is not random
http://stackoverflow.com/questions/2767383/use-of-qsrand-random-method-that-is-not-random

Qt: Fast way to measure time?
http://stackoverflow.com/questions/244646/qt-fast-way-to-measure-time

Qt - 建立一個 Console 應用程式

想要用 Qt 建立一個 Console 應用程式,
只要像下列的範例就可以建立。

#include <QtCore/QCoreApplication>
#include "Foo/Foo.h"

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

Foo foo;

return app.exec();
}

資料來源:
How do I create a simple Qt console application in C++?
http://stackoverflow.com/questions/4180394/how-do-i-create-a-simple-qt-console-application-in-c

2015年7月15日 星期三

Qt WebSocket

宣告 WebSocketServer
new QWebSocketServer("Web Server", QWebSocketServer::NonSecureMode, this)

使用 listen() 開始接受 client 連線

同樣是使用 newConnection() 接受新的 client 事件
使用 nextPendingConnection() 取得 client 的 socket

QWebSocket 使用 disconnected 斷線事件

因為 WebSocket 規範中有兩種資料傳送方式,純文字模式及二進制模式,
所以資料接收事件也分為兩種,分別為
textMessageReceived,參數為 QString message
binaryMessageReceived,參數為 QByteArray message

傳送資料也分為兩個函式,分別為
sendTextMessage
sendBinaryMessage

簡單範例

connect(m_pWebSocketServer, &QWebSocketServer::newConnection, [=]() {
qDebug() << "new client socket";

QWebSocket* socket = this->m_pWebSocketServer->nextPendingConnection();

connect(socket, &QWebSocket::disconnected, [=]() {
qDebug() << "client socket disconnected";

socket->deleteLater();
});

connect(socket, &QWebSocket::textMessageReceived, [=](QString message) {
qDebug() << "client textMessageReceived";

qDebug() << "Read: " << message;
qDebug() << "Send " << message;

socket->sendTextMessage(message);
socket->close();
});

connect(socket, &QWebSocket::binaryMessageReceived, [=](QByteArray message) {
qDebug() << "client processBinaryMessage";

qDebug() << "Read: " << message;
qDebug() << "Send " << message;

socket->sendBinaryMessage(message);
socket->close();
});
});

m_pWebSocketServer->listen(QHostAddress::Any, 8812);

資料來源
Echo Server Example
http://doc.qt.io/qt-5/qtwebsockets-echoserver-example.html

QWebSocketServer
http://doc.qt.io/qt-5/qwebsocketserver.html

2015年7月14日 星期二

Qt TcpSocket

Server 的部份使用 QTcpServer

呼叫 listen 函式開始接受 Client 連線,參數為 QHostAddress::Any, 8811

在 newConnection signal 接受 socket

QTcpSocket* socket = server->nextPendingConnection();

在 readyRead() signal 接收資料

QBtyeArray data = socket->readyAll()

使用 write() 傳送資料

簡單的範例:

  m_server = new QTcpServer();
connect(m_server, &QTcpServer::newConnection, [=]() { qDebug() << "new client socket"; QTcpSocket* socket = this->m_server->nextPendingConnection(); connect(socket, &QTcpSocket::disconnected, [=]() { qDebug() << "client socket disconnected"; socket->deleteLater(); }); connect(socket, &QTcpSocket::readyRead, [=]() { qDebug() << "client readyRead"; QString string = socket->readAll(); qDebug() << "Read: " << string; string = string.toUpper(); qDebug() << "Send " << string; socket->write(string.toLatin1()); socket->close(); }); }); m_server->listen(QHostAddress::Any, 8811); qDebug() << "server listen";


Client 直接使用 QTcpSocket

呼叮 connectToHost 與 server 連線
連上了會觸發 connected 訊號,斷線則是 disconnected
接收資料一樣是 readyRead

簡單的範例:

m_client = new QTcpSocket();

connect(m_client, &QTcpSocket::connected, [=]() {
qDebug() << "socket connected";

QString string = "Hi";
m_client->write(string.toLatin1());
qDebug() << "Send: " << string;
});

connect(m_client, &QTcpSocket::disconnected, [=]() {
qDebug() << "socket disconnected";
});

connect(m_client, &QTcpSocket::readyRead, [=]() {
qDebug() << "client readyRead";

QString string = m_client->readAll();
qDebug() << "Read: " << string;

m_client->close();
});

m_client->connectToHost("127.0.0.1", 8811);


資料來源:
Network Programming with Qt
http://doc.qt.io/qt-5/qtnetwork-programming.html

Sockets - Server & Client using QT
http://www.bogotobogo.com/cplusplus/sockets_server_client_QT.php

QTcpSocket Class
http://doc.qt.io/qt-5/qtcpsocket.html

2015年5月18日 星期一

Arch Linux C++ / Qt 開發

Arch Linux C++ / Qt 開發

要進行 C++ 開發,只要安裝基本開發套件
# pacman -S base-devel
使用 nano 新增 main.cpp 輸入下列文字:

#include <iosteam>

int main(int argc, char* argv[])
{
    std::cout << "Hello World!" << std::endl;

    return 0;
}

存檔後使用 g++ 編譯
# g++ main.cpp
編譯後會產生 a.out 檔案,執行
#./a.out
畫面上就會印出 Hello World! 文字

接著安裝 Qt 套件
# pacman -S qt
新增一個 qt 資料夾並進入
# mkdir qt
# cd qt
使用 nano 新增 main.cpp 輸入下列文字:

#include <QDebug>

int main(int argc, char* argv[])
{
    qDebug() << "Hello World!";

    return 0;
}

使用 qmake 產生專案檔
# qmake -project
使用 qmake 產生 Makefile
# qmake
使用 Makefile 編譯
# make
執行產生的 qt 檔案
# ./qt
畫面上會印出 Hello World! 的文字

相關文章:
Arch Linux 文章列表

資料來源:
Arch Linux
https://www.archlinux.org/

Arch Linux 臺灣社群
http://archlinux.tw/