2016-12-15 17 views
0

나는 QObject의 QVector가 QVector<QWidget*> question_vector;입니다. 위젯은 질문입니다. (신청서는 설문지와 같습니다.)QVector에 저장된 QObject의 측면에 액세스하기

질문지를 만들 때 comboBox의 선택에서 질문 유형이 선택되고 질문 클래스 내에서 질문이 만들어지고 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입니다. 위젯의 개별 구성 요소에있는 텍스트 인 commentsText QLineEdit에 어떻게 액세스합니까?

+0

을 할 다음 수 있었다 : http://stackoverflow.com/questions/41098139/mainpulating-a-qobject-created-from-a-button-press 대답을 얻었다. 그대의 문제는 무엇인가? –

+0

그 중 하나는 line_edit_vector [index] -> text(); QVector의 텍스트를 가져 오는 중 line_edit_vector; 그래서 지금 전진 해 QVector를 가졌습니다 question_vector; 위젯의 다른 유형이 lineedits보다는 추가되기 때문에 question_vector [3]에있는 객체 내에 lineedit이 있으면 그 정보를 어떻게 얻을 수 있습니까? question_vector [3] -> commentsText-> text(); does not work – 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); 
} 
내가 section_commentsText = newLineEdit;QLineEdit *commentsText = new QLineEdit; 같은 물건을 변경되었습니다했다 - 내 questions.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

QLineEdit에 포함 된 내용에 액세스 할 수 있도록 Question 클래스를 확장해야합니다. 내가 제안한 주조가 잘못되었습니다. 나는 당신의 질문 클래스가 무엇인지 오해합니다. Question은 많은 위젯을 캡슐화하므로, 외부 호출자가 텍스트를 가져올 수 있도록 Question에 메소드를 추가해야합니다. – goug