如何让qt tableView每个item中个别字用不同颜色显示?

这篇具有很好参考价值的文章主要介绍了如何让qt tableView每个item中个别字用不同颜色显示?。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

如何让qt tableView每个item中个别字用不同颜色显示?

如何让qt tableView每个item中个别字用不同颜色显示?,qt,qt,数据库,开发语言
从上面图片可以看到,Item为红色,数字5为黑色。

要实现在一个控件实现不同颜色,目前想到的只有QTextEdit 、QLabel。有两种方法,第一种是代理,第二种是通过setIndexWidget函数实现。

    QString abc("<span style=\"color: red;\">");
    abc.append("Item");
    abc.append("</span>");
    abc.append("5");
    QTextEdit *text = new QTextEdit();
    text->setText(abc);

QTextEdit 可以实现多种样式,字体,字号,加粗,倾斜,下划线都可以实现。

第一种方法

写一个自定义代理类,继承QStyledItemDelegate类,重写paint,sizeHint方法。运用QAbstractItemView的三个方法设置代理。

QAbstractItemView::setItemDelegate
QAbstractItemView::setItemDelegateForColumn
QAbstractItemView::setItemDelegateForRow

例子文章来源地址https://www.toymoban.com/news/detail-686617.html

class MyDelegatel : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit MyDelegatel(QObject *parent = nullptr);

    //自定义代理必须重新实现以下4个函数

    //创建编辑组件
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index)const override;

    //从数据模型获取数据,显示到代理组件中
    void setEditorData(QWidget *editor, const QModelIndex &index)const override;

    //将代理组件的数据,保存到数据模型中
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index)const override;

    //更新代理编辑组件的大小
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
                              const QModelIndex &index)const override;


    // QAbstractItemDelegate interface
public:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, 
    	const QModelIndex &index) const override;
    QSize sizeHint(const QStyleOptionViewItem &option, 
    	const QModelIndex &index) const override;
};

paint方法

如何让qt tableView每个item中个别字用不同颜色显示?,qt,qt,数据库,开发语言

This pure abstract function must be reimplemented if you want to provide custom rendering. Use the painter and style option to render the item specified by the item index.
如果要提供自定义呈现,则必须重新实现此纯抽象函数。使用painter和style选项可以渲染由项目索引指定的项目。
If you reimplement this you must also reimplement sizeHint().
如果重新实现此操作,则还必须重新实现sizeHint()。
例子:

void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                         const QModelIndex &index) const
{
    if (index.data().canConvert<StarRating>()) {
        StarRating starRating = qvariant_cast<StarRating>(index.data());

        if (option.state & QStyle::State_Selected)
            painter->fillRect(option.rect, option.palette.highlight());

        starRating.paint(painter, option.rect, option.palette,
                         StarRating::EditMode::ReadOnly);
    } else {
        QStyledItemDelegate::paint(painter, option, index);
    }
}

例子来自官方qt6\Examples\Qt-6.5.2\widgets\itemviews\stardelegate\stardelegate.cpp

sizeHint方法

如何让qt tableView每个item中个别字用不同颜色显示?,qt,qt,数据库,开发语言
This pure abstract function must be reimplemented if you want to provide custom rendering. The options are specified by option and the model item by index.
如果要提供自定义呈现,则必须重新实现此纯抽象函数。选项由选项指定,模型项由索引指定。
If you reimplement this you must also reimplement paint().
如果你重新实现这个,你也必须重新实现paint()。

例子:

QSize StarDelegate::sizeHint(const QStyleOptionViewItem &option,
                             const QModelIndex &index) const
{
    if (index.data().canConvert<StarRating>()) {
        StarRating starRating = qvariant_cast<StarRating>(index.data());
        return starRating.sizeHint();
    }
    return QStyledItemDelegate::sizeHint(option, index);
}

第二种方法

通过setIndexWidget函数实现
如果是QtableWidget非常简单,写好一个widget,调用setCellWidget方法设置就可以了。

// 创建按钮
ui->tableWidget->setCellWidget(rowIndex,6,Widget_btn);//表格中添加Widget

看到QtableWidget有一个setCellWidget方法,我在想,tableView是否有也类似的方法。好在tableView也提供了类似的方法,方法隐在父类QAbstractItemView里。

void QAbstractItemView::setIndexWidget(const QModelIndex &index, QWidget *widget)

Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.
在给定索引的项目上设置给定的小部件,将小部件的所有权传递给视口。

If index is invalid (e.g., if you pass the root index), this function will do nothing.
如果索引无效(例如,如果传递根索引),此函数将不起任何作用。

The given widget’s autoFillBackground property must be set to true, otherwise the widget’s background will be transparent, showing both the model data and the item at the given index.
给定小部件的autoFillBackground属性必须设置为true,否则小部件的背景将是透明的,显示给定索引处的模型数据和项。

If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
如果用索引小部件B替换索引小部件A,则索引小部件将被删除。例如,在下面的代码片段中,QLineEdit对象将被删除。

 setIndexWidget(index, new QLineEdit);
 ...
 setIndexWidget(index, new QTextEdit);

