Qt QComboBox 的 setCompleter 问题

之前看到了一个例子是 QLineEdit setCompleter 的 在QLineEdit上输入就会显示输入提示

例子还说了 QComboBox也有一个以同样工作方式的 setCompleter() 方法.

于是我就想试一下

 

comboBox->setCompleter(directoryCompleter);    //directoryCompleter 是一个模型指针

comboBox->setEditable(true);                                    //comboBox默认是不可编辑的 设置为可编辑的

 

但是程序运行后没有达到应有的效果

究竟是什么原因呢 ? 查看了一下文档

void QComboBox::setCompleter ( QCompleter * completer )

Sets the completer to use instead of the current completer. If completer is 0, auto completion is disabled.

By default, for an editable combo box, a QCompleter that performs case insensitive inline completion is automatically created.

"for an editable combo box"

根据这句话我就把两行代码换了一下位置

comboBox->setEditable(true);                                    //comboBox默认是不可编辑的 设置为可编辑的

comboBox->setCompleter(directoryCompleter);    //directoryCompleter 是一个模型指针

然后程序达到了我想要的效果

希望遇到此问题的同志能顺利解决

一定先要让comboBox可编辑,再 setCompleter();

 

comboBox->setCompleter(QStringList()<<"1234"<<"5678"<<"sdf");    //不传递一个模型 ,传递一个QStringList也是可以的

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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)); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值