我对Qt很陌生。我试图在这里找到答案,但到目前为止没有成功。我在一个main.cpp文件中有一个复杂结构的向量,我想用它作为我在mainwindow.cpp的void函数中定义的QComplete的输入,该函数创建了一个QLineEdit,我将此QCompleter与之关联。我该如何将此向量转移到我的补全符?main.cpp的一部分:
//... l is a vector based on a struct containing, among other thing , string name.
QApplication a(argc, argv);
QStringList *LocationStringList=new QStringList;
for (int k=0;k!=l.size();k++) {
LocationStringList->append(QString::fromStdString(l[k].name));
}
MainWindow w;
w.show();
MainWindows.cpp的一部分:
void MainWindow::new()
{
...
QCompleter *cmpt;
cmpt=new QCompleter(LocationStringList,this);
cmpt->setCaseSensitivity(Qt::CaseInsensitive);
QLineEdit *locationLineEdit = new QLineEdit();
locationLineEdit->setCompleter(cmpt);
...
它似乎不知道:LocationStringList
你试过什么?通常,你可以这样使用它:
QStringList list;
for(auto& complexStructObject : complexStructList)
list << complexStructObject.getStringForCompletion();
QCompleter* myCompleter = new QCompleter(list, this);
myLineEdit->setCompleter(myCompleter);
在你的例子中,我会把列表传给你的班级:
// main.cpp
// above keeps unchanged
MainWindow w(LocationStringList);
w.show();
// at the end, do not forget to delete!! your string list is not managed; better yet use a unique_ptr
// MainWindow.h
#include <QStringList>
class MainWindow
{
Q_OBJECT
public:
MainWindow(QStringList* stringList);
// ...
};
// MainWindows.cpp
MainWindow::MainWindow(QStringList* stringList)
{
QCompleter *cmpt;
cmpt=new QCompleter(*stringList, this);
cmpt->setCaseSensitivity(Qt::CaseInsensitive);
QLineEdit *locationLineEdit = new QLineEdit();
locationLineEdit->setCompleter(cmpt);
}
new
是一个保留关键字,因此您可能只需要使用构造函数