QT5.x开发环境配置

准备工作

参考 https://wiki.qt.io/Install_Qt_5_on_Ubuntu .

# 安装g++
sudo apt install build-essential
# 
sudo apt install libfontconfig1
# 安装openGL支持
sudo apt install mesa-common-dev libglu1-mesa-dev

ustc镜像直接下载安装包, 地址是 http://mirrors.ustc.edu.cn/qtproject/official_releases/qt/5.11/5.11.1/

最后一个开源安装版本是5.14.2, 地址 http://mirrors.ustc.edu.cn/qtproject/official_releases/qt/5.14/5.14.2/

执行chmod u+x使其可执行

Ubuntu安装

直接运行 qt-opensource-linux-x64-5.11.1.run, 会出现安装界面, 这一步可以配置网络代理, 往下进行需要填写自己在QT注册的账户和口令, 会进行在线验证.

设置安装目录时, 可以将路径配置到/opt下, 例如 /opt/qt/Qt5.11.1, 会在最后一步时弹出提示输入su口令.

组件选择: 普通使用一个都不用选, 但是建议全选, 因为这样安装的Qt, 其repository是空的, 之后如果缺组件需要再添加的话比较麻烦, 全选的话整个安装下来占地5.4GB

在菜单中找到Qt Creator, 启动非常快

Windows安装

补充一下win10下的安装,首先下载并运行 qt-opensource-windows-x86-5.14.2.exe,输入账号和口令后,选择组件时,
Qt 5.14.2下至少要选择MSVC 2017 64-bit, MinGW 7.3.0 64-bit, 如果空间允许则将32bit也选上,全选Qt开头的那些组件
Developer and Designer Tools下至少要选择默认的Qt Creator 4.11.1 CDB Debugger Support,和MinGW 7.30 64-bit,空间允许则32bit也选上。
这样需要5.x GB的空间。如果MSVC和MinGW这些组件未选,则打开项目时无法配置桌面环境,并且用维护工具再添加很麻烦。

配置MSVC编译器:前往 https://visualstudio.microsoft.com/vs/older-downloads/ 下载里面的 Build Tools for Visual Studio 2017,下载一个1MB多的Visual Studio Installer可执行文件,运行后会自动下载并展开选择界面,在这个界面上选择Visual C++ build tools,Optional中的Windows 10 SDK很大,这个勾选的话空间要4.5G,不勾选则1.58G。

如果 用到了corecrt.h,那么还需要在Individual Components中勾选 Compilers, build tools and runtimes下面的Windows Universal CRT SDK.

HiDPI支持

默认安装时在3K以上高分屏上显示会有问题,部分界面尺寸及字体非常小,需要配置一下环境变量,修改 .profile,增加

export QT_AUTO_SCREEN_SCALE_FACTOR=1

 在Ubuntu20.04, Qt5.14下测试对系统桌面设置为125%的缩放显示正常。也可以使用 QT_ENABLE_HIGHDPI_SCALING=1

网上搜到的 export QT_DEVICE_PIXEL_RATIO 在运行时会提示warning。

 

运行一个例子

在网上找了一个例子, 项目名叫First, 但是编译中出现了错误

$ gcc   first.o all   -o first
gcc: error: all: No such file or directory

这个是因为文件名称first引起了歧义. 将项目名改为MyFirst就正常编译通过了.

原因说明 https://www.qtcentre.org/threads/32635-compilation-errors-quot-gcc-all-No-such-file-or-directory-quot 

The Makefile contains a generated (fake) target called "first" and it also contains a set of explicit and implied rules related to the source file "first.cpp". The make command builds your TARGET (which is named after the directory if not specified) and then decides that because "first.o" changed it must rebuild the "first" target: at this point it gets confused.

$ make
/usr/bin/moc -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. first.cpp -o first.moc
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. -o first.o first.cpp
g++ -Wl,-O1 -Wl,-rpath,/usr/lib/qt4 -o simple_example first.o    -L/usr/lib/qt4 -lQtSql -L/usr/lib/mysql -L/usr/lib/qt4 -lQtGui -L/usr/X11R6/lib -lQtCore -lgthread-2.0 -lrt -lglib-2.0 -lpthread
gcc   first.o all   -o first   // <<<<< confused, decides to rebuild "first" target using a file called "all"
gcc: all: No such file or directory
make: *** [first] Error 1

