一、Qt5.9安裝
Qt5.9 是用於 C++ 應用程序開發的一種跨平台框架,支持 Windows、macOS、Linux、iOS 和 Android,還支持 WebAssembly、watchOS 和 tvOS。你可以訪問官網 https://www.qt.io/download 下載安裝包。安裝步驟如下:
1、選擇合適的版本來下載(開源/商業版)
2、運行安裝文件
3、勾選需要安裝的組件
4、安裝完成後,為了防止環境變量的混亂,最好將 Qt5.x.x 安裝到默認路徑下
下面是官方提供的Demo安裝演示代碼:
void Widget::install()
{
QString demoInstaller = QDir::toNativeSeparators(QCoreApplication::applicationDirPath())
+ "/demos/qt-installer-framework/examples/repositorymeta/meta/";
QStringList arguments;
arguments << "-v";
arguments << "--all-sub-packages";
arguments << "-r" << demoInstaller;
arguments <setStandardOutputProcess(&m_stdoutProcess);
connect(&m_stdoutProcess, &QProcess::readyReadStandardOutput,
this, &Widget::readInstallerStdout);
process->start("go", arguments);
}
二、Qt5.9代碼5.5
如果你正在使用 Qt5.5,升級到 Qt5.9,可以使你受益匪淺,可以讓你使用更多的新特性,比如增強的 Qt Quick 控件、Qt3D 渲染引擎等等。如果你要將 Qt5.5 代碼移植到 Qt5.9,應注意以下內容:
1、修改項目文件,引入新的 Qt 模塊:
# Qt5.5 項目文件: QT += core gui widgets # Qt5.9 項目文件: QT += core gui widgets quick 3d
2、使用 Qt5.9 開發向後兼容的程序,需要使用 Qt::AA_EnableHighDpiScaling 來啟用自動縮放分辨率的功能。
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); //啟用自動縮放分辨率的功能
MainWindow w;
w.show();
return a.exec();
}
三、Qt5.9代碼qt5.7
Qt5.9 的代碼可以在 Qt5.7 的環境下編譯和運行。如果你的工程中使用了 Qt5.9 特性,需要在 Qt5.7 中加入需要使用的模塊。更多內容可以查看官方文檔 https://doc.qt.io/qt-5.9/qtmodules.html,下面是示例代碼:
# Qt5.7 項目文件:
CONFIG += c++11 #使用 C++11 標準
QT += core gui widgets
# Qt5.7 中加入使用 Qt5.9 模塊:
contains(QT_MAJOR_VERSION, 5) {
# Qt5.9 特性
QT += quick 3d
}
四、Qt5.9環境變量配置
在 Windows 上安裝 Qt5.9 後,默認是沒有加入環境變量的,需要在系統環境變量中加入 QT_HOME 和 PATH 變量。在 Linux 或 macOS 上不需要,只要修改 .bashrc 文件即可。
1、Windows :
QT_HOME : C:\Qt\Qt5.9.4\5.9.4\msvc2015_64 #默認路徑 PATH : %QT_HOME%\bin;
2、Linux 和 macOS :
# Linux: echo "export QT_HOME=/opt/Qt/5.9.4/gcc_64" >> ~/.bashrc echo "export PATH=\$PATH:\$QT_HOME/bin" >> ~/.bashrc source ~/.bashrc # macOS : echo "export QT_HOME=/usr/local/Qt/5.9.4/clang_64" >> ~/.bash_profile echo "export PATH=\$PATH:\$QT_HOME/bin" >> ~/.bash_profile source ~/.bash_profile
五、Qt5.9.7如何創建桌面快捷方式
在 Windows 上使用 Qt5.9.7 創建桌面快捷方式,可以使用 QWinTaskbarButton 類。下面是示例代碼:
/* 創建快捷方式 */
void MainWindow::on_createShortcutButton_clicked()
{
QString shortcutName = QApplication::applicationName();
QString shortcutDir = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
QString shortcutPath = shortcutDir + "/" + shortcutName + ".lnk";
QString exePath = QApplication::applicationFilePath();
QWinTaskbarButton *taskbarButton = new QWinTaskbarButton(this);
QWinTaskbarProgress *taskbarProgress = taskbarButton->progress();
taskbarButton->setWindow(this->windowHandle());
QFileInfo exeFileInfo(exePath);
const QString exeIcoPath = ":/res/" + exeFileInfo.baseName() + ".ico";
QIcon icon(exeIcoPath);
QFile exeIcoFile(exeIcoPath);
if (exeIcoFile.exists() && exeIcoFile.open(QFile::ReadOnly)) {
icon = QIcon(QPixmap::fromImage(QImage::fromData(exeIcoFile.readAll())));
}
QFile link(shortcutPath);
link.open(QIODevice::WriteOnly);
QDataStream out(&link);
out.setVersion(QDataStream::Qt_5_0);
out << quint32(0x0000000b) << QString("InternetShortcut") << quint32(0x00000001) << QString("") << quint16(0x0000);
out << QString("URL") << QString("file:///" + exePath).replace("/", "\\") << quint16(0x0000);
out << QString("IconFile") << exeIcoPath << quint16(0x0000) << qint32(0x00000000) << quint32(0x00000000) << quint32(0x00000001);
out << QString("ShowCommand") <setVisible(true);
taskbarProgress->setValue(0);
taskbarProgress->resume();
//將當前窗口添加到任務欄
taskbarButton->show();
//設置進度條的值
for (int i = 0; i setValue(i);
QTest::qWait(10);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/195869.html
微信掃一掃
支付寶掃一掃