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