You get similar confusion (that make recovers from) if "TARGET = first" even with no files called "first.cpp".

$ make
/usr/bin/moc -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. main.cpp -o main.moc
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtSql -I/usr/include/qt4 -I. -I. -o main.o main.cpp
make: Circular all <- first dependency dropped.   // <<<< confused but recognises the situation
g++ -Wl,-O1 -Wl,-rpath,/usr/lib/qt4 -o first main.o    -L/usr/lib/qt4 -lQtSql -L/usr/lib/mysql -L/usr/lib/qt4 -lQtGui -L/usr/X11R6/lib -lQtCore -lgthread-2.0 -lrt -lglib-2.0 -lpthread

Adding

    .PHONY = first

to Makefile fixes this problem but probably generates another if the TARGET = first. The change would be overwritten by qmake anyway. Easier to just avoid it.

Qt5常用快捷键

F2  跳转到变量的声明, 切换函数的声明和实现
F4  在头文件和C文件之间切换
Ctrl + B 构建
Ctrl + R 运行
Alt + Enter 在H文件的成员变量上, 会右键弹出创建setter, getter的选项, 非常省事

Qt5教程

bogotobogo.com的这个教程不错, 值得推荐 http://www.bogotobogo.com/Qt/Qt_tutorial_list_New.php

一个带菜单的图片浏览器例子

来源是

http://www.bogotobogo.com/Qt/Qt5_QMainWindow_QAction_ImageViewer.php

http://www.bogotobogo.com/Qt/Qt5_QMainWindow_QAction_ImageViewer_B.php

其中的主要部分代码为

MenuWindow.pro

#-------------------------------------------------
#
# Project created by QtCreator 2018-08-27T14:02:44
#
#-------------------------------------------------

QT       += core gui
QT       += printsupport

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = MenuWindow
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp \
    mydialog.cpp

HEADERS += \
        mainwindow.h \
    mydialog.h

FORMS += \
        mainwindow.ui \
    mydialog.ui

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QLabel>
#include <QScrollArea>
#include <QMenu>
#include <QAction>
#include "mydialog.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void open();
    void print();
    void zoomIn();
    void zoomOut();
    void normalSize();
    void fitToWindow();
    void about();
    void newWindow();

private:
    Ui::MainWindow *ui;
    MyDialog *myDialog;
    QLabel *imageLabel;
    QScrollArea *scrollArea;
    QMenu *fileMenu;
    QMenu *viewMenu;
    QMenu *helpMenu;
    QAction *openAct;
    QAction *printAct;
    QAction *newWindowAct;
    QAction *exitAct;
    QAction *zoomInAct;
    QAction *zoomOutAct;
    QAction *normalSizeAct;
    QAction *fitToWindowAct;
    QAction *aboutAct;
    QAction *aboutQtAct;

    double scaleFactor;

    void updateActions();
    void scaleImage(double factor);
    void adjustScrollBar(QScrollBar *scrollBar, double factor);
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QtWidgets>
#include <QFileDialog>
#include <QMessageBox>

