Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

when i write text on QLineEdit that text shows on QLabel



  • Hi Guys ,,
    Can anybody please help me to solve it,,

    1.make new project and show QLabel and QPushButton in QListWidgetItem.
    2. add sample datas to QListWidget.
    3. when i click push button, editbox is showing.
    4. after input some text in edittext, QLabel will be changed with this text in QListWidgetItem.

    when i click on pushbutton QLineEdit is showing but when i write some text on QLineEdit that text should be displayed on QLabel ??
    i dnt knw how to do this??



  • @mrjj thanks for ur reply & thank u all ,,,
    i changed two lines of code,,,now whatever i text ,,that is showing first ,,,this is i wanted anyway thanku all for ur reply ,,i got output wat i wanted!

    Mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_add_clicked()
    {

    QString text=ui->lineEdit->text();
    QListWidgetItem * item = new QListWidgetItem(text);
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    int row = ui->listWidget->row(ui->listWidget->currentItem());
    
    ui->listWidget->insertItem(row, item);
    
    
     ui->lineEdit->clear();
    

    }

    void MainWindow::on_delete_2_clicked()
    {
    delete ui->listWidget->currentItem();
    }

    Ui Page

    0_1547799303419_111.PNG


  • Qt Champions 2017

    Did you study signal & slots topic ? Did you see signals in lineedit and slots in labels? These two hints should be able to solve your issue.



  • MainWindow.h

    signals:
    void textChanged(const QString &text);
    
    //public slots:
    //void recvText(const QString &text);
    
    
    private slots:
      void on_pushButton_clicked();
    
      void on_lineEdit_textChanged(const QString &text);
    
      void on_label_linkActivated(const QString &link);
    

    MainWindow.cpp

       connect(ui->lineEdit,SIGNAL(textChanged(QString)),
              ui->label,SLOT(linkActivated(QString)));
    
    // connect(ui->lineEdit,SIGNAL(textChanged(QString)),
        //      ui->label,SLOT(on_label_linkActivated(QString)));
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
    ui->lineEdit->show();
    ui->label->show();
    
    
    }
    
    void MainWindow::on_lineEdit_textChanged(const QString &text)
    {
       // emit textChanged(const QString  &text);
       // QString text = lineedit->text();
         emit textChanged(text);
    
         ui->label->show();
    
    }
    
    void MainWindow::on_label_linkActivated(const QString &text)
    {
        QString input= text;
        ui->label->clear();
        ui->label->setText(input);
        ui->label->update();
        ui->label->show();
    
    }
    

    can u guess my mistakes pls?



  • I don't see anything related to QListWidget here...



  • 0_1547636824629_ui.PNG
    on ui page i took QListWidget ,,,on qlist widget i placed QPushbutton ,QLineEdit & QLabel

    can u pls send correct code ??



  • Do you want a button and a label for each element in your list or just 1?



  • Thanks for ur reply,,
    No need pushbutton ,,,only QLineEdit & QLabel,,,when i text anything on QLineEdit that text should shows on QLabel thatsit ,,,

    already when i clicked on QPushButton ,,,QLineEdit is showing ,,after write some text on QLineEdit that text should shows on Qlabel


  • Lifetime Qt Champion

    @Dimple
    Hi
    and what does the QListWidget do ?



  • After input some text in edittext, QLabel will be changed with this text in QListWidgetItem.



  • Do you want to add items to the list?
    Should the button enable the editing of existing items?
    Do you really need the button (QListWidget by default does what you want on existing items)?



  • -> yes ,, i want to add items to the list .
    ->i want to add some sample datas(EX: Hi ,,hello,,999,,22...etc)
    just for testing

    Actually the goal we must to do is adding and updating contacts in contacts listwidget.



  • ok, delete your label, lineedit, and pushbutton. try putting in your constructor

    ui->listwidget->model()->insertRows(0,10);
    

    Now there should be 10 blank "labels" in your list widget, if you doubleclick on them a lineedit appears to let you change it and when you move away it goes back into "label mode". All 100% already managed by QListWidget. Is this what you were looking for?

    Actually the goal we must to do is adding and updating contacts in contacts listwidget.

    To add a new line you have 2 options:

    • a button that adds a new line
    • a system that creates a "ghost line" at the end that, when edited will become a real line (more or less like MS Access lets you change tables)


  • suppose if i type 1 it shows on qlistwidget ,,
    next i ll type 2 then on QListWidget shows in order one by one
    ex: 1
    2

    when i type then only it should display in order one by one ,,this is i wanted ???



  • I'm not sure I understand the question, sorry...



  • @VRonin
    There's some sort of "history" mechanism going on here. The user types something somewhere, it is to be added to this list as the first item. User then types something again, this gets added as the next item in the list.... :)

    So to the OP, @Dimple: you need to write code so that after each text entered in the line edit it should insert/append one additional row, setting that row's text from the line edit.



  • @VRonin : its ok

    @JonB : thanks for ur reply ,,im a fresher ,,,can u please if u have a code share with me ??


  • Lifetime Qt Champion

    Hi,

    Did you took a look at the the example code in QListWidget's documentation ? It shows how to add new items.



  • @SGaist
    yes ,,,i added this code now whatever i text ,,,everything is adding in list in order but i want to make it whatever i text ,,that text shows first ??
    void MainWindow::on_add_clicked()
    {
    QString text=ui->lineEdit->text();
    QListWidgetItem * item = new QListWidgetItem(text);
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    ui->listWidget->addItem(item);
    ui->lineEdit->clear();
    }


  • Lifetime Qt Champion

    @Dimple
    Hi
    There are in the order you addItem them.
    However, there is also
    http://doc.qt.io/qt-5/qlistwidget.html#insertItem
    which allows you to insert at any row you want and hence put on top if you wish.



  • @mrjj thanks for ur reply & thank u all ,,,
    i changed two lines of code,,,now whatever i text ,,that is showing first ,,,this is i wanted anyway thanku all for ur reply ,,i got output wat i wanted!

    Mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_add_clicked()
    {

    QString text=ui->lineEdit->text();
    QListWidgetItem * item = new QListWidgetItem(text);
    item->setFlags(item->flags() | Qt::ItemIsEditable);
    int row = ui->listWidget->row(ui->listWidget->currentItem());
    
    ui->listWidget->insertItem(row, item);
    
    
     ui->lineEdit->clear();
    

    }

    void MainWindow::on_delete_2_clicked()
    {
    delete ui->listWidget->currentItem();
    }

    Ui Page

    0_1547799303419_111.PNG


Log in to reply