c++qt

1.显示画布

#include "code.h"
#include <QtWidgets/QApplication>
#include<iostream>
#include<vector>
#include <QWindow>
#include <QGraphicsView>
#include <QGraphicsScene>



using namespace std;



//1.空格  2.墙  3.入口  4.终点  5.双入口
/*
1.入口
2.出口
3.出入口
4.下一个任务迷宫
5.没有障碍
6 有障碍
7.玩家位置
*/


class Maze {
public:
    int row, col;//迷宫的行数和列数
    int layer;//所属层
    Maze();
};

Maze::Maze() {
    this->row = row;
    this->col = col;
}

class MazeManager { //迷宫管理器
public:
    int row;//玩家位置
    int col;//玩家位置
    MazeManager();
    vector<Maze> mazes; //存储多个迷宫
};


MazeManager::MazeManager() {

}


class Canvas : public QGraphicsView {
public:
     Canvas(QWidget* parent = nullptr);
    
private:
    QGraphicsScene* scene;
};

class MainWindow : public QMainWindow
{
public:
    MainWindow(QWidget* parent = nullptr);
    void cnavasInit();
    Canvas* canvas; // 指向 Canvas 的指针
};


void MainWindow::cnavasInit() {
    canvas = new Canvas(this); // 创建 Canvas 对象
    canvas->setGeometry(50, 50, 800, 800); //     // 设置 Canvas 的绝对位置和大小
    canvas->show();

}

Canvas::Canvas(QWidget* parent)
    : QGraphicsView(parent), scene(new QGraphicsScene(this)) {
    // 设置背景颜色为白色
    setBackgroundBrush(Qt::white);
    // 设置场景
    setScene(scene);
    setRenderHint(QPainter::Antialiasing); // 开启抗锯齿
    setSceneRect(0, 0, 800*2, 800*2); // 设置场景大小
    // 添加一些图形项到场景
    scene->addRect(50, 50, 200, 100, QPen(Qt::blue), QBrush(Qt::cyan)); // 矩形
    scene->addEllipse(300, 50, 100, 100, QPen(Qt::red), QBrush(Qt::yellow)); // 椭圆
    scene->addLine(50, 200, 300, 200, QPen(Qt::green)); // 线条
}


MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    // 设置主窗口标题
    setWindowTitle("迷宫");
    this->resize(1600, 900);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow* mainWindow = new MainWindow();
    mainWindow->cnavasInit(); //显示画布
    mainWindow->show();
    return a.exec();
}

2.按钮函数

QString getStyle(int r,int g,int b) {
    QString styleSheet = QString(
        "QPushButton {"
        "background-color: rgb(%1, %2, %3);"
        "color: black;" // 设置字体颜色为白色
        "font-weight: bold;" // 设置字体加粗
        "border: 1px solid black;" // 设置黑色边框,宽度为2像素
        "border-radius: 8px;" // 设置圆角边框,半径为8像素
        "}"
        "QPushButton:hover {"
        "background-color: rgb(128, 0, 128);" // 鼠标悬停时为紫色
        "}"
        "QPushButton:pressed {"
        "background-color: rgb(255, 0, 0);" // 鼠标点击时为红色
        "}"
    ).arg(r).arg(g).arg(b);
    return styleSheet;
}





QPushButton* getButton(QWidget* parent, QString message,int r,int g,int b,int x,int y) {
    QPushButton* button = new QPushButton(message, parent);
    button->setGeometry(x, y, 100, 40);
    QFont font("微软雅黑", 12); // 字体名称和大小
    button->setFont(font);
    button->setStyleSheet(getStyle(r,g,b));
    return button;
}

2.自定义按钮类