#ifndef QT_NO_PRINTER
#include <QPrintDialog>
#endif

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);

    openAct = new QAction(tr("&Open..."), this);
    openAct->setShortcut(tr("Ctrl+O"));

    printAct = new QAction(tr("&Print..."), this);
    printAct->setShortcut(tr("Ctrl+P"));
    printAct->setEnabled(false);

    newWindowAct = new QAction(tr("&New Window"), this);
    newWindowAct->setShortcut(tr("Ctrl+N"));

    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));

    zoomInAct = new QAction(tr("Zoom &In (25%)"), this);
    zoomInAct->setShortcut(tr("Ctrl+="));   //(Ctrl)(+)
    zoomInAct->setEnabled(false);

    zoomOutAct = new QAction(tr("Zoom &Out (25%)"), this);
    zoomOutAct->setShortcut(tr("Ctrl+-"));  //(Ctrl)(-)
    zoomOutAct->setEnabled(false);

    normalSizeAct = new QAction(tr("&Normal Size"), this);
    normalSizeAct->setShortcut(tr("Ctrl+S"));
    normalSizeAct->setEnabled(false);

    fitToWindowAct = new QAction(tr("&Fit to Window"), this);
    fitToWindowAct->setEnabled(false);
    fitToWindowAct->setCheckable(true);
    fitToWindowAct->setShortcut(tr("Ctrl+F"));

    aboutAct = new QAction(tr("&About"), this);

    aboutQtAct = new QAction(tr("About &Qt"), this);

    fileMenu = new QMenu(tr("&File"), this);
    fileMenu->addAction(openAct);
    fileMenu->addAction(printAct);
    fileMenu->addAction(newWindowAct);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    viewMenu = new QMenu(tr("&View"), this);
    viewMenu->addAction(zoomInAct);
    viewMenu->addAction(zoomOutAct);
    viewMenu->addAction(normalSizeAct);
    viewMenu->addSeparator();
    viewMenu->addAction(fitToWindowAct);

    helpMenu = new QMenu(tr("&Help"), this);
    helpMenu->addAction(aboutAct);
    helpMenu->addAction(aboutQtAct);

    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(viewMenu);
    menuBar()->addMenu(helpMenu);

    imageLabel = new QLabel(this);
    imageLabel->setBackgroundRole(QPalette::Base);
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);

    scrollArea = new QScrollArea(this);
    scrollArea->setBackgroundRole(QPalette::Dark);
    scrollArea->setWidget(imageLabel);

    setCentralWidget(scrollArea);
    setWindowTitle(tr("Image Viewer"));
    resize(500, 400);

    connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
    connect(printAct, SIGNAL(triggered()), this, SLOT(print()));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    connect(zoomInAct, SIGNAL(triggered()), this, SLOT(zoomIn()));
    connect(zoomOutAct, SIGNAL(triggered()), this, SLOT(zoomOut()));
    connect(normalSizeAct, SIGNAL(triggered()), this, SLOT(normalSize()));
    connect(fitToWindowAct, SIGNAL(triggered()), this, SLOT(fitToWindow()));
    connect(aboutAct, SIGNAL(triggered()), this, SLOT(about()));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    connect(newWindowAct, SIGNAL(triggered(bool)), this, SLOT(newWindow()));
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::open() {
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath());
    if (!fileName.isEmpty()) {
        QImage image(fileName);
        if (image.isNull()) {
            QMessageBox::information(this, tr("Image Viewer"), tr("Cannot load %1.").arg(fileName));
            return;
        }
        imageLabel->setPixmap(QPixmap::fromImage(image));
        scaleFactor = 1.0;

        printAct->setEnabled(true);
        fitToWindowAct->setEnabled(true);
        updateActions();

        if (!fitToWindowAct->isChecked())
            imageLabel->adjustSize();
    }
}

void MainWindow::print() {

}

void MainWindow::newWindow() {
    myDialog = new MyDialog(this);
    myDialog->resize(300, 200);
    /*myDialog->setModal(true);
    myDialog->exec();*/
    myDialog->show();
}

void MainWindow::zoomIn() {
    scaleImage(1.25);
}

void MainWindow::zoomOut() {
    scaleImage(0.8);
}

void MainWindow::normalSize() {
    imageLabel->adjustSize();
    scaleFactor = 1.0;
}

void MainWindow::fitToWindow() {
    bool fitToWindow = fitToWindowAct->isChecked();
    scrollArea->setWidgetResizable(fitToWindow);
    if (!fitToWindow) {
        normalSize();
    }
    updateActions();
}

void MainWindow::about() {
    QMessageBox::about(this, tr("About Image Viewer"), tr("<b>Image Viewer</b> example."));
}

void MainWindow::updateActions() {
    zoomInAct->setEnabled(!fitToWindowAct->isChecked());
    zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
    normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
}

void MainWindow::scaleImage(double factor) {
    Q_ASSERT(imageLabel->pixmap());
    scaleFactor *= factor;
    imageLabel->resize(scaleFactor * imageLabel->pixmap()->size());

    adjustScrollBar(scrollArea->horizontalScrollBar(), factor);
    adjustScrollBar(scrollArea->verticalScrollBar(), factor);

    zoomInAct->setEnabled(scaleFactor < 3.0);
    zoomOutAct->setEnabled(scaleFactor > 0.333);
}

void MainWindow::adjustScrollBar(QScrollBar *scrollBar, double factor)
{
    int value = scrollBar->value();
    int step = scrollBar->pageStep();
    value = int(factor * value + ((factor - 1) * step/2));
    scrollBar->setValue(value);
}

