2017-12-04 71 views
0

Qt C++ 응용 프로그램에서 QStringList의 내용을 기반으로 버튼을 동적으로 만듭니다 (즉, 버튼 수는 QStringlist의 요소 수와 같고 버튼의 텍스트는 목록의 요소입니다)). 다음동적으로 생성 된 버튼 실행

내 코드

다음
#include "dialog.h" 
#include "ui_dialog.h" 
#include "QFrame" 
#include "QLabel" 
#include "QPushButton" 

Dialog::Dialog(QWidget *parent) : 
QDialog(parent), 

ui(new Ui::Dialog) 
{ 
    ui->setupUi(this); 

} 

Dialog::~Dialog() 
{ 
    delete ui; 
} 

void Dialog::createButtons(){ 

    List<<"Run"<<"Stop""; 

    for(int i=0;i<List.size();i++){ 
     f1 = new QFrame(); 
     a= new QPushButton(); 
     a->setText(List[i]); 
     ui->horizontalLayout->addWidget(a); 
    } 
} 

void Dialog::Run(){ 
    qDebug() << "You clicked on the Run button"; 
} 

void Dialog::Stop(){ 
    qDebug() << "You clicked on the Stop button"; 

} 

void Dialog::on_pushButton_clicked() 
{ 
    createButtons()   
} 

"목록"입니다 내가 사용하는 각각의 QStringList입니다!

단추를 클릭하여 createButtons() 메서드를 호출하면 코드에 표시된대로 단추가 동적으로 만들어집니다!

단추가 qstringlist의 요소 이름을 표시합니다! 각 요소에는 메서드 이름이 있습니다! 그래서 버튼을 클릭 할 때 메소드 이름과 비슷한 메소드가 실행되어야합니다!

이 시나리오에서는 "실행"과 "중지"라는 2 개의 버튼이 생성됩니다. "Run"을 표시하는 버튼을 클릭하면 위에 정의 된 Run() 메서드를 실행하려고합니다!

어떻게하면됩니까?

답변

1

가능한 솔루션은 QMetaObject::invokeMethod()를 사용하는 것입니다 만,이 것이 실행 중지 기능 슬롯해야 할 필요가있다 :

:

private slots: 
    void on_pushButton_clicked(); 
    // slots of button 
    void onClicked(); 
    // your functions 
    void Run(); 
    void Stop(); 

우리가 onClicked 슬롯 버튼에 연결이 작업을 수행하려면

void Dialog::createButtons() 
{ 
    List<<"Run"<<"Stop"; 
    for(const QString &text : List){ 
     QPushButton* a= new QPushButton(this); 
     a->setText(text); 
     ui->horizontalLayout->addWidget(a); 
     connect(a, &QPushButton::clicked, this, &Dialog::onClicked); 
    } 
} 

그리고 sender()을 통해 당신은 신호를 발행 버튼을 얻을, 따라서 당신은 텍스트를 얻을 :

void Dialog::onClicked() 
{ 
    QPushButton *b = qobject_cast<QPushButton *>(sender()); 
    QMetaObject::invokeMethod(this, b->text().toLatin1().data()); 
} 

전체 예제는 다음 link에서 찾을 수 있습니다. QSignalMapper

:

.H

private slots: 
    void on_pushButton_clicked(); 
    void Run(); 
    void Stop(); 
    void onClicked(const QString &text); 
private: 
    QSignalMapper *signalMapper; 

통화 당

Dialog::Dialog(QWidget *parent) : 
    QDialog(parent), 
    ui(new Ui::Dialog) 
{ 
    ui->setupUi(this); 
    signalMapper = new QSignalMapper(this); 
} 

void Dialog::createButtons() 
{ 
    List<<"Run"<<"Stop"; 
    for(const QString &text : List){ 
     QPushButton* a= new QPushButton(this); 
     a->setText(text); 
     ui->horizontalLayout->addWidget(a); 
     connect(a, &QAbstractButton::clicked, signalMapper, QOverload<>::of(&QSignalMapper::map)); 
     signalMapper->setMapping(a, text); 
    } 

    connect(signalMapper, QOverload<const QString &>::of(&QSignalMapper::mapped), this, &Dialog::onClicked); 
} 

void Dialog::onClicked(const QString &text) 
{ 
    QMetaObject::invokeMethod(this, text.toLatin1().data()); 
} 
+0

고맙습니다 많이! 이 시나리오에서 신호 매퍼를 사용할 방법이 있습니까? –

+0

예, 연결을 바꾸기 만합니다. 잠시 후 그 옵션을 보여 드리겠습니다. – eyllanesc

+0

고맙습니다 다시 동생 –