2016-12-15 54 views
0

我有QObject的QVector QVector<QWidget*> question_vector;。这些小部件是问题。 (我的申请就像问卷调查一样)。访问存储在QVector中的QObject的方面

当创建问卷时,问题类型从comboBox上的选择中选择,并且在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); 
} 

This is what it looks like

的SingleLineEdit是小工具,标题,titleEdit,评论,commentsEdit。 如何访问,例如widget的单个组件的文本,commentsText QLineEdit?

+0

你已提出类似的问题:http://stackoverflow.com/questions/41098139/mainpulating-a-qobject-created-from-a-button-press并得到答案。什么是你的问题? –

+0

是的,有一个line_edit_vector [索引] - >文本();获取QVector的文本 line_edit_vector;所以现在即时通讯和QVector question_vector;因为不同类型的小部件正在被添加,而不仅仅是lineedits,所以如果我在question_vector [3]的对象中有一个lineedit,我如何从中获取信息? question_vector [3] - > commentsText->文本();不起作用 – Phauk

回答

1

我觉得我已经成功地解决了什么,我试图做(至少部分地)

所以我在这里

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 = new QLineEdit;section_commentsText = newLineEdit; - 在我的问题h中有QTextEdit *section_commentsText

当时我能够做到

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

角色的元件到一个QLineEdit的:

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

if (line_edit) 
{ 
    QString text = line_edit->text(); 
} 

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

+0

您需要扩展您的Question类才能访问QLineEdit中包含的内容。我提出的铸造不正确;我误解你的问题类是什么。由于Question封装了一堆小部件,因此您需要向Question添加方法,以便外部调用者可以获取文本: – goug

相关问题