91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

GNU開發工具——CMake構建Qt工程實踐

發布時間:2020-06-30 14:06:16 來源:網絡 閱讀:2767 作者:天山老妖S 欄目:編程語言

GNU開發工具——CMake構建Qt工程實踐

一、CMake構建Qt工程

1、Qt工程源碼

創建Migration目錄,在目錄下創建main.cpp文件:

#include <QApplication>
#include <QLabel>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QLabel label(QString("Hello Qt%1!").arg(int(QT_VERSION >> 16)));
    label.setAlignment(Qt::AlignCenter);
    label.resize(200, 100);
    label.show();
    return app.exec();
}

2、Qt4和Qt5兼容宏

編寫兼容Qt4和Qt5的CMake宏,QtMigration.cmake文件如下:

# 定義宏QT_USE_MODULES
macro(QT_USE_MODULES _target)
    # Enable AUTOMOC
    set_target_properties(${_target} PROPERTIES AUTOMOC TRUE)
    # Local variables
    set(_modules_qt4)
    set(_modules_qt5)
    # Prepare modules
    foreach(_module ${ARGN})
        list(APPEND _modules_qt4 Qt${_module})
        list(APPEND _modules_qt5 ${_module})
        if(_module MATCHES "Gui")
            list(APPEND _modules_qt5 "Widgets")
        endif(_module MATCHES "Gui")
    endforeach(_module ${ARGN})
    list(REMOVE_DUPLICATES _modules_qt4)
    list(REMOVE_DUPLICATES _modules_qt5)
    # Find Qt libraries
    find_package(Qt5 QUIET COMPONENTS ${_modules_qt5})
    if(Qt5_FOUND)
        qt5_use_modules(${_target} ${_modules_qt5})
    else(Qt5_FOUND)
        find_package(Qt4 QUIET COMPONENTS ${_modules_qt4})
        if(Qt4_FOUND OR QT4_FOUND)
            include(${QT_USE_FILE})
            include_directories(${QT_INCLUDES})
            add_definitions(${QT_DEFINITIONS})
            target_link_libraries(${_target} ${QT_LIBRARIES})
        endif(Qt4_FOUND OR QT4_FOUND)
    endif(Qt5_FOUND)
endmacro(QT_USE_MODULES)

3、CMakeLists.txt編寫

編寫工程CMakeLists.txt文件,內容如下:

cmake_minimum_required(VERSION 2.8.9)
project(Migration)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
include(QtMigration.cmake)
QT_USE_MODULES(${PROJECT_NAME} Core Gui)

4、構建工程

Migration工程內創建build目錄,進入build目錄,進行構建。

cmake ..
make

5、CMake的Qt相關變量

對于Qt4,使用FIND_PACKAGE后,會生成有效的Qt4_FOUND,QT_USE_FILE,QT_INCLUDES,QT_LIBRARIES變量。

FIND_PACKAGE(Qt4 REQUIRED Core Gui)
if(Qt4_FOUND)
    INCLUDE(${QT_USE_FILE})
    INCLUDE_DIRECTORIES(${QT_INCLUDES})
    TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${QT_LIBRARIES})
endif()

對于Qt5,使用FIND_PACKAGE后,會生成有效的Qt5_FOUND,Qt5Core_INCLUDE_DIRS,Qt5Xml_INCLUDE_DIRS,Qt5Gui_INCLUDE_DIRS,Qt5Widgets_INCLUDE_DIRS,Qt5OpenGL_INCLUDE_DIRS,Qt5Widgets_LIBRARIES,Qt5Core_LIBRARIES,Qt5Gui_LIBRARIES,Qt5Xml_LIBRARIES,Qt5OpenGL_LIBRARIES等相應模塊的變量。

FIND_PACKAGE(Qt5 REQUIRED Core Gui Widgets OpenGL Xml)
if(Qt5_FOUND)
    INCLUDE_DIRECTORIES(${Qt5Core_INCLUDE_DIRS} ${Qt5Xml_INCLUDE_DIRS}
        ${Qt5Gui_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5OpenGL_INCLUDE_DIRS})
    #定義QT_LIBRARIES
    SET(QT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${Qt5Core_LIBRARIES}
        ${Qt5Gui_LIBRARIES} ${Qt5Xml_LIBRARIES} ${Qt5OpenGL_LIBRARIES})
    TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${QT_LIBRARIES})
endif(Qt5_FOUND)

開啟MOC支持
set(CMAKE_AUTOMOC ON)
開啟RCC支持
set(CMAKE_AUTORCC ON)
開啟UIC支持
set(CMAKE_AUTOUIC ON)
設置Qt安裝目錄:
set(CMAKE_PREFIX_PATH "Qt to path ") //bin lib include目錄層

二、QtCreator使用CMake構建Qt工程

1、QtCreator創建Qt工程(CMake構建)

