Change Screen Resolution in Windows using C++
主要使用 Windows API
ChangeDisplaySettings
及
EnumDisplaySettings
首先使用 EnumDisplaySettings 取得目前的螢幕設定值
然後修改解析度的寬高
最後使用 ChangeDisplaySettings 更新螢幕設定值
=== 程式範例
#include "windows.h"
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
if (argc == 3)
{
try
{
unsigned long width = std::stoul(argv[1]);
unsigned long height = std::stoul(argv[2]);
DEVMODE *dm = new DEVMODE();
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, dm);
dm->dmPelsWidth = width;
dm->dmPelsHeight = height;
ChangeDisplaySettings(dm, CDS_UPDATEREGISTRY);
}
catch (std::invalid_argument e)
{
std::cout << e.what() << std::endl;
}
}
return 0;
}
=== 資料來源
How to change screen resolution using QT, OpenGL, C++, and Linux?
http://stackoverflow.com/questions/11387724/how-to-change-screen-resolution-using-qt-opengl-c-and-linux
How to change screen resolution in Windows using Qt
http://eastfist.com/qt_tutorials/how-to-change-screen-resolution-in-windows-using-qt/
ChangeDisplaySettings function
https://msdn.microsoft.com/en-us/library/dd183411(VS.85).aspx
2015年10月10日 星期六
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
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
=== 測試環境
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月26日 星期六
Arch 安裝 PostgreSQL
第一步先安裝 PostgreSQL
sudo pacman -S postgresql
然後登入 postgres 帳號
sudo -i -u postgres
接著進行資料庫初始化
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
如果發生錯誤,表示目前的 locale 不是 en_US.UTF-8
initdb: invalid locale name "en_US.UTF-8"
初始化資料庫後就可以啟動 PostgreSQL
sudo systemctl start postgresql.service
sudo systemctl enable postgresql.service
建立使用者
createuser --interactive
接著用剛剛建立的使用者帳號建立資料庫
createdb myDatabaseName
使用 psql 進入資料庫
psql -d myDatabaseName
允許遠端連線
修改 /var/lib/postgres/data/postgresql.conf 的內容
listen_addresses = '*'
接著在 /var/lib/postgres/data/pg_hba.conf 將原本的 IPv4設定為
host all all my_remote_client_ip_address/32 md5
設定完之後,重新啟動 PostgreSQL
sudo systemctl restart postgresql
資料來源:
Archlinux Wiki - PostgreSQL
https://wiki.archlinux.org/index.php/PostgreSQL
sudo pacman -S postgresql
然後登入 postgres 帳號
sudo -i -u postgres
接著進行資料庫初始化
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
如果發生錯誤,表示目前的 locale 不是 en_US.UTF-8
initdb: invalid locale name "en_US.UTF-8"
初始化資料庫後就可以啟動 PostgreSQL
sudo systemctl start postgresql.service
sudo systemctl enable postgresql.service
建立使用者
createuser --interactive
接著用剛剛建立的使用者帳號建立資料庫
createdb myDatabaseName
使用 psql 進入資料庫
psql -d myDatabaseName
允許遠端連線
修改 /var/lib/postgres/data/postgresql.conf 的內容
listen_addresses = '*'
接著在 /var/lib/postgres/data/pg_hba.conf 將原本的 IPv4設定為
host all all my_remote_client_ip_address/32 md5
設定完之後,重新啟動 PostgreSQL
sudo systemctl restart postgresql
資料來源:
Archlinux Wiki - PostgreSQL
https://wiki.archlinux.org/index.php/PostgreSQL
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
然後使用 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
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
只要像下列的範例就可以建立。
#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
訂閱:
文章 (Atom)