Mac下使用CLion+Qt+MySQL实现一个推箱子游戏(一)——环境配置

2 篇文章 0 订阅
2 篇文章 0 订阅

前言:

        此版本的推箱子游戏为上一个版本的升级,通过数据库实现了用户的登陆和注册,并且实现了关卡的跳转,将游戏中的玩家数据和地图数据存入数据库中.实现效果为:

(上一个版本链接:Qt实现简单的推箱子游戏_supermario19的博客-CSDN博客_qt推箱子)

 

 

 

一. 环境配置

1. 下载CLion

        这个没什么难度,Mac下CLion默认是Clang编译器,如果想换到GCC编译器的同学可以参考这一篇文章:

CLion for Mac(m1)配置GCC编译器_supermario19的博客-CSDN博客

2. MySQL的下载与配置

        可以参考这一篇文章: 

Mac(m1)下载MySQL+DataGrip配置_supermario19的博客-CSDN博客

3. Qt的下载

        1. 打开homebrew

        2. 命令行输入

brew install qt

4. 找到自己安装MySQL, Qt的路径

        如果有同学和我用的都是m1的mac, 那么路径应该会和我差不多

        m1的电脑通过homebrew下载MySQL和Qt这些东西时,都会存放在一个固定路径:

        /opt/homebrew/Cellar/, 如果是非m1的话路径好像有点不一样, 是在/usr/local/Cellar/下

         记住自己的几个路径:(以下是我的,你们的可能会和我有些不同)

        mysql的include路径: /opt/homebrew/Cellar/mysql/8.0.26/include/mysql

        mysql的lib路径: /opt/homebrew/Cellar/mysql/8.0.26/lib

        qt的include路径 : /opt/homebrew/Cellar/qt/6.1.3/include

        qt的lib路径 :/opt/homebrew/Cellar/qt/6.1.3/lib

5. 创建一个新的CLion的Qt控制台可执行文件

 

6. 修改CMakeLists.txt文件

cmake_minimum_required(VERSION 3.19)
include_directories("/opt/homebrew/Cellar/mysql/8.0.26/include/mysql")
include_directories("/opt/homebrew/Cellar/qt/6.1.3/include")
link_directories("/opt/homebrew/Cellar/mysql/8.0.26/lib")
link_directories("/opt/homebrew/Cellar/qt/6.1.3/lib")

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

find_package(Qt6Core REQUIRED)
find_package(Qt6Widgets REQUIRED)
find_package(Qt6Quick REQUIRED)


file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp")
foreach(file ${files})
    get_filename_component(name ${file} NAME)
    add_executable(${name} ${file} MainWindow.cpp)
    target_link_libraries(${name} libmysqlclient.dylib)
    target_link_libraries(${name} Qt6::Core)
    target_link_libraries(${name} Qt6::Widgets)
    target_link_libraries(${name} Qt6::Quick)
endforeach()


        ps:(2-5行include的路径就改为第4步中的include路径, lib路径就改为第四步中的lib路径)

7.新建一个Qt UI 类, 写几行测试代码

mainwindow.h

//
// Created by supermario on 2021/9/24.
//

#ifndef TEST__MAINWINDOW_H_
#define TEST__MAINWINDOW_H_

#include <QWidget>
#include <QPushButton>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QWidget {
 Q_OBJECT

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

 private:
  Ui::MainWindow *ui;
  QPushButton *button;
};

#endif //TEST__MAINWINDOW_H_

mainwindow.cpp

//
// Created by supermario on 2021/9/24.
//

// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved

#include "mainwindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  button = new QPushButton();
}

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

 main.cpp

#include <QApplication>
#include <QDebug>
#include "mainwindow.h"

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);
  qDebug() << "Hello World";
  MainWindow main_window;
  main_window.show();
  return a.exec();
}

 结果:

 

8. 测试一下数据库连接

创建一个C++的普通类(并非Qt)

Database.h

//
// Created by supermario on 2021/9/24.
//

#ifndef TEST__DATABASE_H_
#define TEST__DATABASE_H_

#include "mysql.h"
#include <string>

class Database {
 public:
  Database();
  ~Database();
  // 单例模式
  static Database *Instance();
  // 初始化数据库连接
  bool init_database();
  // 销毁数据库连接
  void close_database();
 private:
  MYSQL *mysql;
  MYSQL_RES *result{};
};

#endif //TEST__DATABASE_H_

Database.cpp

//
// Created by supermario on 2021/9/24.
//

#include "Database.h"
#include <QDebug>

Database::Database() {
  mysql = new MYSQL;
}

Database::~Database() = default;

Database* Database::Instance() {
  static Database db;
  qDebug() << &db;
  return &db;
}

bool Database::init_database() {
  mysql_init(mysql);
  mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "utf8");
  if (mysql_real_connect(mysql, "127.0.0.1", "root", "Lh19980402@", "test_db", 3306, nullptr, 0) == nullptr ){
    qDebug() << "连接失败";
    qDebug() << "错误原因:" << mysql_error(mysql);
    return false;
  }
  return true;
}

void Database::close_database() {
  mysql_free_result(result);
  mysql_close(mysql);
}

 myql_real_connect中参数, 127.0.0.1为本地地址, 后面依次为用户名, 密码, 数据库名称, 端口号

改写Mainwindow.cpp

//
// Created by supermario on 2021/9/24.
//

// You may need to build the project (run Qt uic code generator) to get "ui_MainWindow.h" resolved

#include "mainwindow.h"
#include "ui_MainWindow.h"
#include "Database.h"

MainWindow::MainWindow(QWidget *parent) :
    QWidget(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  button = new QPushButton();
  bool ret = Database::Instance()->init_database();
  qDebug() << ret;
}

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

得到结果:

环境配置结束!!! 

 

 

 

 

        

        

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值