選擇新建工程,創建一個Plain C++ Application。
GNU開發工具——CMake構建Qt工程實踐
選擇使用CMake構建工程:
GNU開發工具——CMake構建Qt工程實踐
選擇使用的Qt Kits版本:
GNU開發工具——CMake構建Qt工程實踐

2、增加C++類

選擇菜單欄新建文件按鈕,選擇Files and Classes,C++ Class。
GNU開發工具——CMake構建Qt工程實踐
填寫創建C++類的名稱BarModelMapperChartWidget:
GNU開發工具——CMake構建Qt工程實踐
選擇創建類文件所在位置,點擊瀏覽按鈕:
GNU開發工具——CMake構建Qt工程實踐
點擊新建目錄按鈕創建ChartWidget目錄,選擇ChartWidget。
BarModelMapperChartWidget.h文件如下:

#ifndef BARMODELMAPPERCHARTWIDGET_H
#define BARMODELMAPPERCHARTWIDGET_H

#include <QWidget>
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QVXYModelMapper>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QVBarModelMapper>
#include <QtWidgets/QHeaderView>
#include <QtCharts/QBarCategoryAxis>
#include <QHBoxLayout>

QT_CHARTS_USE_NAMESPACE

/**
 * @brief 柱狀圖ModelMapper報表界面類
 */
class BarModelMapperChartWidget : public QWidget
{
    Q_OBJECT
public:
    explicit BarModelMapperChartWidget(QWidget *parent = NULL);
    /**
     * @brief 初始化ModelMapper
     * @param model
     */
    void initModelMapper(QAbstractTableModel* model);
private:
    QChart* m_chart;//報表組件
    QChartView* m_chartView;//報表視圖組件
};

#endif // BARMODELMAPPERCHARTWIDGET_H

BarModelMapperChartWidget.cpp文件如下:

#include "BarModelMapperChartWidget.h"

BarModelMapperChartWidget::BarModelMapperChartWidget(QWidget *parent) : QWidget(parent)
{
    m_chart = new QChart;
    m_chart->setAnimationOptions(QChart::AllAnimations);

    m_chartView = new QChartView(m_chart);
    m_chartView->setRenderHint(QPainter::Antialiasing);
    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(m_chartView);
    setLayout(layout);
}

void BarModelMapperChartWidget::initModelMapper(QAbstractTableModel *model)
{
    QBarSeries *series = new QBarSeries;
    int first = 3;
    int count = 5;
    QVBarModelMapper *mapper = new QVBarModelMapper(this);
    mapper->setFirstBarSetColumn(1);
    mapper->setLastBarSetColumn(4);
    mapper->setFirstRow(first);
    mapper->setRowCount(count);
    // 設置mapper的數據集對象
    mapper->setSeries(series);
    // 設置mapper對應的數據Model
    mapper->setModel(model);
    m_chart->addSeries(series);

    QStringList categories;
    categories << "April" << "May" << "June" << "July" << "August";
    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    m_chart->createDefaultAxes();
    m_chart->setAxisX(axis, series);
}

創建TableModel類:
GNU開發工具——CMake構建Qt工程實踐
TableModel.h文件如下:

#ifndef TABLEMODEL_H
#define TABLEMODEL_H

#include <QAbstractTableModel>
#include <QHash>
#include <QRect>
#include <QVector>
#include <QTime>
#include <QColor>
#include <QTimer>
/**
 * @brief 數據模型
 */
class TableModel : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit TableModel(QObject *parent = 0);
    virtual ~TableModel();
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
    Qt::ItemFlags flags(const QModelIndex &index) const;
    /**
     * @brief 生成數據
     */
    void generateData();
private slots:
    /**
     * @brief 定時器超時信號的槽函數
     */
    void onGenerateData();
private:
    QList<QVector<qreal> * > m_data;//模型數據容器
    int m_columnCount;//列數
    int m_rowCount;//行數
    QTimer timer;//定時器
};

#endif // TABLEMODEL_H

TableModel.cpp文件如下:

#include "TableModel.h"

TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent)
{
    m_columnCount = 6;
    m_rowCount = 12;
    // 創建數據容器并初始化
    for (int i = 0; i < m_rowCount; i++)
    {
        QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
        for (int k = 0; k < dataVec->size(); k++)
        {
            dataVec->replace(k, 100 + qrand() % 50);
        }
        m_data.append(dataVec);
    }
    // 每隔2秒超時
    timer.start(2000);
    connect(&timer, SIGNAL(timeout()), this, SLOT(onGenerateData()));
}

TableModel::~TableModel()
{
    qDeleteAll(m_data);
}

int TableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_data.count();
}

int TableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_columnCount;
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
        return QString("201%1").arg(section);
    else
        return QString("%1").arg(section + 1);
}