This function should only be used to display static content within the visible area corresponding to an item of data.
If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate instead.
此功能应仅用于在与数据项相对应的可见区域内显示静态内容。
See also indexWidget() and Delegate Classes.
如果要显示自定义动态内容或实现自定义编辑器小部件,请改为使用子类QStyledItemDelegate。

例子

Widget::Widget(QWidget *parent): QWidget(parent) , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QTableView *tableView = ui->tableView;
    QStandardItemModel *model = new QStandardItemModel();
    tableView->setModel(model);
    // Create and populate QStandardItem objects
    QStandardItem *item1 = new QStandardItem("Item 1");
    QStandardItem *item2 = new QStandardItem("Item 2");
    // Add child items to item1
    item1->appendRow(new QStandardItem("Child 1"));
    item1->appendRow(new QStandardItem("Child 2"));

    QStandardItem *item3 = new QStandardItem();
    QString abc("<span style=\"color: red;\">");
    abc.append("Item");
    abc.append("</span>");
    abc.append("5");
    QTextEdit *text = new QTextEdit();
    text->setText(abc);
    text->setFrameShape(QFrame::NoFrame);
    text->setFocusPolicy(Qt::ClickFocus);
    text->setReadOnly(true);
    text->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);// 设置水平滚动条按需显示
    text->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);// 设置垂直滚动条不显示


    // Add items to the model
    model->appendRow(item1);
    model->appendRow(item2);
    model->appendRow(item3);

    tableView->setIndexWidget(model->index(model->rowCount()-1,0),text);
}

到了这里,关于如何让qt tableView每个item中个别字用不同颜色显示?的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • qt表格可修改不可修改(qt tableview修改表格内容)

    QTableWidget是QT程序中常用的显示数据表格的控件.qt表格可修改不可修改. 不管是在金融app,还是医疗机械,还是智慧农业,QTableWidget无处不在. QTableWidget的效果大概如下图: 好了,接下来就来教大家如何使用QTableWidget. 首先,它的位置控件在: 注意,不要拖错控件,很多同学容易犯的错误

    2024年02月09日
    浏览(29)
  • qt设置tableview单元大小跟随窗口变化

    设置效果过于离奇。 右侧为代码设置显示效果

    2024年02月11日
    浏览(31)
  • C++ Qt开发:TableView与TreeView组件联动

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍 TableView 与 TreeView 组件联动的常用方法及灵活运用。 本章我们继续实现表格的

    2024年02月04日
    浏览(30)
  • C++ Qt开发:数据库与TableView多组件联动

    Qt 是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍 TableView 组件与数据库联动的常用方法及灵活运用。 在Qt中,通常我们不会在

    2024年02月04日
    浏览(40)
  • Qt中的 tableView 设置 二进制 十六进制 序号表头

    因为QTableView的垂直表头并不支持使用委托来自定义。 相反,可以通过将自定义的QWidget作为QHeaderView的标签来实现这一目标。 代码: 在这个示例中,自定义了BinaryHeaderView类,继承自QHeaderView, 重写了paintSection方法来绘制二进制序列。然后,将这个自定义的垂直表头应用到了

    2024年04月27日
    浏览(30)
  • 更改el-select-dropdown_item selected选中颜色

    更改el-select-dropdown_item selected选中颜色 默认为element主题色 在修改element select下拉框选中颜色时会发现不生效,原因是:el-select下拉框插入到了body中 解决办法: 在select标签里填写:popper-append-to-body=\\\"false\\\"属性,禁止select下拉框插入到body中 然后更改选中元素的颜色,就可以了

    2024年02月16日
    浏览(45)
  • css:如何通过不同的值,改变盒子的样式和字体颜色通过computed而不是v-if

    在使用uniapp编写功能时,可以通过computed方法来实现根据num这个值也可以是后端传过来的值只要是number类型都可以。不同取值来修改盒子的背景颜色和字体颜色。首先,在data中定义一个num来存储当前的值,然后在computed中创建一个样式对象,并根据num的取值来设置相应的背景

    2024年02月08日
    浏览(39)
  • QT如何实现不同窗口之间的通信

    方法: 用qt的信号与槽来实现   1,首先,对发送信号的窗口,自定义信号,和槽函数。   如:From1   signals:     void  SendData (QString s);  //信号   private slots: void  SendSlot (); //传递信号的函数,这个函数的主要功能就是,发送 SendData 这个信号     //槽函数实现 void Form1:: S

    2024年02月09日
    浏览(28)
  • 画tsne-2d图,为每个类设置指定的颜色与标签,并显示标签

    *********************************原创不易,转载请注明出处!********************************* 功能要求:         1. 画一个关于5个类的2维tsne图;         2. 给每个类指定自定义的颜色与名字:                 颜色:                  hex = [ \\\"#c957db\\\" , \\\"#dd5f57\\\" , \\\"#b9db57\\\" , \\\"#57db3

    2024年02月05日
    浏览(19)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包