Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 访问QVector中存储的QObject的各个方面_Qt_Qobject_Qvector - Fatal编程技术网

Qt 访问QVector中存储的QObject的各个方面

Qt 访问QVector中存储的QObject的各个方面,qt,qobject,qvector,Qt,Qobject,Qvector,我有一个QObjects的QVectorQVector问题向量。这些小部件是问题。(我的申请就像问卷调查一样) 创建问卷时,从组合框上的选择中选择问题类型,并在Questions类中创建问题,并将其存储在QVector中 void CreateSurvey::comboBox_selection(const QString &arg1) { if(arg1 == "Single Line Text") { Question *singleLineText = ne

我有一个QObjects的QVector
QVector问题向量。这些小部件是问题。(我的申请就像问卷调查一样)

创建问卷时,从组合框上的选择中选择问题类型,并在Questions类中创建问题,并将其存储在QVector中

void CreateSurvey::comboBox_selection(const QString &arg1)
{
    if(arg1 == "Single Line Text")
    {
    Question *singleLineText = new Question("Single Line Text");
    surveyLayout->addWidget(singleLineText);
    question_vector.append(singleLineText);
    qDebug() << "Number of items: "<< question_vector.size();

    } ...
}

void Question::create_singleLineEdit()
{
    QVBoxLayout *vLayout = new QVBoxLayout;
    QLabel *titleLabel = new QLabel("Title");
    vLayout->addWidget(titleLabel);
    QLineEdit *inputText = new QLineEdit;
    vLayout->addWidget(inputText);
    QLabel *commentsLabel = new QLabel("Comments");
    vLayout->addWidget(commentsLabel);
    QLineEdit *commentsText = new QLineEdit;
    vLayout->addWidget(commentsText);

    ui->frame->setLayout(vLayout);
}
void CreateSurvey::组合框_选择(常量QString&arg1)
{
如果(arg1==“单行文本”)
{
问题*单行文本=新问题(“单行文本”);
surveyLayout->addWidget(单线文本);
问题_vector.append(单线文本);
qDebug()addWidget(inputText);
QLabel*commentsLabel=新的QLabel(“注释”);
vLayout->addWidget(commentsLabel);
QLineEdit*commentsText=新的QLineEdit;
vLayout->addWidget(commentsText);
用户界面->框架->设置布局(vLayout);
}

SingleLineEdit是小部件、标题、标题编辑、注释、注释编辑。
如何访问小部件单个组件的文本,例如commentsText QLineEdit?

将元素强制转换为QLineEdit:

QLineEdit *line_edit = dynamic_cast <QLineEdit *> (question_vector[3]);

if (line_edit)
{
   QString text = line_edit->text();
}
QLineEdit*line\u edit=dynamic\u cast(问题向量[3]);
如果(行编辑)
{
QString text=line_edit->text();
}

这是C++编程的一个基本方面,你可能应该对C++类进行一些阅读,如何派生它们,如何使用基类指针和派生类指针等等。

< P>把元素投到一个qLnEEDIT:

QLineEdit *line_edit = dynamic_cast <QLineEdit *> (question_vector[3]);

if (line_edit)
{
   QString text = line_edit->text();
}
QLineEdit*line\u edit=dynamic\u cast(问题向量[3]);
如果(行编辑)
{
QString text=line_edit->text();
}

这是C++编程的一个基本方面,你可能应该对C++类进行一些阅读,如何派生它们,如何使用基类指针和派生类指针等等。 所以我在这里

void Question::create_singleLineEdit()
{
    QVBoxLayout *vLayout = new QVBoxLayout;
    QLabel *titleLabel = new QLabel("Title");
    vLayout->addWidget(titleLabel);
    QLineEdit *inputText = new QLineEdit;
    vLayout->addWidget(inputText);
    QLabel *commentsLabel = new QLabel("Comments");
    vLayout->addWidget(commentsLabel);
    QLineEdit *commentsText = new QLineEdit;
    vLayout->addWidget(commentsText);
    ui->frame->setLayout(vLayout);
}
我所做的是将类似于
QLineEdit*commentsText=newqlineedit;
的内容更改为
section\u commentsText=newLineEdit;
-在我的问题中有
qtexedit*section\u commentsText

然后我就可以做了

Question *object = question_vector[0];
QString text = object->section_commentsText->text();
qDebug() << text;
Question*object=Question_向量[0];
QString text=object->section_commentsText->text();

qDebug()我想我已经设法解决了我想做的事情(至少部分)

所以我在这里

void Question::create_singleLineEdit()
{
    QVBoxLayout *vLayout = new QVBoxLayout;
    QLabel *titleLabel = new QLabel("Title");
    vLayout->addWidget(titleLabel);
    QLineEdit *inputText = new QLineEdit;
    vLayout->addWidget(inputText);
    QLabel *commentsLabel = new QLabel("Comments");
    vLayout->addWidget(commentsLabel);
    QLineEdit *commentsText = new QLineEdit;
    vLayout->addWidget(commentsText);
    ui->frame->setLayout(vLayout);
}
我所做的是将类似于
QLineEdit*commentsText=newqlineedit;
的内容更改为
section\u commentsText=newLineEdit;
-在我的问题中有
qtexedit*section\u commentsText

然后我就可以做了

Question *object = question_vector[0];
QString text = object->section_commentsText->text();
qDebug() << text;
Question*object=Question_向量[0];
QString text=object->section_commentsText->text();

qDebug()你们已经问了类似的问题:并得到了答案。你们的问题是什么?是的,其中一个有行编辑向量[index]->text();获取QVector line_edit_vector的文本;因此,现在我将继续使用Qvectorkstion_vector;由于添加了不同类型的小部件,而不仅仅是lineedits,因此如果我在问题向量[3]的对象中有lineedit,我如何从中获取信息?问题向量[3]->commentsText->text();不起作用您已经问过类似的问题:并得到了答案。您的问题是什么?是的,其中一个有行编辑向量[index]->text();获取QVector line_edit_vector的文本;因此,现在我将继续使用Qvectorkstion_vector;由于添加了不同类型的小部件,而不仅仅是lineedits,因此如果我在问题向量[3]的对象中有lineedit,我如何从中获取信息?问题向量[3]->commentsText->text();不起作用您需要扩展您的问题类以提供对QLineEdit中包含内容的访问。我建议的类型转换不正确;我误解了您的问题类是什么。由于问题封装了一组小部件,您需要向问题添加方法,以便外部调用方可以获取文本:您需要扩展您的问题类提供对QLineEdit中包含的内容的访问。我建议的类型转换不正确;我误解了您的问题类是什么。由于问题封装了一组小部件,您需要向问题添加方法,以便外部调用方可以获取文本: