123444444

该代码段定义了一个名为Solution的类,包含一个textDataTrans方法。此方法接收一个字符串数组,处理每个字符串(移除“line”和“col”),并将数据解析成一个最大行数为maxLine,最大列数为maxCol的二维字符串矩阵。处理后的数据以表格形式返回,用竖线分隔单元格并添加了边界线。
class Solution {
    String[] textDataTrans(String[] datas) {
        int maxLine = 0;
        int maxCol = 0;
        List<String> converts = new ArrayList<>();
        for (String data : datas) {
            data = data.replace("line", "");
            data = data.replace("col", "");
            converts.add(data);
            String[] s = data.split(" ");
            if (maxLine < Integer.parseInt(s[0])) {
                maxLine = Integer.parseInt(s[0]);
            }
            if (maxCol < Integer.parseInt(s[1])) {
                maxCol = Integer.parseInt(s[1]);
            }
        }
        String[][] as = new String[maxLine][maxCol];
        for (String convert : converts) {
            String[] s = convert.split(" ");
            int line = Integer.parseInt(s[0]) - 1;
            int col = Integer.parseInt(s[1]) - 1;
            if (s.length == 3) {
                as[line][col] = s[2];
            } else {
                as[line][col] = " ";
            }
        }

        String[] result = new String[maxLine + 2];
        StringBuilder lineStart = new StringBuilder("+");
        for (int i = 0; i < maxCol; i++) {
            lineStart.append("---+");
        }
        result[0] = lineStart.toString();
        result[maxLine + 1] = lineStart.toString();

        for (int i = 0; i < maxLine; i++) {
            StringBuilder line = new StringBuilder("|");
            for (int j = 0; j < maxCol; j++) {
                if (as[i][j] == null) {
                    line.append("   |");
                } else {
                    line.append(" ").append(as[i][j]).append(" |");
                }
            }
            result[i + 1] = line.toString();
        }
        return result;
    }
}

#include <QPainter> #include "PageItemDelegate.h" const static QColor background_color_selected = QColor(30, 31, 34); const static QColor background_color_hover = QColor(51, 51, 51); const static QColor foreground_color_normal = QColor(162, 169, 172); const static QColor foreground_color_pressed = QColor(238, 238, 238); static void drawDelegateBackground(QPainter* painter, const QStyleOptionViewItem& option) { bool bSelected = option.state.testFlag(QStyle::State_Selected); bool bHover = option.state.testFlag(QStyle::State_MouseOver); painter->save(); painter->setPen(Qt::NoPen); if (bSelected) { painter->setBrush(background_color_selected); } else { if (bHover) { painter->setBrush(background_color_hover); } else { painter->setBrush(Qt::NoBrush); } } painter->drawRect(option.rect); painter->restore(); } static void drawDelegateText(QPainter* painter, const QRectF& rect, const QString& text, int alignment) { painter->drawText(rect, Qt::AlignCenter, text); } static void drawDelegateText(QPainter* painter, const QStyleOptionViewItem& option, const QRectF& rect, const QString& text, int alignment) { bool bSelected = option.state.testFlag(QStyle::State_Selected); painter->save(); QPen pen(bSelected ? foreground_color_pressed : foreground_color_normal); painter->setBrush(Qt::NoBrush); painter->setPen(pen); drawDelegateText(painter, rect, text, alignment); painter->restore(); } void PageItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { if (!index.isValid()) return; drawDelegateBackground(painter, option); painter->save(); bool bHover = option.state.testFlag(QStyle::State_MouseOver); bool bSelected = option.state.testFlag(QStyle::State_Selected); QRect imageRect = option.rect; imageRect.setSize(QSize(30, 30)); QRect itemRect = option.rect; QPen pen((bHover || bSelected) ? QColor(238, 238, 238) : QColor(162, 169, 172)); painter->setBrush(Qt::NoBrush); painter->setPen(pen); QFont font = qApp->font(); font.setPixelSize(18); font.setBold(true); painter->setFont(font); QPixmap pixmap = qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)); if (bHover || bSelected) { painter->drawPixmap(imageRect, pixmap); } else { painter->drawPixmap(imageRect, pixmap); } QString strText = index.data(Qt::DisplayRole).toString(); drawDelegateText(painter, itemRect, strText, index.data(Qt::TextAlignmentRole).toInt()); //QFontMetrics fm = painter->fontMetrics(); //int width = fm.horizontalAdvance("123444444"); //if (width > pathRect.width()) { // int elidedWidth = fm.horizontalAdvance("..."); // QString strElidedText = fm.elidedText("123444444", Qt::ElideMiddle, pathRect.width() - elidedWidth); // painter->drawText(pathRect, Qt::AlignVCenter | Qt::AlignLeft, strElidedText); //} //else { // painter->drawText(pathRect, Qt::AlignVCenter | Qt::AlignLeft, "123444444"); //} painter->restore(); }这个是我的模版帮我改一下
03-14
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值