QScrollBar组件

结合Qt5中对于QScrollBar的说明, 对void MainWindow::scaleImage(double factor) 和 void MainWindow::adjustScrollBar(QScrollBar *scrollBar, double factor) 这两个方法进行分析.

 

a. 这是滚动条的滑块, 提供了快速的定位, 但是不能提供精确的定位
b. 箭头按钮用于精确地浏览定位, 对于文本编辑器, 典型的步长是一行, 而对于图片则是20像素.
c. 翻页控制部分, 点击的效果等价于一次翻页, 正常情况下, 一个翻页对于scrollbar的value的变化等于滑块的长度.
每个滚动条有一个value字段, 用于标示此滑块距离滚动条起点的距离. 这个值一定是介于 minimum() 和 maximum() 之间. 在 minimum value和 maximum value时, 滑块的位置分别如上图所示. 所以要注意, 这个滚动条的长度并非等于maximum().

由此可知, 对于前面例子中图片放大factor倍时, 对于scrollbar.value:
1. 如果value不变, 那么当图片放大时, 因为maximum value增大, 故滑块一边缩小, 一边位置会往起始方向收缩
2. 如果value与factor进行同等比例的放大, 那么滑块一边缩小, 一边起始边界的位置保持不变
3. 如果value与factor进行同等比例放大的同时, 增加滑块长度(pageStep)的收缩长度, 那么滑块一边缩小, 一边中心位置保持不变. 所以上面adjustScrollBar()方法的处理为

value = int(factor * value + ((factor - 1) * step/2));

Qt5的Layout

常用的Layout主要有以下几种

  • QHBoxLayout 将组件按水平进行布局, 宽度平均分割, 高度占满外框
  • QVBoxLayout 将组件按垂直进行布局, 高度平均分割, 宽度占满外框
  • QGridLayout 将组件按表格进行布局, 类似于HTML中的table tr td, 可以水平跨列, 也可以垂直跨行
  • QFormLayout 用于有多个成对的标签+输入的界面, 可以设置不同风格以及在宽带不足时自动换行
  • QGraphicsAnchorLayout 用于在组件上添加锚点, 并以锚点的相对位置来控制布局
  • QStackedLayout 对于内部的组件, 每次只显示一个(可以用于制作标签页)

而在主窗口中, 常用的底层布局并非Layout, 而是QSplitter. 这个自带了对内部widget的自动拉伸, 以及分隔栏的鼠标拖动调整,非常方便. 可以参考Redis Desktop Manager的布局设计(是qml格式的, 但是可以看出来布局的设计) https://github.com/uglide/RedisDesktopManager/blob/0.9/src/qml/app.qml

import QtQuick 2.0
import QtQuick.Layouts 1.1
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.2
import QtQml.Models 2.2
import QtQuick.Window 2.2
import Qt.labs.settings 1.0
import "."
import "./common"
import "./common/platformutils.js" as PlatformUtils
import "./value-editor"
import "./connections-tree"
import "./console"
import "./server-info"
import "./bulk-operations"