QString getStyle(int r,int g,int b) {
    QString styleSheet = QString(
        "QPushButton {"
        "background-color: rgb(%1, %2, %3);"
        "color: black;" // 设置字体颜色为白色
        "font-weight: bold;" // 设置字体加粗
        "border: 1px solid black;" // 设置黑色边框,宽度为2像素
        "border-radius: 8px;" // 设置圆角边框,半径为8像素
        "}"
        "QPushButton:hover {"
        "background-color: rgb(128, 0, 128);" // 鼠标悬停时为紫色
        "}"
        "QPushButton:pressed {"
        "background-color: rgb(255, 0, 0);" // 鼠标点击时为红色
        "}"
    ).arg(r).arg(g).arg(b);
    return styleSheet;
}
QPushButton* getButton(QWidget* parent, QString message,int r,int g,int b,int x,int y) {
    QPushButton* button = new QPushButton(message, parent);
    button->setGeometry(x, y, 100, 40);
    QFont font("微软雅黑", 12); // 字体名称和大小
    button->setFont(font);
    button->setStyleSheet(getStyle(r,g,b));
    return button;
}

3.编码格式转换

QString asciitoqstring(string str) {
    return QString::fromLocal8Bit(str);
}
//str编码为utf-8 
QString utf8toQString(string str) {
    QString qstr = QString::fromUtf8(str.c_str());
    return qstr;
}

输入提示初始化

void labelAndInputInit(QLabel* label, QLineEdit* inputBox, QWidget *parent,QString  text, int x, int y) {
    // 设置标签的文本和父窗口
       // 创建字体对象,并设置为微软雅黑和加粗
    QFont font("微软雅黑", 12, QFont::Bold); // 设置为微软雅黑,大小为12,加粗
    // 应用字体到标签和输入框
    label->setFont(font);
    inputBox->setFont(font);
    label->setText(text);
    label->setParent(parent);
    // 计算文本宽度
    QFontMetrics metrics(label->font());
    int textWidth = metrics.horizontalAdvance(text);
    // 设置标签的位置
    label->setGeometry(10, y, textWidth + 10, 40); // +10 留一些边距
    // 设置输入框的位置,x 坐标在标签的右侧
    inputBox->setParent(parent);
  //  inputBox->setGeometry(textWidth + 20, y, 300, 40); // +20 留一些边距
    inputBox->setGeometry(60, y, 300, 40); // +20 留一些边距
}

显示提示信息


void showWarn(QWidget* parent,QString m,QString m1) {
    QMessageBox::warning(parent, m, m1);
}
void showInformation(QWidget* parent, QString m, QString m1) {
    QMessageBox::information(parent, m, m1);
}

表格

class RoundedTableWidget : public QTableWidget {
public:
    RoundedTableWidget(QWidget* parent = nullptr) : QTableWidget(parent) {
        // 设置样式表以实现圆角效果
        setStyleSheet("QTableWidget {"
            "border: 1px solid #e6e7e8;" // 边框
            "border-radius: 10px;" // 圆角半径
            "padding: 5px;" // 内边距
            "background-color: white;" // 表格背景颜色
            "}"
            "QHeaderView::section {"
            "background-color: #169efa;" // 表头背景颜色
            "color: white;" // 表头字体颜色
            "font-weight: bold;" // 表头字体加粗
          //  "border: 1px solid black;" // 表头边框
          //  "border-radius: 10px;" // 表头圆角
            "}");


    }
};


RoundedTableWidget* getQTableWidget(QWidget* parent ,vector<string> attributes) {
    // 创建一个新的 QTableWidget 实例
    RoundedTableWidget* tableWidget = new RoundedTableWidget(parent);
    // 设置列数为 attributes 的大小
    int columnCount = attributes.size();
    tableWidget->setColumnCount(columnCount);
    // 将 attributes 转换为 QStringList 并设置为表头标签
    QStringList headers;
    for (const auto& attr : attributes) {
        headers << QString::fromStdString(attr);
    }
    tableWidget->setHorizontalHeaderLabels(headers);
    return tableWidget; // 返回 QTableWidget 指针

}


QTableWidgetItem* getItem(string data) {
    QTableWidgetItem* isbnItem = new QTableWidgetItem(QString::fromStdString(data));
    isbnItem->setFlags(isbnItem->flags() & ~Qt::ItemIsEditable); // 设置为不可编辑
    return isbnItem;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超维Ai编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值