QComboBox下拉列表-设置选中项样式

发布于 2024-12-19 18:42:57 字数 41 浏览 0 评论 0原文

是否可以设置QComboBox下拉列表的所选项目样式(Qt样式表)?

Is it possible to set selected item style (Qt style sheet) of the QComboBox drop-down list?

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

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

发布评论

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

评论(3

尝蛊 2024-12-26 18:42:57

解决方案是

  • 创建一个 ListView 对象,
  • 设置其样式表,
  • 将其用作 ComboBox 的视图,

方法如下:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QMainWindow * mainWindow = new QMainWindow();
    QComboBox * combo = new QComboBox(mainWindow);
    QListView * listView = new QListView(combo);
    combo->addItem("foo");
    combo->addItem("bar");
    combo->addItem("foobar");
    combo->addItem("fooooo");

    listView->setStyleSheet("QListView::item {                              \
                             border-bottom: 5px solid white; margin:3px; }  \
                             QListView::item:selected {                     \
                             border-bottom: 5px solid black; margin:3px;    \
                             color: black;                                  \
                            }                                               \
                            ");
    combo->setView(listView);


    mainWindow->show();
    app.exec();

    return 0;
    }

备注:
我认为,根据 Qt 文档 应用此风格也应该有效……但事实并非如此。

QComboBox QAbstractItemView::item {
    border-bottom: 5px solid white; margin:3px;
}
QComboBox QAbstractItemView::item:selected {
    border-bottom: 5px solid black; margin:3px;
}

The solution is to

  • create a ListView object
  • set its stylesheet
  • use it as the view of the ComboBox

Here is how:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QMainWindow * mainWindow = new QMainWindow();
    QComboBox * combo = new QComboBox(mainWindow);
    QListView * listView = new QListView(combo);
    combo->addItem("foo");
    combo->addItem("bar");
    combo->addItem("foobar");
    combo->addItem("fooooo");

    listView->setStyleSheet("QListView::item {                              \
                             border-bottom: 5px solid white; margin:3px; }  \
                             QListView::item:selected {                     \
                             border-bottom: 5px solid black; margin:3px;    \
                             color: black;                                  \
                            }                                               \
                            ");
    combo->setView(listView);


    mainWindow->show();
    app.exec();

    return 0;
    }

Remark:
I think, according to the Qt docs applying this style should also work...but it does not.

QComboBox QAbstractItemView::item {
    border-bottom: 5px solid white; margin:3px;
}
QComboBox QAbstractItemView::item:selected {
    border-bottom: 5px solid black; margin:3px;
}
唐婉 2024-12-26 18:42:57

如果您的意思是希望当组合框显示其元素时(即处于“下拉”状态),所选项目显示不同,您可以更改调色板中突出显示和突出显示文本的颜色,或设置内部 QAbstractItemView

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QComboBox cb;
  cb.addItem("Item 1");
  cb.addItem("Item 2");
  cb.addItem("Item 3");
  cb.show();

  QPalette p = cb.palette();
  p.setColor(QPalette::HighlightedText, QColor(Qt::red));
  p.setColor(QPalette::Highlight, QColor(Qt::green));
  cb.setPalette(p);

  // OR ...
  // cb.setStyleSheet("QComboBox QAbstractItemView { "
  //                  "selection-background-color: green; "
  //                  "selection-color: red; }");

  return app.exec();
}

如果您只是指处于“折叠”状态的元素的样式,我会看一下“自定义 QComboBox" Qt 样式表参考示例部分关于你正在尝试做的事情。

If you mean you want the selected item to appear different when the combo box is showing its elements (i.e. in the "dropped down" state), you can change the colors for the Highlight and HighlightedText in the palette, or style the inner QAbstractItemView

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QComboBox cb;
  cb.addItem("Item 1");
  cb.addItem("Item 2");
  cb.addItem("Item 3");
  cb.show();

  QPalette p = cb.palette();
  p.setColor(QPalette::HighlightedText, QColor(Qt::red));
  p.setColor(QPalette::Highlight, QColor(Qt::green));
  cb.setPalette(p);

  // OR ...
  // cb.setStyleSheet("QComboBox QAbstractItemView { "
  //                  "selection-background-color: green; "
  //                  "selection-color: red; }");

  return app.exec();
}

If you just mean the style of the element in its "collapsed" state, I'd take a look at the "Customizing QComboBox" section of the Qt Style Sheets reference for examples on what you are trying to do.

请恋爱 2024-12-26 18:42:57

@Sergey Vlasov:我不知道是否有更好的解决方案来解决您的问题,但是我设法通过以下方法解决了它:

class MyDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        if (option.state & QStyle::State_HasFocus){
            QStyleOptionViewItem my_option = option;
            my_option.state = my_option.state ^ QStyle::State_HasFocus;
            QStyledItemDelegate::paint(painter, my_option, index);
            return;
        }
        QStyledItemDelegate::paint(painter, option, index);
    }
};

然后在组合框中使用您的委托:

QStyledItemDelegate* itemDelegate = new MyDelegate();
    combobox->setItemDelegate(itemDelegate);

这消除了所选项目周围的讨厌框架

@Sergey Vlasov: I don't know if there is a better solution to your problem but , but I managed to solve it with the following:

class MyDelegate : public QStyledItemDelegate
{
protected:
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        if (option.state & QStyle::State_HasFocus){
            QStyleOptionViewItem my_option = option;
            my_option.state = my_option.state ^ QStyle::State_HasFocus;
            QStyledItemDelegate::paint(painter, my_option, index);
            return;
        }
        QStyledItemDelegate::paint(painter, option, index);
    }
};

And then using your delegate in your combobox:

QStyledItemDelegate* itemDelegate = new MyDelegate();
    combobox->setItemDelegate(itemDelegate);

this eliminates nasty frame around selected item

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