简体   繁体   English

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

[英]searching for a particular entry in a table by qlineedit?

What i want is that if i enter an ID in the textbox and then press enter,then if the ID is present ,then it gets displayed on the table the valuesof the table are inserted with the help of map in another window from which this window Box1 is opened as map.So as far as i have an idea,we have to run find command of map and then using if loop if that entered value in textbox is presentthen will display it in the same way as dummy data is displayed. 我想要的是,如果我在文本框中输入一个ID,然后按Enter键,那么如果该ID存在,那么它将在表上显示,该表的值将借助map插入到另一个窗口中,该窗口从该窗口插入Box1作为map打开。据我所知,我们必须运行map的find命令,然后使用if循环(如果存在文本框中输入的值)进行显示,则将其显示为与显示伪数据相同的方式。 code used 使用的代码

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);
}

Thanks for any help in advance EDIT what i want 感谢事先编辑我想要的任何帮助

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();
        }
}

EDIT2 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();
        }
}

As far as I understand your question. 据我了解你的问题。 You need to create a new slot in the Box1 class. 您需要在Box1类中创建一个新插槽。 Let's call it textReturnPressed(). 我们称其为textReturnPressed()。 Then you have to connect it to returnPressed() signal from text 然后,您必须将其连接到text returnPressed()信号

connect(text, SIGNAL(returnPressed()), this, SLOT(textReturnPressed());

and here is textReturnPressed (I hope it compiles) 这是textReturnPressed(我希望它可以编译)

void textReturnPressed()
{
     bool ok;
     int id = text->text().toInt(&ok);
     if (ok && dataa.count(id) > 0) {
         QList<QStandardItem *> items;
         items << new QStandardItem(QString("%1").arg(id));
         items << new QStandardItem(dataa[id]);
         mode1.appendRow(items);
     }
}

You don't need an iterator to check if an item is in the map. 您不需要迭代器即可检查地图中是否有项目。 Just call map.count() function. 只需调用map.count()函数即可。

As @KCiebiera said, you have to do this connection 正如@KCiebiera所说,您必须进行此连接

connect(text, SIGNAL(returnPressed()), this, SLOT(textReturnPressed());

Then you need to find your key in the table using 然后您需要使用以下命令在表中找到密钥

QList<QStandardItem *> QStandardItemModel::findItems ( const QString & text, 
                     Qt::MatchFlags flags = Qt::MatchExactly, int column = 0 )

As you have map, so elements shouldn't repeat, your QList should be NULL or contains just one element. 有了地图后,元素就不应重复,您的QList应该为NULL或仅包含一个元素。 When u'll get your element (as QStandardItem) you just need to invoke 当您获取元素(作为QStandardItem)时,只需调用

tablee->showColumn ( int column )
tablee->showRow ( int row )

Where your column will be QStandarItem->column() and row QStandardItem->row(); 您的列将是QStandarItem-> column()和行QStandardItem-> row();

EDIT 编辑

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);
}

Something like this; 像这样的东西;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM