Qt 通过qlineedit在表中搜索特定条目?

Qt 通过qlineedit在表中搜索特定条目?,qt,Qt,我想要的是,如果我在文本框中输入一个ID,然后按enter键,然后如果ID存在,那么它会显示在表格上。表格的值会借助map插入另一个窗口中,该窗口框1作为map打开。据我所知,我们必须运行map的find命令,然后使用if循环,如果textbox中输入的值存在,那么将以与显示虚拟数据相同的方式显示它。 使用的代码 Box1::Box1(QWidget *parent) :QDialog(parent) { searchgroup = new QGroupBox(t

我想要的是,如果我在文本框中输入一个ID,然后按enter键,然后如果ID存在,那么它会显示在表格上。表格的值会借助map插入另一个窗口中,该窗口框1作为map打开。据我所知,我们必须运行map的find命令,然后使用if循环,如果textbox中输入的值存在,那么将以与显示虚拟数据相同的方式显示它。 使用的代码

Box1::Box1(QWidget *parent)
        :QDialog(parent)
    {
    searchgroup = new QGroupBox(tr("Data Search"));

    QHBoxLayout *layout2 = new QHBoxLayout;
    text = new QLineEdit(this);
    searchh = new QLabel(tr("&Enter ID:"));
    searchh->setBuddy(text);
    layout2->addWidget(searchh);
    layout2->addWidget(text);
    searchgroup->setLayout(layout2);
    tableegroup = new QGroupBox(tr("Searched Data"));
    QVBoxLayout *layout1 = new QVBoxLayout;
    tablee = new QTableView(this);
    mode1 = new QStandardItemModel(1,2,this);
    mode1->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
    mode1->setHorizontalHeaderItem(1, new QStandardItem(QString("DATA")));
    map<int,QString>::iterator itt;
    itt=dataa.begin();
            for (int colu = 0; colu < 2; colu++)
            {
                    item1 = new QStandardItem();

                    if (colu == 0)
                    {
                            item1->setData(((*itt).first), Qt::DisplayRole);
                            mode1->setItem(0,0,item1);
                    } else
                    {
                            item1->setData(((*itt).second), Qt::DisplayRole);
                            mode1->setItem(0,1,item1);
                    }
            }

    tablee->setModel(mode1);
    layout1->addWidget(tablee);
    tableegroup->setLayout(layout1);

    QVBoxLayout *mainlayout1 = new QVBoxLayout;
    //mainlayout1->addWidget(menubarr);
    mainlayout1->addWidget(searchgroup);
    mainlayout1->addWidget(tableegroup);
    setLayout(mainlayout1);
}
Box1::Box1(QWidget*parent)
:QDialog(父级)
{
searchgroup=新QGroupBox(tr(“数据搜索”));
QHBoxLayout*layout2=新的QHBoxLayout;
text=新的QLineEdit(此);
searchh=newqlabel(tr(“&enterid:”);
searchh->setBuddy(文本);
布局2->addWidget(搜索);
布局2->addWidget(文本);
搜索组->设置布局(布局2);
TableGroup=新的QGroupBox(tr(“搜索数据”);
QVBoxLayout*layout1=新的QVBoxLayout;
tablee=新的QTableView(本);
模式1=新的QS标准项目模式(1,2,本);
模式1->setHorizontalHeaderItem(0,新的QStandarItem(QString(“ID”));
模式1->setHorizontalHeaderItem(1,新的QStandarItem(QString(“数据”)));
迭代器itt;
itt=dataa.begin();
for(int colu=0;colu<2;colu++)
{
item1=新的QStandardItem();
if(colu==0)
{
item1->setData((*itt.first),Qt::DisplayRole);
模式1->设置项(0,0,项1);
}否则
{
item1->setData((*itt.second),Qt::DisplayRole);
模式1->设置项(0,1,项1);
}
}
表->设置模型(模式1);
布局1->addWidget(表格);
表组->设置布局(布局1);
QVBoxLayout*mainlayout1=新的QVBoxLayout;
//mainlayout1->addWidget(菜单栏);
mainlayout1->addWidget(搜索组);
mainlayout1->addWidget(TableGroup);
设置布局(主布局1);
}
提前谢谢你的帮助 编辑 我想要什么

void Box1::textReturn()
{
        bool ok;
        int id = text->text().toInt(&ok);
//      map<int,QString>::iterator itt;
        if (ok && dataa.contains(id))
        {

        //      add row (id, data[id] to table
        }
        else
        {
                QMessageBox msgbox = new QMessagebox();
                msgbox->setWindowTitle("Alert");
                msgbox->setText("No such ID present!");
                msgbox->show();
        }
}
void Box1::textreurn()
{
布尔ok;
int id=text->text().toInt(&ok);
//迭代器itt;
如果(确定&&dataa.contains(id))
{
//将行(id,数据[id]添加到表中
}
其他的
{
QMessageBox msgbox=新的QMessageBox();
msgbox->setWindowTitle(“警报”);
msgbox->setText(“不存在这样的ID!”);
msgbox->show();
}
}
EDIT2

void Box1::textReturn()
{
        int id = (text->text()).toInt();
        map<int,QString>::iterator itt;
        itt = dataa.find(id);
        if(itt != dataa.end())           //returns 1 if we found something
        {
                QList<QStandardItem *> items;
                items << new QStandardItem(QString("%1").arg(id));
                items << new QStandardItem((*itt).second);
                mode1->appendRow(items);
                tablee->update();
        }
        else
        {
                QMessageBox *msgbox = new QMessageBox();
                msgbox->setWindowTitle("Alert");
                msgbox->setText("INVALID  ID  ENTERED");
                msgbox->show();
        }
}
void Box1::textreurn()
{
int id=(text->text()).toInt();
迭代器itt;
itt=数据a.find(id);
if(itt!=dataa.end())//如果我们发现了什么,则返回1
{
QList项目;
项目更新();
}
其他的
{
QMessageBox*msgbox=新的QMessageBox();
msgbox->setWindowTitle(“警报”);
msgbox->setText(“输入的ID无效”);
msgbox->show();
}
}

据我所知,你的问题。你需要在Box1类中创建一个新的插槽。我们称它为textReturnPressed()。然后你必须将它连接到
returnPressed()
来自
text的信号

这里是textReturnPressed(我希望它能编译)

void textReturnPressed()
{
布尔ok;
int id=text->text().toInt(&ok);
如果(ok&&dataa.count(id)>0){
QList项目;

items正如@KCiebiera所说,您必须进行此连接

connect(text, SIGNAL(returnPressed()), this, SLOT(textReturnPressed());
然后,您需要在表中使用

QList<QStandardItem *> QStandardItemModel::findItems ( const QString & text, 
                     Qt::MatchFlags flags = Qt::MatchExactly, int column = 0 )
其中,列将是QStandarItem->column()和行QStandarItem->row()

编辑

void Box1::textReturnPressed()
{
int id=(测试->文本()).toInt();
对它进行迭代器;
it=dataa.find(id);
如果(it!=dataa.end())//我们发现了一些东西
{
QList项目;

items您想在何处显示找到的item?只需在表或其他地方激活它?并请更正您的代码。就像现在我插入了一行map的第一个值。此map dataa是其第一个值的map,即int值是我要搜索的。如果找到它,则该值和相应的QString值是我将显示的。和是的,我将显示在一个只包含一行的表中。目前,我插入for循环的值只是一个伪值,我认为您对此感到困惑。谢谢。原因:)@bloodo问题是:如何从映射
dataa
中添加到
table
行中,以匹配在
text
行编辑中输入的数字?是的,这是我想要的。我已经像这样填充了map元素:-dataa.insert(pair(g+1,“HELLO”);g++;并且我在父窗口的表中有如下条目:-for(it=dataa.begin();it!=dataa.end();it++){for(int col=0;col<2;col++{item=new QStandardItem();if(col==0){item->setData((*it).first),Qt::DisplayRole);mode->setItem(k,0,item);}else item->setData(((*it).second),Qt::DisplayRole);mode->setItem(k,1,item);}k++}@KCiebiera基本上我知道如何添加使用项和模式,但不知道如何将输入作为用户的参数,然后如何检查该参数是否匹配。以及如何在用户输入输入后发出按enter键事件的信号。Hanks寻求帮助,但这做了什么dtaa.contains(id)?添加行(id,数据[id]是什么意思到表?我们不需要为map使用一个迭代器,然后运行一个循环类的东西,然后在其中使用if statemnet吗?b什么是bool OK for?如果找到了该值,则会显示该值,如果未找到,则会显示一个消息框,告知未找到值。首先,您需要确保在文本字段中输入了一个数字。因此变量ok的意思是,我们有int(不是ab12)
connect(text, SIGNAL(returnPressed()), this, SLOT(textReturnPressed());
QList<QStandardItem *> QStandardItemModel::findItems ( const QString & text, 
                     Qt::MatchFlags flags = Qt::MatchExactly, int column = 0 )
tablee->showColumn ( int column )
tablee->showRow ( int row )
void Box1::textReturnPressed()
{
     int id = (test->text()).toInt();
     map<int, string>::iterator it;
     it = dataa.find(id);
     if(it != dataa.end())           //we found something
     {
         QList<QStandardItem *> items;
         items << new QStandardItem(QString("%1").arg(id));
         items << new QStandardItem((*it).second);
         mode1->appendRow(items);
     }
     else
         QMessageBox::information(this, "Info", "ID not found!", QMessageBox::ok);
}