ApplicationWindow {
    id: approot
    visible: true
    objectName: "rdm_qml_root"
    title: "Redis Desktop Manager " + Qt.application.version
    width: 1100
    height: 800

    property double wRatio : (width * 1.0) / (Screen.width * 1.0)
    property double hRatio : (height * 1.0) / (Screen.height * 1.0)

    property var currentValueFormatter

    Component.onCompleted: {
        if (hRatio > 1 || wRatio > 1) {
            console.log("Ratio > 1.0. Resize main window.")
            width = Screen.width * 0.9
            height = Screen.height * 0.8
        }

        if (PlatformUtils.isOSXRetina(Screen)) {
            bottomTabView.implicitHeight = 100
        }
    }

    Settings {
        category: "windows_settings"
        property alias x: approot.x
        property alias y: approot.y
        property alias width: approot.width
        property alias height: approot.height
    }

    SystemPalette {
        id: sysPalette
    }

    FontLoader {
        id: monospacedFont
        Component.onCompleted: {
            source = "qrc:/fonts/Inconsolata-Regular.ttf"
        }
    }

    QuickStartDialog {
        id: quickStartDialog
        objectName: "rdm_qml_quick_start_dialog"

        width: PlatformUtils.isOSX() ? 600 : approot.width * 0.8
    }

    GlobalSettings {
        id: settingsDialog
    }

    ConnectionSettignsDialog {
        id: connectionSettingsDialog

        objectName: "rdm_connection_settings_dialog"

        onTestConnection: {                       
            if (connectionsManager.testConnectionSettings(settings)) {
                hideLoader()
                showMsg(qsTr("Successful connection to redis-server"))
            } else {
                hideLoader()
                showError(qsTr("Can't connect to redis-server"))
            }            
        }

        onSaveConnection: connectionsManager.updateConnection(settings)
    }

    MessageDialog {
        id: notification
        objectName: "rdm_qml_error_dialog"
        visible: false
        modality: Qt.WindowModal
        icon: StandardIcon.Warning
        standardButtons: StandardButton.Ok

        function showError(msg) {
            icon = StandardIcon.Warning
            text = msg
            open()
        }

        function showMsg(msg) {
            icon = StandardIcon.Information
            text = msg
            open()
        }
    }

    BulkOperationsDialog {
        id: bulkOperationDialog
    }

    Connections {
        target: bulkOperations

        onOpenDialog: {
            bulkOperationDialog.operationName = operationName
            bulkOperationDialog.open()
        }
    }

    Connections {
        target: connectionsManager

        onEditConnection: {
            connectionSettingsDialog.settings = config
            connectionSettingsDialog.open()
        }

        onError: {            
            notification.showError(err)
        }

        Component.onCompleted: {
            if (connectionsManager.size() == 0)
                quickStartDialog.open()
        }
    }

    toolBar: AppToolBar {}

    BetterSplitView {
        anchors.fill: parent
        orientation: Qt.Horizontal        

        BetterTreeView {
            id: connectionsTree
            Layout.fillHeight: true
            Layout.minimumWidth: 350
            Layout.minimumHeight: 500
        }

        BetterSplitView {
            orientation: Qt.Vertical

            BetterTabView {
                id: tabs
                objectName: "rdm_qml_tabs"
                currentIndex: 0

                Layout.fillHeight: true
                Layout.fillWidth: true
                Layout.minimumWidth: 650
                Layout.minimumHeight: 30

                onCurrentIndexChanged: {

                    if (tabs.getTab(currentIndex).tabType) {
                        if (tabs.getTab(currentIndex).tabType == "value") {

                            var realIndex = currentIndex - serverStatsModel.tabsCount();

                            if (welcomeTab) {
                                realIndex -= 1
                            }

                            viewModel.setCurrentTab(realIndex);
                        } else if (tabs.getTab(currentIndex).tabType == "server_info") {
                            var realIndex = currentIndex;

                            if (welcomeTab) {
                                realIndex -= 1
                            }

                            serverStatsModel.setCurrentTab(index);
                        }
                    }
                }

                WelcomeTab {
                    id: welcomeTab
                    clip: true
                    objectName: "rdm_qml_welcome_tab"

                    property bool not_mapped: true

                    onClose: tabs.removeTab(index)

                    function closeIfOpened() {
                        var welcomeTab = tabs.getTab(0)

                        if (welcomeTab && welcomeTab.not_mapped)
                            tabs.removeTab(0)
                    }
                }

                ServerInfoTabs {
                    model: serverStatsModel
                }

                Connections {
                    target: serverStatsModel

                    onRowsInserted: if (welcomeTab) welcomeTab.closeIfOpened()
                }

                ValueTabs {
                    objectName: "rdm_qml_value_tabs"
                    model: valuesModel
                }

                AddKeyDialog {
                    id: addNewKeyDialog
                    objectName: "rdm_qml_new_key_dialog"

                    width: approot.width * 0.7
                    height: {
                        if (approot.height > 500) {
                            return approot.height * 0.7
                        } else {
                            return approot.height
                        }
                    }
                }

                Connections {
                    target: valuesModel
                    onKeyError: {
                        if (index != -1)
                            tabs.currentIndex = index

                        notification.showError(error)
                    }

                    onRowsInserted: {
                        if (welcomeTab) welcomeTab.closeIfOpened()
                    }

                    onNewKeyDialog: addNewKeyDialog.open()
                }
            }

            BetterTabView {
                id: bottomTabView
                Layout.fillWidth: true
                Layout.minimumHeight: PlatformUtils.isOSXRetina()? 15 : 30

                tabPosition: Qt.BottomEdge

                Consoles {
                    objectName: "rdm_qml_console_tabs"
                    model: consoleModel
                }

                Connections {
                    target: consoleModel

                    onChangeCurrentTab: {
                        bottomTabView.currentIndex = i + 1
                    }
                }

                BetterTab {
                    closable: false
                    title: "Log"
                    icon: "qrc:/images/log.svg"

                    BaseConsole {
                        id: logTab
                        readOnly: true
                        textColor: "#6D6D6E"

                        Connections {
                            target: appLogger
                            onEvent: logTab.append(msg)
                            Component.onCompleted: appLogger.getMessages()
                        }
                    }
                }
            }
        }
    }
}

