简体   繁体   中英

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. 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

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. Let's call it textReturnPressed(). Then you have to connect it to returnPressed() signal from text

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

and here is textReturnPressed (I hope it compiles)

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.

As @KCiebiera said, you have to do this connection

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. When u'll get your element (as QStandardItem) you just need to invoke

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

Where your column will be QStandarItem->column() and row 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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