2016-12-12 49 views
0

我正在創建一個應用程序,並且在某些時候,用戶將創建某種來自/調查。創建時,用戶通過按下按鈕選擇各種問題類型等,並創建一個新的對象。調用從按鈕創建的QObject按

要創建新的部分,例如:

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個部分。如何獲得爲每個部分的標題輸入的字符串?或者如何獲取爲特定部分輸入的字符串?

感謝

+0

您需要存儲你感興趣的對象,否則你將不(僅僅)訪問他們。 –

+0

我該怎麼做?在數組中? – Phauk

+0

佈局包含您要添加的所有對象。您可以循環並提取所需的數據。 – bibi

回答

1

您可以使用一個容器,像QVector來存儲您的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" 
+0

謝謝,我會去那裏。 下一步是將其存儲爲JSON,以便再次創建相同的調查。那可能嗎? – Phauk

+0

@Phauk好的。看[這個例子](http://doc.qt.io/qt-5/qtcore-json-savegame-example.html)。 – thuga

+0

這真的很有幫助。我打算將QLayouts存儲在我的矢量中。 我正在運行一個快速示例來嘗試存儲一些QLayouts,然後再次打印出來。 'question_vector.append(dLayout); question_vector.append(sLayout); question_vector.append(eLayout); 連接(UI->提交按鈕,&QPushButton ::點擊的,對此,[=] { surveyLayout->的addItem(question_vector.at(0)); });' 然而,當我按下提交按鈕,一個空白空間被添加,而不是我試圖添加的佈局(和小部件)。我究竟做錯了什麼? – Phauk

0
CreateSurvey

添加

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