QT的多国化

在现有代码中增加多国化的步骤

修改pro文件

增加翻译配置, 这里可以增加多个

TRANSLATIONS += \
    $$PWD/languages/v2rock_zh_CN.ts

生成ts文件

点击Tools -> External -> Linguist -> Update Translations (lupdate), 会根据上一步的pro文件, 生成对应的ts文件

如果项目中对tr()有改动, 也需要调用这个命令更新ts文件

编辑ts文件

切换到File System模式, 在ts文件上右键, 点击 Open With -> Qt Linguist, 在界面里会列出每一个语言条目, 输入翻译后 Ctrl + Enter就会打勾并进入下一条

生成qm文件

可以通过菜单 Tools -> External -> Linguist -> Release Translations (lrelease), 如果项目路径较复杂, 可以通过命令行执行 lrelease xxx.ts 直接生成

添加qm到资源文件

打开项目资源文件(qrc文件), 将qm文件添加到资源列表, 例如

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
    <file>images/off.png</file>
    <file>languages/app_zh_CN.qm</file>
</qresource>
</RCC>

在程序中引用多国化资源

在main.cpp中, 增加 

#include <QTranslator>
....

int main(int argc, char *argv[])
{
    ...

    QTranslator translate;
    bool bret = translate.load(":/languages/app_zh_CN.qm");
    if(!bret)
    {
       qWarning() << "Load language file failed!";
    }
    a.installTranslator(&translate);

    MainDialog w;
    w.show();

    return a.exec();
}

 

Qt界面程序编写的步骤

1. 设计好全局的QAction, 这个可以被顶栏菜单QMenuBar, 工具栏QToolBar 以及 右键弹出菜单QMenu引用.

2. 设计好主窗体布局和主要widget

3. 开始制作各种slot, 并且将QAction connect到对应的slot上, 这一步需要开始设计各个弹出的QDialog

一个未完成的主窗口例子

rockredismain.h

#ifndef ROCKREDISMAIN_H
#define ROCKREDISMAIN_H

#include <QMainWindow>
#include <QSplitter>
#include <QTreeWidget>
#include <QTableWidget>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>

namespace Ui {
class RockRedisMain;
}

class RockRedisMain : public QMainWindow
{
    Q_OBJECT

public:
    explicit RockRedisMain(QWidget *parent = 0);
    ~RockRedisMain();

private slots:
    void onCustomContextMenuRequested(const QPoint& point);
    void showContextMenu(QTreeWidgetItem* item, const QPoint& globalPos);
    void onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous);

private:
    Ui::RockRedisMain *ui;
    QSplitter * splitter;
    QScrollArea *scrollArea;
    QTreeWidget *treeWidget;
    QTableWidget* tableWidget;
    QMenu *fileMenu;
    QMenu *helpMenu;
    QToolBar *fileToolBar;
    QAction *newConnectionAct;
    QAction *editConnectionAct;
    QAction *removeConnectionAct;
    QAction *exitAct;
    QAction *aboutAct;

    void initMenu();
    QTreeWidgetItem* addTreeNode(QString name, QString description);
    QTreeWidgetItem* addChildNode(QTreeWidgetItem *parent, QString name, QString description);
};

#endif // ROCKREDISMAIN_H

rockredismain.cpp

#include "rockredismain.h"
#include "ui_rockredismain.h"

