2016-12-15 12 views
0

QObjectのQVectorがQVector<QWidget*> question_vector;です。これらのウィジェットは質問です。 (私のアプリケーションはアンケートのようなものです)。QVectorに格納されたQObjectの側面にアクセスする

アンケートを作成するときに、コンボボックスの選択肢から質問タイプが選択され、質問クラス内に質問が作成され、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

シングルは、ウィジェット、タイトル、titleEdit、コメント、commentsEditです。 ウィジェットの個々のコンポーネントのテキスト、commentsText QLineEditなどにアクセスするにはどうすればよいですか?

+0

を行うことができました:http://stackoverflow.com/questions/41098139/mainpulating-a-qobject-created-from-a-button-press答えを得た。 exaclyあなたの問題は何ですか? –

+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

私は私が(少なくとも部分的に)行うようにしようとしていたものを解くことができたと思います

だから、私は section_commentsText = newLineEdit;QLineEdit *commentsText = new QLineEdit;のようなものを変更して何をしたか、ここで

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); 
} 

を持っていました - 私のquestion.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

関連する問題