1.建立一個字串列表作為補全資料來源
QStringList wordlist; wordlist<< "apple" <<"banana" << "orange";
2.將QCompleter的資料來源設定為之前建立的QStringList,與QLineEdit控制元件關聯
QCompleter * completer = new QCompleter(wordlist,this); completer->setMaxVisibleItems(6); //設定最大顯示為6行,超過出現捲軸 completer->setCaseSensitivity(Qt::CaseInsensitive); //大小寫不敏感 ui->lineEditName->setCompleter(completer);
3.自定義QCompleter下拉補全列表的樣式
QAbstractItemView* popup = completer->popup(); popup->setFont(QFont("Times New Roman", 13)); popup->setStyleSheet("color: black; background-color: rgb(220, 233, 239);");