RockRedisMain::RockRedisMain(QWidget *parent) : QMainWindow(parent), ui(new Ui::RockRedisMain) {
    ui->setupUi(this);
    initMenu();

    splitter = new QSplitter(Qt::Horizontal, this);
    setCentralWidget(splitter);

    treeWidget = new QTreeWidget();
    treeWidget->setHeaderHidden(true);
    //treeWidget->setHeaderLabels(QStringList() << "Connections");
    treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    QTreeWidgetItem* item = addTreeNode("Folder", "It's a folder");
    addChildNode(item, "A Connection Folder", "first");
    addChildNode(item, "B Connection File", "second");

    item = addTreeNode("File", "it's a file");
    addChildNode(item, "A", "first");
    addChildNode(item, "B", "second");

    addTreeNode("Folder", "It's a folder");
    addTreeNode("File", "it's a file");
    addTreeNode("Folder", "It's a folder");
    addTreeNode("File", "it's a file");
    addTreeNode("Folder", "It's a folder");
    addTreeNode("File", "it's a file");

    treeWidget->resize(100, 100);
    splitter->addWidget(treeWidget);

    tableWidget = new QTableWidget();
    splitter->addWidget(tableWidget);
    splitter->setStretchFactor(1,1);

    setWindowTitle(tr("RockRedis"));
    resize(800, 600);

    connect(treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onCustomContextMenuRequested(QPoint)));
    connect(treeWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), SLOT(onCurrentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)));
}

RockRedisMain::~RockRedisMain() {
    delete ui;
}

void RockRedisMain::initMenu() {
    fileMenu = new QMenu(tr("&File"), this);
    helpMenu = new QMenu(tr("&Help"), this);
    menuBar()->addMenu(fileMenu);
    menuBar()->addMenu(helpMenu);

    newConnectionAct = new QAction(tr("&New Connection"), this);
    newConnectionAct->setShortcut(tr("Ctrl+N"));
    newConnectionAct->setStatusTip(tr("Add a new connection"));
    //connect(newConnectionAct, SIGNAL(triggered()), this, SLOT(open()));
    fileMenu->addAction(newConnectionAct);

    editConnectionAct = new QAction(tr("&Edit Connection"), this);
    editConnectionAct->setShortcut(tr("Alt+E"));
    editConnectionAct->setEnabled(false);
    //connect(editConnectionAct, SIGNAL(triggered()), this, SLOT(print()));
    fileMenu->addAction(editConnectionAct);

    removeConnectionAct = new QAction(tr("Remove Connection"), this);
    editConnectionAct->setShortcut(tr("Del"));
    removeConnectionAct->setEnabled(false);
    //connect(newWindowAct, SIGNAL(triggered(bool)), this, SLOT(newWindow()));
    fileMenu->addAction(removeConnectionAct);

    exitAct = new QAction(tr("E&xit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
    fileMenu->addAction(exitAct);

    aboutAct = new QAction(tr("&About"), this);
    helpMenu->addAction(aboutAct);
}

QTreeWidgetItem* RockRedisMain::addTreeNode(QString name, QString description) {
    QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, 0);
    item->setText(0, name);
    item->setText(1, description);
    return item;
}

QTreeWidgetItem* RockRedisMain::addChildNode(QTreeWidgetItem *parent, QString name, QString description) {
    QTreeWidgetItem *item = new QTreeWidgetItem(parent, 1);
    item->setText(0, name);
    item->setText(1, description);
    return item;
}

void RockRedisMain::onCustomContextMenuRequested(const QPoint& point) {
    QTreeWidgetItem* item = treeWidget->itemAt(point);

    if (item) {
        // Note: We must map the point to global from the viewport to
        // account for the header.
        showContextMenu(item, treeWidget->mapToGlobal(point));
    }
}

void RockRedisMain::showContextMenu(QTreeWidgetItem* item, const QPoint& point) {
    QMenu menu;
    switch (item->type()) {
        case 0:
            menu.addAction(editConnectionAct);
            menu.addAction(removeConnectionAct);
            break;
        case 1:
            menu.addAction("This is a type 2");
            break;
    }
    menu.exec(point);
}

void RockRedisMain::onCurrentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) {
    if (current == NULL) {
        editConnectionAct->setEnabled(false);
        removeConnectionAct->setEnabled(false);
    } else if (current->type() == 0) {
        editConnectionAct->setEnabled(true);
        removeConnectionAct->setEnabled(true);
    } else {
        editConnectionAct->setEnabled(false);
        removeConnectionAct->setEnabled(false);
    }
}

 

QT项目的图标

