操纵通过按下按钮创建的对象
Mainpulating a QObject created from a button press
我正在制作一个应用程序,在某个时候,用户将创建某种 from/survey。创建时,用户通过按下按钮选择各种问题类型等,将创建一个新的object。
新建一个栏目,例如:
void CreateSurvey::question_section()
{
QLabel *sectionTitle = new QLabel();
sectionTitle->setText("New Section");
layout->addWidget(sectionTitle);
QLabel *titleLabel = new QLabel("Title");
QLineEdit *titleEdit = new QLineEdit("New Section");
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(titleLabel);
hLayout->addWidget(titleEdit);
layout->addLayout(hLayout);
sectionCount++;
qDebug() << "sections: " << sectionCount;
}
当应用程序为 运行 时,文本 'TitleEdit' 将被用户编辑为部分标题。
假设这已被调用 3 次,所以有 3 个部分。如何获取为每个部分的标题输入的字符串?或者如何获取为特定部分输入的字符串?
谢谢
您可以使用像 QVector
这样的容器来存储您的 QLineEdit
对象。使用此容器访问每个 QLineEdit
对象的文本。
#include <QApplication>
#include <QtWidgets>
class Survey : public QWidget
{
Q_OBJECT
public:
Survey(QWidget *parent = Q_NULLPTR) : QWidget(parent)
{
resize(600, 400);
setLayout(new QVBoxLayout);
layout()->setAlignment(Qt::AlignTop);
QPushButton *button = new QPushButton("Add line edit");
connect(button, &QPushButton::clicked, this, &Survey::addLineEdit);
layout()->addWidget(button);
QPushButton *print_button = new QPushButton("Print all text");
connect(print_button, &QPushButton::clicked, this, [=]
{
for(int i = 0; i < line_edit_vector.size(); i++)
qDebug() << getText(i);
});
layout()->addWidget(print_button);
}
QString getText(int index) const
{
if(line_edit_vector.size() > index)
return line_edit_vector[index]->text();
return QString();
}
private slots:
void addLineEdit()
{
QLineEdit *edit = new QLineEdit("Line edit");
layout()->addWidget(edit);
line_edit_vector.append(edit);
}
private:
QVector<QLineEdit*> line_edit_vector;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Survey survey;
survey.show();
return a.exec();
}
#include "main.moc"
在你的 CreateSurvey
header 添加一个
public slot:
void title_changed();
在您的 question_section
方法中,添加一个连接:
connect(titleEdit,SIGNAL(editingFinished()),this,SLOT(title_changed()));
并添加 title_changed
插槽:
void CreateSurvey::title_changed()
{
QLineEdit *titleEdit=qobject_cast<QLineEdit*>(sender());
if (titleEdit) {
qDebug() << titleEdit->text();
}
}
这样每次编辑行时都会触发插槽 title_changed
。
如果只编辑 一个 就想知道所有标题,请使用此插槽:
void CreateSurvey::title_changed()
{
for (int i = 0; i < layout->count(); ++i) {
QLineEdit *titleEdit=qobject_cast<QLineEdit*>(layout->itemAt(i));
if (titleEdit) {
qDebug() << titleEdit->text();
}
}
}
我正在制作一个应用程序,在某个时候,用户将创建某种 from/survey。创建时,用户通过按下按钮选择各种问题类型等,将创建一个新的object。
新建一个栏目,例如:
void CreateSurvey::question_section()
{
QLabel *sectionTitle = new QLabel();
sectionTitle->setText("New Section");
layout->addWidget(sectionTitle);
QLabel *titleLabel = new QLabel("Title");
QLineEdit *titleEdit = new QLineEdit("New Section");
QHBoxLayout *hLayout = new QHBoxLayout;
hLayout->addWidget(titleLabel);
hLayout->addWidget(titleEdit);
layout->addLayout(hLayout);
sectionCount++;
qDebug() << "sections: " << sectionCount;
}
当应用程序为 运行 时,文本 'TitleEdit' 将被用户编辑为部分标题。 假设这已被调用 3 次,所以有 3 个部分。如何获取为每个部分的标题输入的字符串?或者如何获取为特定部分输入的字符串?
谢谢
您可以使用像 QVector
这样的容器来存储您的 QLineEdit
对象。使用此容器访问每个 QLineEdit
对象的文本。
#include <QApplication>
#include <QtWidgets>
class Survey : public QWidget
{
Q_OBJECT
public:
Survey(QWidget *parent = Q_NULLPTR) : QWidget(parent)
{
resize(600, 400);
setLayout(new QVBoxLayout);
layout()->setAlignment(Qt::AlignTop);
QPushButton *button = new QPushButton("Add line edit");
connect(button, &QPushButton::clicked, this, &Survey::addLineEdit);
layout()->addWidget(button);
QPushButton *print_button = new QPushButton("Print all text");
connect(print_button, &QPushButton::clicked, this, [=]
{
for(int i = 0; i < line_edit_vector.size(); i++)
qDebug() << getText(i);
});
layout()->addWidget(print_button);
}
QString getText(int index) const
{
if(line_edit_vector.size() > index)
return line_edit_vector[index]->text();
return QString();
}
private slots:
void addLineEdit()
{
QLineEdit *edit = new QLineEdit("Line edit");
layout()->addWidget(edit);
line_edit_vector.append(edit);
}
private:
QVector<QLineEdit*> line_edit_vector;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Survey survey;
survey.show();
return a.exec();
}
#include "main.moc"
在你的 CreateSurvey
header 添加一个
public slot:
void title_changed();
在您的 question_section
方法中,添加一个连接:
connect(titleEdit,SIGNAL(editingFinished()),this,SLOT(title_changed()));
并添加 title_changed
插槽:
void CreateSurvey::title_changed()
{
QLineEdit *titleEdit=qobject_cast<QLineEdit*>(sender());
if (titleEdit) {
qDebug() << titleEdit->text();
}
}
这样每次编辑行时都会触发插槽 title_changed
。
如果只编辑 一个 就想知道所有标题,请使用此插槽:
void CreateSurvey::title_changed()
{
for (int i = 0; i < layout->count(); ++i) {
QLineEdit *titleEdit=qobject_cast<QLineEdit*>(layout->itemAt(i));
if (titleEdit) {
qDebug() << titleEdit->text();
}
}
}