qcombobox popup设置border-radius
时间: 2023-10-31 22:06:04 浏览: 225
PyQt QCombobox设置行高的方法
QComboBox的下拉框样式设置border-radius属性后出现黑色边角是因为QComboBox的view窗口属性干扰导致的。为了解决这个问题,你需要设置view窗口的属性以使样式生效。你可以使用以下代码进行设置:
```cpp
ui->comboBox_rand->view()->window()->setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
ui->comboBox_rand->view()->window()->setAttribute(Qt::WA_TranslucentBackground);
```
此外,如果你想要给下拉框添加边缘阴影效果,你可以使用以下代码:
```cpp
QGraphicsDropShadowEffect *shadowRand = new QGraphicsDropShadowEffect(this);
shadowRand->setOffset(0, 0);
shadowRand->setColor(QColor("#444444"));
shadowRand->setBlurRadius(15);
ui->comboBox_rand->view()->setGraphicsEffect(shadowRand);
```
你可以参考这篇博客文章了解更多关于QComboBox样式的信息:https://www.cnblogs.com/csuftzzk/p/qss_combobox.html
另外,如果你想要实现QCompleter的自动补全下拉列表样式,你可以通过completer->popup()进行设置。具体代码参考以下示例:
```cpp
Widget::Widget(QWidget *parent) : QWidget(parent) {
word_list << "Java" << "C" << "C#" << "PHP" << "Perl" << "Python" << "Delphi" << "Ruby";
search_line_edit = new QLineEdit(this);
completer = new QCompleter(this);
string_list_model = new QStringListModel(word_list, this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setModel(string_list_model);
search_line_edit->setCompleter(completer);
connect(search_line_edit, SIGNAL(editingFinished()), this, SLOT(editComplete()));
}
void Widget::editComplete() {
QString text = search_line_edit->text();
if (QString::compare(text, QString("")) != 0) {
bool is_contains = word_list.contains(text, Qt::CaseInsensitive);
if (!is_contains) {
word_list << text;
string_list_model->setStringList(word_list);
//completer->setModel(new QStringListModel(wordList, this));
}
}
}
```
阅读全文