自学内容网 自学内容网

Qt笔记(十七)cmake编译Qt项目

1. 文件内容与文件结构

1.1.文件目录

文件夹下文件,其中CMakeLists.txt是我新添加的。

MouseEvent
        CMakeLists.txt
        main.cpp
        mouseevent.cpp
        mouseevent.h

1.2. CMakeLists.txt内容

cmake_minimum_required(VERSION 3.5)

project(mouseEvent LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5 COMPONENTS Widgets REQUIRED)

include_directories(
    ${PROJECT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_BINARY_DIR}
)

# 由于采用set(CMAKE_AUTOUIC ON)的方式处理ui文件,编译时会报错找不到ui文件。
# 需要将ui文件夹加入AUTOUIC的搜索目录,这样AUTOUIC处理时才能找到ui文件。
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_SOURCE_DIR}/ui)

file(GLOB PRO_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB PRO_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
file(GLOB PRO_UI ${CMAKE_CURRENT_SOURCE_DIR}/*.ui)
file(GLOB PRO_RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.qrc)

add_executable(mouseEvent
    ${PRO_SRC}
    ${PRO_INCLUDE}
    ${PRO_UI}
    ${PRO_RESOURCES}
)

target_link_libraries(mouseEvent PRIVATE Qt5::Widgets)

1.3. main.cpp文件

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

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MouseEvent w;
    w.show();

    return a.exec();
}

1.4. mouseevent.h

#ifndef MOUSEEVENT_H
#define MOUSEEVENT_H

#include <QMainWindow>
#include <QLabel>
#include <QStatusBar>
#include <QMouseEvent>
class MouseEvent : public QMainWindow
{
    Q_OBJECT

public:
    MouseEvent(QWidget *parent = 0);
    ~MouseEvent();
protected:
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
    void mouseDoubleClickEvent(QMouseEvent *e);
private:
    QLabel *statusLabel;
    QLabel *MousePosLabel;
};

#endif // MOUSEEVENT_H

1.5. mouseevent.cpp

#include "mouseevent.h"

MouseEvent::MouseEvent(QWidget *parent)
    : QMainWindow(parent)
{
    setWindowTitle(tr(u8"鼠标事件"));//设置窗体的标题
    statusLabel = new QLabel;//(a)
    statusLabel->setText(tr(u8"当前位置:"));
    statusLabel->setFixedWidth(100);
    MousePosLabel = new QLabel;//(b)
    MousePosLabel->setText(tr(""));
    MousePosLabel->setFixedWidth(100);
    statusBar()->addPermanentWidget(statusLabel);//(c)
    statusBar()->addPermanentWidget(MousePosLabel);
    this->setMouseTracking(true);//(d)
    resize(400,200);
}

void MouseEvent::mousePressEvent(QMouseEvent *e)
{
    QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";//(a)
    if(e->button()==Qt::LeftButton)
    {
        statusBar()->showMessage(tr(u8"左键:")+str);
    }
    else if(e->button()==Qt::RightButton)
    {
        statusBar()->showMessage(tr(u8"右键:")+str);
    }
    else if(e->button()==Qt::MidButton)
    {
        statusBar()->showMessage(tr(u8"中键:")+str);
    }
}

void MouseEvent::mouseMoveEvent(QMouseEvent *e)
{
    MousePosLabel->setText("("+QString::number(e->x())+","+QString::number(e->y())+")");
}

void MouseEvent::mouseReleaseEvent(QMouseEvent *e)
{
    QString str="("+QString::number(e->x())+","+QString::number(e->y()) +")";
    statusBar()->showMessage(tr(u8"释放在:")+str,3000);
}

void MouseEvent::mouseDoubleClickEvent(QMouseEvent *e){}

MouseEvent::~MouseEvent()
{

}

1.6. 生成Visual Studio项目后编译报错

warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

解决办法:warning C4819 解决方案 warning C4819: The file contains a character that cannot be represented in the cu_warning c4819 the file contains a character that c-CSDN博客

修改编码方式为:打开出现warning的文件,Ctrl+A全选,然后在文件菜单:file->advanced save options (高级保存选项),在弹出的选项中选择新的编码方式为:UNICODE codepage 1200 ,点击确定,问题就解决了。

1.7. 界面显示中文乱码问题

如下,在中文前添加u8。(上面列出的代码是修改后的代码)

statusBar()->showMessage(tr(u8"左键:")+str);

运行结果如下:
在这里插入图片描述


原文地址:https://blog.csdn.net/juluwangriyue/article/details/142444282

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!