Getting QStringList text to line edit based on a string inserted on another line edit
-
In my qt c++ application I have 2 QLine edits! In one Line edit I enter a word which is stored in QStringList! In the other line edit I want to display the Index of that word in the QStringList(with out any button click).
following is my code
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include "QFile"namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
QString name;
QStringList names;explicit MainWindow(QWidget *parent = 0); ~MainWindow();
private slots:
void getNameIndex();
private:
Ui::MainWindow *ui;
};MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
getNameIndex();
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::getNameIndex(){
names<<"John"<<"Smith"<<"Mary"<<"Anne";
QString Name;
QString index;
Name=ui->name->text();
for(int i=0;i<names.size();i++)
{
if(names[i]==Name){
index=i;
}} ui->Index->setText(index); }
Here name and Index are the 2 line edits where the Index of the String given in the name line edit should be displayed after the word is typed by the user(with out any button click)!
Though I called getNameIndex() method in the constructor it did not work! How can I correct this? -
Hi
Hook up the QLine(1) textChanged() to a slot and in that slot
call getNameIndex() -
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(ui->lnName, &QLineEdit::textChanged, this, [=](QString arg){ ui->lnIndex->setText(QString::number(this->getIndex(arg))); }); } MainWindow::~MainWindow() { delete ui; } int MainWindow::getIndex(QString arg) { QStringList names; names << "John" << "Smith" << "Mary" <<"Anne"; for (int i = 0; i < names.size(); i++) { if (names[i] == arg) { return i; } } return -1; }
.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; int getIndex(QString arg); }; #endif // MAINWINDOW_H
-
@Lasith
Yeah, sorry saw that after.
Use @Taz742 solution (which uses lambda and is more smooth)
or try this test project (that does it with your code)
https://www.dropbox.com/s/pozxsv4sd1sa64p/editindex.zip?dl=0 -
@Lasith
Your problems:- You only once call the returning function of the index, and in the designer. (In fact, when ui->name text changes, the function should be called every time.)
@Lasith said in Getting QStringList text to line edit based on a string inserted on another line edit:
QString index;
Name=ui->name->text();
for(int i=0;i<names.size();i++)
{
if(names[i]==Name){
index=i;
}You want the string but in your cycle the 'i' of the integer. Why do not you pay attention to this(index = i)? Try compile the app? If yes, the compiler would inform you about it.
From your profile it seems that you are using Qt too several months. But it does not seem to be at all.
Do not try to conquer the peak, learn to walk first.