Qt,如何更改 QComboBox 的一项的文本颜色? (C++)

发布于 2024-09-06 12:33:06 字数 280 浏览 1 评论 0原文

我不知道如何更改 QComboBox 的一项特定项目的文本颜色。我能够更改项目的背景颜色:(

comboBox->setItemData(i, Qt::green, Qt::BackgroundRole);

Qt::ForegroundRole 根本没有效果,Qt 4.6,Ubuntu 10.04)

并且我能够使用以下命令更改所有项目的文本颜色样式表,但我不知道如何更改一个指定项目的文本颜色。

感谢您的帮助!

I cannot figure out how to change the text color of one particular item of a QComboBox. I was able to change the Background color of an item:

comboBox->setItemData(i, Qt::green, Qt::BackgroundRole);

(Qt::ForegroundRole had no effect at all, Qt 4.6, Ubuntu 10.04)

and I was able to change the text color of all items with a stylesheet but I cannot figure out how to change the text color of one specified item.

Thanks for your Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染年凉城似染瑾 2024-09-13 12:33:06

这几乎就像您提议的那样,但是您必须将角色更改为Qt::TextColorRole

comboBox->setItemData(0, QBrush(Qt::red), Qt::TextColorRole);  

It's almost like you propose, but you have to change the role to Qt::TextColorRole.

comboBox->setItemData(0, QBrush(Qt::red), Qt::TextColorRole);  
许你一世情深 2024-09-13 12:33:06

我从未尝试过这样做,但我想唯一的方法就是编写自己的模型,继承 QAbstractListModel ,重新实现 rowCount() 和 data (),您可以在其中设置每个项目的颜色(使用 TextColorRole 角色)。

然后,使用 QComboBox::setModel() 使 QComboBox 显示它。

更新

我能够使用上述解决方案做你想做的事情。这是一个简单的例子。

我创建了自己的列表模型,继承了 QAbstractListModel

class ItemList : public QAbstractListModel
{
   Q_OBJECT
public:
   ItemList(QObject *parent = 0) : QAbstractListModel(parent) {}

   int rowCount(const QModelIndex &parent = QModelIndex()) const { return 5; }
   QVariant data(const QModelIndex &index, int role) const {
      if (!index.isValid())
          return QVariant();

      if (role == Qt::TextColorRole)
         return QColor(QColor::colorNames().at(index.row()));

      if (role == Qt::DisplayRole)
          return QString("Item %1").arg(index.row() + 1);
      else
          return QVariant();
   }
};

现在可以轻松地将这个模型与组合框一起使用:

comboBox->setModel(new ItemList);

我尝试过,它工作正常。

I never tried to do it, but I guess the only way to do it would be to write your own model, inheriting QAbstractListModel, reimplementing rowCount()and data() where you can set the color for each item (using the TextColorRole role).

Then, use QComboBox::setModel() to make the QComboBox display it.

UPDATE

I was able to do what you want using the above solution. Here is a simple example.

I created my own list model, inheriting QAbstractListModel :

class ItemList : public QAbstractListModel
{
   Q_OBJECT
public:
   ItemList(QObject *parent = 0) : QAbstractListModel(parent) {}

   int rowCount(const QModelIndex &parent = QModelIndex()) const { return 5; }
   QVariant data(const QModelIndex &index, int role) const {
      if (!index.isValid())
          return QVariant();

      if (role == Qt::TextColorRole)
         return QColor(QColor::colorNames().at(index.row()));

      if (role == Qt::DisplayRole)
          return QString("Item %1").arg(index.row() + 1);
      else
          return QVariant();
   }
};

It is now easy to use this model with the combo box :

comboBox->setModel(new ItemList);

I tried it and it's working fine.

明媚如初 2024-09-13 12:33:06

不要认为这是解决方案,但是,如果它很方便,在某些情况下您可以将 QPixmap-s 用于组合框。看一下 QComboBox::insertItem 方法。

Don't think that this is the solution, but, if it is handy, in some cases you could use QPixmap-s for your combo box. Take a look at QComboBox::insertItem methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文