QTableWidget——编辑单元格

文章目录


前言

熟悉QTableWiget,通过实现单元格的合并、拆分、通过编辑界面实现表格内容及属性的配置、实现表格的粘贴复制功能熟悉QTableWiget的属性

一、单元格的合并、拆分

二、表格内容及属性设置

1.效果图

通过双击单元格进入编辑界面

2.主要代码

编辑界面的代码如下:

cpp 复制代码
void CItemTypeDialog::init()
{
    m_bold = 0;
    m_pFontColorDlg = new  QColorDialog(this);
    mp_bgdColorDialog = new  QColorDialog(this);
    connect(m_pUi->pButtonOk,SIGNAL(clicked()),this,SLOT(onOk()));
    connect(m_pUi->pButtonCancel,SIGNAL(clicked()),this,SLOT(onCancel()));
    connect(m_pUi->pFontColorBtn, &QPushButton::clicked, this, &CItemTypeDialog::showColorDialog);
    connect(m_pUi->pBackgroundBtn, &QPushButton::clicked, this, &CItemTypeDialog::showColorDialog);
    connect(m_pFontColorDlg, &QColorDialog::currentColorChanged, this, &CItemTypeDialog::colorValue);
    connect(mp_bgdColorDialog, &QColorDialog::currentColorChanged, this, &CItemTypeDialog::bgdcolorValue);

    sprintf(m_bgdcolorarr, "#%s%s%s","ff","ff","ff");
    sprintf(m_fontcolorarr, "#%s%s%s","00","00","00");
}

void CItemTypeDialog::setType(TableItemInfor *pItem)
{
    m_pItem = pItem;
    m_pUi->textEdit->setText(m_pItem->data);
    QString str = QString::fromUtf8(m_pItem->style);
    if (!(str.isEmpty()))
    {
        QStringList stylelist = str.split(",");

        m_pUi->pFontComboBox->setCurrentText(stylelist.at(0));//字体
        m_pUi->pFontSizeComboBox->setCurrentText(stylelist.at(1));//字号
        //是否加粗
        m_pUi->boldCheckBox->setChecked(stylelist.at(2).toInt());
        //字体颜色
        strcpy(m_fontcolorarr, stylelist.at(3).toUtf8());
        m_pUi->pFontColorBtn->setStyleSheet(QString::fromUtf8("background-color: %1;").arg(stylelist.at(3)));
        QColor color(stylelist.at(3));
        m_pFontColorDlg->setCurrentColor(color);
        //背景颜色
        strcpy(m_bgdcolorarr, stylelist.at(4).toUtf8());
        m_pUi->pBackgroundBtn->setStyleSheet(QString::fromUtf8("background-color: %1;").arg(stylelist.at(4)));
        QColor bgcolor(stylelist.at(4));
        mp_bgdColorDialog->setCurrentColor(bgcolor);
    }


}
void CItemTypeDialog::showColorDialog()
{
    QToolButton* tb = qobject_cast<QToolButton*>(sender());
    if (tb == m_pUi->pFontColorBtn)
    {
        m_pFontColorDlg->show();
    }
    else if (tb == m_pUi->pBackgroundBtn)
    {
        mp_bgdColorDialog->show();
    }
}
void CItemTypeDialog::onOk()
{
    getStyle();
    accept();
}

void CItemTypeDialog::onCancel()
{
    reject();
}

void CItemTypeDialog::getStyle()
{
    m_pItem->data = m_pUi->textEdit->toPlainText();
    sprintf(m_pItem->style, "%s,%d,%d,%s,%s", m_pUi->pFontComboBox->currentText().toStdString().c_str(),
        m_pUi->pFontSizeComboBox->currentText().toInt(), m_bold, m_fontcolorarr, m_bgdcolorarr);
}

void CItemTypeDialog::colorValue(const QColor& color)
{
    int r, g, b, a;
    color.getRgb(&r, &g, &b, &a);

    QString hexRed = QString::number(r, 16);
    if (hexRed == "0")
    {
        hexRed = "00";
    }

    QString hexGreen = QString::number(g, 16);
    if (hexGreen == "0")
    {
        hexGreen = "00";
    }

    QString hexBlue = QString::number(b, 16);
    if (hexBlue == "0")
    {
        hexBlue = "00";
    }

    sprintf(m_fontcolorarr, "#%s%s%s", hexRed.toStdString().c_str(), hexGreen.toStdString().c_str(), hexBlue.toStdString().c_str());
    m_pUi->pFontColorBtn->setStyleSheet(QString::fromUtf8("background-color: %1;").arg(QString::fromUtf8(m_fontcolorarr)));
}

void CItemTypeDialog::bgdcolorValue(const QColor &color)
{
    int r, g, b, a;
    color.getRgb(&r, &g, &b, &a);
    QString hexRed = QString::number(r, 16);
    if (hexRed == "0")
    {
        hexRed = "00";
    }

    QString hexGreen = QString::number(g, 16);
    if (hexGreen == "0")
    {
        hexGreen = "00";
    }

    QString hexBlue = QString::number(b, 16);
    if (hexBlue == "0")
    {
        hexBlue = "00";
    }
    sprintf(m_bgdcolorarr, "#%s%s%s", hexRed.toStdString().c_str(), hexGreen.toStdString().c_str(), hexBlue.toStdString().c_str());
    m_pUi->pBackgroundBtn->setStyleSheet(QString::fromUtf8("background-color: %1;").arg(QString::fromUtf8(m_bgdcolorarr)));
}

三、表格的粘贴复制功能

功能待完善!

相关推荐
凤城老人1 天前
C++使用拉玛努金公式计算π的值
开发语言·c++·算法
HAH-HAH1 天前
【Python 入门】(2)Python 语言基础(变量)
开发语言·python·学习·青少年编程·个人开发·变量·python 语法
递归不收敛1 天前
一、Java 基础入门:从 0 到 1 认识 Java(详细笔记)
java·开发语言·笔记
zhangfeng11331 天前
win7 R 4.4.0和RStudio1.25的版本兼容性以及系统区域设置有关 导致Plots绘图面板被禁用,但是单独页面显示
开发语言·人工智能·r语言·生物信息
姓刘的哦1 天前
Qt中的QWebEngineView
数据库·c++·qt
SundayBear1 天前
QT零基础入门教程
c++·qt
子午1 天前
Python的uv包管理工具使用
开发语言·python·uv
HMBBLOVEPDX1 天前
C++(静态函数)
开发语言·c++
dpxiaolong1 天前
RK3588 Android12默认移除导航栏
开发语言·python
Pocker_Spades_A1 天前
Python快速入门专业版(二十九):函数返回值:多返回值、None与函数嵌套调用
服务器·开发语言·python