在Linux下发布项目, 图标遵循的也是Linux下的规范, 现在较流行的是 freedesktop , 可以参考其最新的文档. 应用的图标必须按theme分类, theme下面再按图标尺寸分目录存储. 必须实现的theme是hicolor. 在Ubuntu20.04下, 图标的存放位置主要为 /usr/share/icons/ 和 $HOME/.local/share/icons/ 这两个目录, 其他的在/usr/share/ 下分应用, 在各自目录下还有一些icons目录.

/usr/share/icons/ 是系统主要的图标目录, 下面有hicolor, Yaru, locolor, default, gnome等多个目录, 每个目录对应的一种theme, 在每个theme目录下, 是按尺寸分类的子目录, 每个子目录下是对应尺寸的应用图标类别, 类别下才是图标的png文件. 16x16这个尺寸的图标是最完整的.

/usr/share/icons/hicolor/16x16/apps/$ ll
total 164
-rw-r--r-- 1 root root  658 Mar 13 16:32 bluetooth.png
-rw-r--r-- 1 root root 1418 Mar 24 22:05 display-im6.q16.png
-rw-r--r-- 1 root root  526 Apr 15 22:27 evolution-mail.png
-rw-r--r-- 1 root root  553 Apr 15 22:27 evolution-memos.png
-rw-r--r-- 1 root root  714 Apr 15 22:27 evolution-tasks.png
-rw-r--r-- 1 root root  459 Feb  8 18:59 filezilla.png
-rw-r--r-- 1 root root  722 May  8 22:42 firefox.png
-rw-r--r-- 1 root root  411 Mar 19 23:30 gcr-gnupg.png
-rw-r--r-- 1 root root  693 Mar 19 23:30 gcr-key-pair.png
-rw-r--r-- 1 root root  511 Mar 19 23:30 gcr-key.png
-rw-r--r-- 1 root root  267 Mar 19 23:30 gcr-password.png
-rw-r--r-- 1 root root  334 Mar 19 23:30 gcr-smart-card.png
-rw-r--r-- 1 root root  528 May  1 15:10 gnome-power-manager.png
-rw-r--r-- 1 root root  781 May  1 15:10 goa-panel.png
-rw-r--r-- 1 root root  689 May 10 01:30 google-chrome.png
-rw-r--r-- 1 root root  640 Apr  2 19:25 hook-notifier.png
-rw-r--r-- 1 root root  508 Mar 30 17:50 ibus-keyboard.png
-rw-r--r-- 1 root root  586 Apr 16 19:44 inkscape.png

同样需要了解的是freedesktop的桌面应用图标标准,  https://www.freedesktop.org/wiki/Specifications/desktop-entry-spec/ 

这个标准规定了桌面图标文件(.desktop文件)的格式. desktop文件通常存储于  /usr/share/applications/ 和 $HOME/.local/share/applications/ 目录. 前者是系统的应用入口文件路径, 可以被所有用户访问. 以下是QT自己的desktop文件作为例子

[Desktop Entry]
Type=Application
Exec="/opt/qt/Qt5.14.2/Tools/QtCreator/bin/qtcreator" %F
Name=Qt Creator
GenericName=The IDE of choice for Qt development.
Icon=QtProject-qtcreator
StartupWMClass=qtcreator
Terminal=false
Categories=Development;IDE;Qt;
MimeType=text/x-c++src;text/x-c++hdr;text/x-xsrc;application/x-designer;applicat
ion/vnd.qt.qmakeprofile;application/vnd.qt.xml.resource;text/x-qml;text/x-qt.qml
;text/x-qt.qbs;

  

查看QT生成的AppImage文件

有两种类型,  

Type 1:

mkdir mountpoint
sudo mount -o loop my.AppImage mountpoint/

# You can now inspect the contents
# You can now also copy the contents to a writable location of your hard disk

sudo umount mountpoint/
# Do not forget the umount step!
# If you do forget it, your system may exhibit unwanted behavior.

Type 2:

mkdir mountpoint
my.AppImage --appimage-offset
123456 # This is just an example output

sudo mount my.AppImage mountpoint/ -o offset=123456

# you can now inspect the contents

sudo umount mountpoint/
# Do not forget the umount step!
# If you do forget it, your system may exhibit unwanted behavior.

  

 

corecrt.h

posted on 2018-08-25 22:59  Milton  阅读(6425)  评论(0编辑  收藏  举报

导航