Qt 访问动态 qLineEdit
Qt Acces to dynamic qt LineEdit
我正在编写一个配置器。如果单击 Addbutton
,它会动态添加多个标签和 LineEdit
(请参阅下面的代码)。
我的问题如下:
在我创建 Label
s 和 LineEdit
s 之后它们将被显示,但是如果用户更改 LineEdits
的值,我如何访问该数据?更重要的是,我怎么知道它是哪个(第一个、第二个、第三个)LineEdit
或标签?
在下面的代码中,值没有保存在列表中,但我将在下面的步骤中添加它。我希望你能明白我的问题。我的代码的重点应该放在 "onAbstandSent"
上,在那里我创建了我的 LineEdit
,稍后我需要访问它。正如我提到的,重要的是,我必须知道用户更改了哪个 LineEdit
!
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
counter=0;
ui->setupUi(this);
//Verteiler links
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
//SLOT zum Daten bekommen
void MainWindow::onDNSent (const QString & DN)
{
DNhelp=DN;
qDebug()<<(DNhelp);
//showing DN label
QLabel *DNlabel = new QLabel ();
// DNlabel->sets
DNlabel->setParent(this);
DNlabel->show();
DNlabel->setText(DNhelp);
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
DNlabel->setGeometry(xposition+80,507,40,40);
}
else
{
DNlabel->setGeometry(xposition+45,507,40,40);
}
}
void MainWindow::onPNSent (const QString & PN)
{
PNhelp=PN;
qDebug()<<(PNhelp);
//showing PN label
QLabel *PNlabel = new QLabel ();
// DNlabel->sets
PNlabel->setParent(this);
PNlabel->show();
PNlabel->setText(PNhelp);
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
PNlabel->setGeometry(xposition+80,530,40,40);
}
else
{
PNlabel->setGeometry(xposition+45,530,40,40);
}
}
void MainWindow::onAbstandSent (const double & mm)
{
Abstand=mm;
qDebug()<<(Abstand);
//showing Abstand LineEdit
QLineEdit *Abstandlineedit = new QLineEdit ();
//can this help?
ui->gridLayout->addWidget(Abstandlineedit,counter-1,0);
Abstandlineedit->setParent(this);
Abstandlineedit->show();
Abstandlineedit->setText(QString::number(Abstand));
//lineedit Hintergrund transparent machen
Abstandlineedit->setStyleSheet("background:transparent;");
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
Abstandlineedit->setGeometry(xposition+80,230,40,40);
}
else
{
Abstandlineedit->setGeometry(xposition+15,568,30,20);
}
}
void MainWindow::on_pushButton_add_clicked()
{
//label Verteiler Picture
QPixmap vert_links("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_links");
QPixmap vert_mitte("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_mitte");
QPixmap vert_rechts("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_rechts");
QLabel *label = new QLabel ();
//add Label to the List
xposition=counter*60;
if (counter==0)
{
label->setPixmap(vert_links);
}
else
{
xposition=xposition+35;
label->setPixmap(vert_rechts);
}
if (counter>1)
{
qlist.last()->setPixmap(vert_mitte);
}
qlist.append(label);
label->setParent(this);
label->setGeometry((xposition),500,125,172);
label->show();
//opening choose dialog
stu_info* stufo = new stu_info(this,counter);
connect (stufo, &stu_info::sendDN,this,&MainWindow::onDNSent);
connect (stufo, &stu_info::sendPN,this,&MainWindow::onPNSent);
connect (stufo,&stu_info::sendAbstand,this,&MainWindow::onAbstandSent);
stufo->setModal(true);
stufo->show();
counter++;
}
如果我对你的理解正确,你想知道更改了哪个 lineedit 而不是以编程方式更改它。
如果是这样,您可以将一些 属性 添加到您的 lineedit,然后将您的插槽连接到 textChanged(const QString &)
信号并在插槽中读取您的 属性。
例如:
Abstandlineedit->setProperty("id", someValue);
connect(Abstandlineedit, SIGNAL(textChanged(const QString &)), this, SLOT(slotLineEdit(const QString&)));
....
void MainWindow::slotLineEdit(const QString& s)
{
int id = sender()->property("id").toInt();
//use id
...
}
但是如果您需要在代码中的某处更改您的行编辑而不是作为对信号的反应,那么您肯定需要添加一些指向此小部件的指针向量并使用它。
我正在编写一个配置器。如果单击 Addbutton
,它会动态添加多个标签和 LineEdit
(请参阅下面的代码)。
我的问题如下:
在我创建 Label
s 和 LineEdit
s 之后它们将被显示,但是如果用户更改 LineEdits
的值,我如何访问该数据?更重要的是,我怎么知道它是哪个(第一个、第二个、第三个)LineEdit
或标签?
在下面的代码中,值没有保存在列表中,但我将在下面的步骤中添加它。我希望你能明白我的问题。我的代码的重点应该放在 "onAbstandSent"
上,在那里我创建了我的 LineEdit
,稍后我需要访问它。正如我提到的,重要的是,我必须知道用户更改了哪个 LineEdit
!
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
counter=0;
ui->setupUi(this);
//Verteiler links
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
//SLOT zum Daten bekommen
void MainWindow::onDNSent (const QString & DN)
{
DNhelp=DN;
qDebug()<<(DNhelp);
//showing DN label
QLabel *DNlabel = new QLabel ();
// DNlabel->sets
DNlabel->setParent(this);
DNlabel->show();
DNlabel->setText(DNhelp);
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
DNlabel->setGeometry(xposition+80,507,40,40);
}
else
{
DNlabel->setGeometry(xposition+45,507,40,40);
}
}
void MainWindow::onPNSent (const QString & PN)
{
PNhelp=PN;
qDebug()<<(PNhelp);
//showing PN label
QLabel *PNlabel = new QLabel ();
// DNlabel->sets
PNlabel->setParent(this);
PNlabel->show();
PNlabel->setText(PNhelp);
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
PNlabel->setGeometry(xposition+80,530,40,40);
}
else
{
PNlabel->setGeometry(xposition+45,530,40,40);
}
}
void MainWindow::onAbstandSent (const double & mm)
{
Abstand=mm;
qDebug()<<(Abstand);
//showing Abstand LineEdit
QLineEdit *Abstandlineedit = new QLineEdit ();
//can this help?
ui->gridLayout->addWidget(Abstandlineedit,counter-1,0);
Abstandlineedit->setParent(this);
Abstandlineedit->show();
Abstandlineedit->setText(QString::number(Abstand));
//lineedit Hintergrund transparent machen
Abstandlineedit->setStyleSheet("background:transparent;");
//counter muss hier 1 sein, weil der add button schon durchgelaufen ist und deshalb beim ersten durchlauf counter hier! schon 1 ist
if (counter==1)
{
Abstandlineedit->setGeometry(xposition+80,230,40,40);
}
else
{
Abstandlineedit->setGeometry(xposition+15,568,30,20);
}
}
void MainWindow::on_pushButton_add_clicked()
{
//label Verteiler Picture
QPixmap vert_links("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_links");
QPixmap vert_mitte("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_mitte");
QPixmap vert_rechts("C:/Users/Boushar/Desktop/Bachelor_Fred/Coden/Verteiler_rechts");
QLabel *label = new QLabel ();
//add Label to the List
xposition=counter*60;
if (counter==0)
{
label->setPixmap(vert_links);
}
else
{
xposition=xposition+35;
label->setPixmap(vert_rechts);
}
if (counter>1)
{
qlist.last()->setPixmap(vert_mitte);
}
qlist.append(label);
label->setParent(this);
label->setGeometry((xposition),500,125,172);
label->show();
//opening choose dialog
stu_info* stufo = new stu_info(this,counter);
connect (stufo, &stu_info::sendDN,this,&MainWindow::onDNSent);
connect (stufo, &stu_info::sendPN,this,&MainWindow::onPNSent);
connect (stufo,&stu_info::sendAbstand,this,&MainWindow::onAbstandSent);
stufo->setModal(true);
stufo->show();
counter++;
}
如果我对你的理解正确,你想知道更改了哪个 lineedit 而不是以编程方式更改它。
如果是这样,您可以将一些 属性 添加到您的 lineedit,然后将您的插槽连接到 textChanged(const QString &)
信号并在插槽中读取您的 属性。
例如:
Abstandlineedit->setProperty("id", someValue);
connect(Abstandlineedit, SIGNAL(textChanged(const QString &)), this, SLOT(slotLineEdit(const QString&)));
....
void MainWindow::slotLineEdit(const QString& s)
{
int id = sender()->property("id").toInt();
//use id
...
}
但是如果您需要在代码中的某处更改您的行编辑而不是作为对信号的反应,那么您肯定需要添加一些指向此小部件的指针向量并使用它。