如何使用自动添加的 Qt 元素
How to use automatically added Qt-elements
我的程序可以在单击按钮后将新的 QLabel 和 QLineEdits 添加到 QScrollArea。这个想法是创建一个购物清单。我的问题是当单击第二个按钮时我想获取所有 QLineEdits 的文本。但我不知道如何使用这些元素,因为每个新的 QLineEdit 变量都有相同的名称,我不知道如何更改它。
下面是一个小例子:
我的MainWindow.h:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
private slots:
void on_create_clicked();
read_text();
};
#endif // MainWINDOW_H
我的MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_create_clicked()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(read_text()));
i = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::read_text()
{
QString mytext = ui->lineEdit->text();
}
你不能,因为你没有任何指向这些对象的指针或引用,一个解决方案是在你的 class 定义中有一个 QLabel 数组。
例如:
QVector<QLabel*> _labels;
并通过按下按钮一个一个地添加和实例化,然后您将拥有整个对象列表,以及它们的名称
我会简单地将指向每个 QLineEdit
的指针存储在 QVector
中,然后在该向量中循环以获取每个
的文本。
Header:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
#include <QVector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
QVector<QLineEdit *> m_VecLineEdits;
private slots:
void on_create_clicked();
private:
void read_text();
void GetAllTextEdit();
};
#endif // MainWINDOW_H
在 Cpp 文件中,更改以下内容:
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
m_VecLineEdits.push_back(lineEdit); // <-- Line added here to save the pointers in a QVector.
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::GetAllTextEdit()
{
for(int j = 0; j<m_VecLineEdits.size(); ++j)
{
QString lineEditText = m_VecLineEdits.at(j)->text();
/* Do anything with this value */
}
}
如果删除 QLineEdit
,请记得将它们也从 QVector
中删除。
如果您想在每次调用插槽时更改变量的名称(即指向 QLineEdit
的指针),并且假设 i
将保持较小(< 10),例如,您可以使用 switch(i)
并为每种情况选择不同的变量名称,但您必须将所有这些变量存储为 class 的成员。因此,最好将指针存储在 QList 或 QVector 中并循环遍历这些容器以访问每个 QLineEdit 上的 text()
方法。
我的程序可以在单击按钮后将新的 QLabel 和 QLineEdits 添加到 QScrollArea。这个想法是创建一个购物清单。我的问题是当单击第二个按钮时我想获取所有 QLineEdits 的文本。但我不知道如何使用这些元素,因为每个新的 QLineEdit 变量都有相同的名称,我不知道如何更改它。
下面是一个小例子:
我的MainWindow.h:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
private slots:
void on_create_clicked();
read_text();
};
#endif // MainWINDOW_H
我的MainWindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(on_create_clicked()));
connect(ui->pushButton_2, SIGNAL(clicked()), this, SLOT(read_text()));
i = 1;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::read_text()
{
QString mytext = ui->lineEdit->text();
}
你不能,因为你没有任何指向这些对象的指针或引用,一个解决方案是在你的 class 定义中有一个 QLabel 数组。
例如:
QVector<QLabel*> _labels;
并通过按下按钮一个一个地添加和实例化,然后您将拥有整个对象列表,以及它们的名称
我会简单地将指向每个 QLineEdit
的指针存储在 QVector
中,然后在该向量中循环以获取每个
Header:
#ifndef MainWINDOW_H
#define MainWINDOW_H
#include <QMainWindow>
#include <string>
#include <QVector>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
int i;
private:
Ui::MainWindow *ui;
QVector<QLineEdit *> m_VecLineEdits;
private slots:
void on_create_clicked();
private:
void read_text();
void GetAllTextEdit();
};
#endif // MainWINDOW_H
在 Cpp 文件中,更改以下内容:
void MainWindow::on_create_clicked()
{
if(i < 10)
{
i ++;
QLabel *label_2 = new QLabel();
QString s = QString::number(zaehlerHeight) + ". ";
label_2->setText(s);
ui->scrollArea->widget()->layout()->addWidget(label_2);
QLineEdit *lineEdit = new QLineEdit();
m_VecLineEdits.push_back(lineEdit); // <-- Line added here to save the pointers in a QVector.
ui->scrollArea_2->widget()->layout()->addWidget(lineEdit);
}
else{
ui->label->setText("already 10");
}
}
void MainWindow::GetAllTextEdit()
{
for(int j = 0; j<m_VecLineEdits.size(); ++j)
{
QString lineEditText = m_VecLineEdits.at(j)->text();
/* Do anything with this value */
}
}
如果删除 QLineEdit
,请记得将它们也从 QVector
中删除。
如果您想在每次调用插槽时更改变量的名称(即指向 QLineEdit
的指针),并且假设 i
将保持较小(< 10),例如,您可以使用 switch(i)
并为每种情况选择不同的变量名称,但您必须将所有这些变量存储为 class 的成员。因此,最好将指针存储在 QList 或 QVector 中并循环遍历这些容器以访问每个 QLineEdit 上的 text()
方法。