QVariant TableModel::data(const QModelIndex &index, int role) const
{
    if (role == Qt::DisplayRole)
    {
        return m_data[index.row()]->at(index.column());
    }
    else if (role == Qt::EditRole)
    {
        return m_data[index.row()]->at(index.column());
    }

    return QVariant();
}

bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        m_data[index.row()]->replace(index.column(), value.toDouble());
        emit dataChanged(index, index);
        return true;
    }
    return false;
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

void TableModel::generateData()
{
    // 生成新的數據
    for (int i = 0; i < m_rowCount; i++)
    {
        for (int k = 0; k < m_data.at(i)->size(); k++)
        {
            m_data.at(i)->replace(k, 100 + qrand() % 50);
        }
    }
    QModelIndex leftTop = index(0, 0);
    QModelIndex rightBottom = index(m_rowCount - 1, m_columnCount - 1);
    // 生成新數據后發送模型數據發生變化的信號dataChanged
    emit dataChanged(leftTop, rightBottom);
}

void TableModel::onGenerateData()
{
    generateData();
}

增加MainWindow類:
GNU開發工具——CMake構建Qt工程實踐
MainWindow.h文件如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QWidget>
#include <QHBoxLayout>
#include <QTableView>
#include "ChartWidget/BarModelMapperChartWidget.h"
#include "Model/TableModel.h"

/**
 * @brief 程序主界面類
 */
class MainWindow : public QWidget
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = NULL);

private:
    BarModelMapperChartWidget* m_barModelMapperChartWidget;//報表組件
    QTableView* m_tableView;//表格視圖組件
    TableModel* m_model;//表格模型
};

#endif // MAINWINDOW_H

MainWindow.cpp文件如下:

#include "MainWindow.h"

MainWindow::MainWindow(QWidget *parent) : QWidget(parent)
{
    m_model = new TableModel();
    m_barModelMapperChartWidget = new BarModelMapperChartWidget(this);
    m_barModelMapperChartWidget->initModelMapper(m_model);

    m_tableView = new QTableView(this);
    m_tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    m_tableView->setModel(m_model);
    QHBoxLayout* layout = new QHBoxLayout;
    layout->addWidget(m_tableView);
    layout->addWidget(m_barModelMapperChartWidget);
    setLayout(layout);
    resize(800, 400);
}

main.cpp文件:

#include <QApplication>
#include "MainWindow.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}

3、工程CMakeLists.txt文件

CMakeLists.txt文件如下:

# CMake版本約束
cmake_minimum_required(VERSION 2.8.3)
#工程名稱
project(CMakeDemo)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "-fPIC")
set(CMAKE_BUILD_TYPE "Debug")
# 增加子目錄ChartWidget的源碼到變量SOURCES
aux_source_directory(ChartWidget SOURCES)
# 增加子目錄Model的源碼到變量SOURCES
aux_source_directory(Model SOURCES)
# 設置QT安裝路徑
set(CMAKE_PREFIX_PATH "/usr/local/Trolltech/Qt5.10.1/5.10.1/gcc_64")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 開啟Qt MOC
set(CMAKE_AUTOMOC ON)
#開啟Qt ROC
set(CMAKE_AUTORCC ON)
# 開啟Qt UIC
set(CMAKE_AUTOUIC ON)
set(QT Core Gui Widgets Charts)
# 查找Qt庫
find_package(Qt5 REQUIRED ${QT})
if(Qt5_FOUND)
    include_directories(${Qt5Core_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}
        ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Charts_INCLUDE_DIRS})
    set(QT_LIBRARIES ${Qt5Widgets_LIBRARIES} ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Charts_LIBRARIES})
    # 增加可執行文件
    add_executable(${PROJECT_NAME} ${SOURCES} main.cpp MainWindow.cpp)
    target_link_libraries(${PROJECT_NAME} ${QT_LIBRARIES})
endif(Qt5_FOUND)

4、構建

QtCreator管理工程如下:
GNU開發工具——CMake構建Qt工程實踐
編譯、調試可以使用QtCreator直接進行,方便快捷。
如果需要調試,必須在CMakeLists.txt設置(CMAKE_BUILD_TYPE為Debug。
set(CMAKE_BUILD_TYPE "Debug")
運行結果如下:
GNU開發工具——CMake構建Qt工程實踐

源碼示例:https://down.51cto.com/data/2461580

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

原阳县| 莱州市| 抚顺市| 和政县| 花莲县| 册亨县| 宜城市| 中阳县| 甘肃省| 彰化县| 天峨县| 建湖县| 西和县| 塔河县| 筠连县| 如东县| 威信县| 锡林郭勒盟| 建阳市| 枣阳市| 纳雍县| 乌恰县| 偏关县| 龙井市| 黑水县| 福贡县| 简阳市| 恩施市| 光山县| 聊城市| 抚州市| 辽阳市| 包头市| 富民县| 丹阳市| 灵武市| 壤塘县| 太仆寺旗| 八宿县| 洛阳市| 同心县|